Issue with spaces and %20 in VBA macro

ElCap99

New Member
Joined
Jan 12, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I'm fairly new to this so bear with me.

I have a code that stores a sheet at a specific location in sharepoint, then attaches the file to an outlook email with some text etc. and displays it.

The filename in sharepoint turns out fine, but the in the attachment all spaces in the pdf file name is replaced with %20.

Can anyone please help with spotting the error in my code?

CODE:
Sub SavePdfAndSendEmail()

On Error GoTo err_handler

Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem

Dim currentDate As String
Dim project As String
Dim mottaker As String
Dim folderPath As String
Dim pdfFileName As String
Dim pdfFullName As String
Dim duplicateNumber As Long

'// Construct the file name
currentDate = Format(Date, "dd-mm-yyyy")
project = Sheets("Endringsskjema UE").Range("E13").Value
mottaker = Sheets("Endringsskjema UE").Range("I13").Value

' folderPath = "C:\Temp\"
folderPath = Sheets("Endringsskjema UE").Range("J5").Value

pdfFileName = "Varsel nr " & Sheets("Endringsskjema UE").Range("E10").Value & " - " & project & " - " & mottaker & " - " & currentDate
duplicateNumber = 1

''' Dir() virker ikke direkte i Sharepoint så her må det lages en ny sjekk
''' Evt prøv å åpne, skal da gi feil.
''' Hvis filen kan åpnes finnes den og duplicate number må da genereres...


' '// Check if the file already exists
' If Dir(folderPath & pdfFileName & ".pdf") <> "" Then
' '// The file exists so append duplicateNumber to the file name and check if that also exists, if it does increment duplicateNumber and test again. Rinse and repeat.
' Do While Dir(folderPath & pdfFileName & "-" & Format(duplicateNumber, "000") & ".pdf") <> ""
' duplicateNumber = duplicateNumber + 1
' Loop
' '// Construct the new file name
' pdfFileName = pdfFileName & "-" & Format(duplicateNumber, "000")
' End If

'// Construct the full name (path, file name and extension)
pdfFullName = folderPath & pdfFileName & ".pdf"

'// Export the workbook as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfFullName

'// Open Outlook and create a new email
Set oApp = New Outlook.Application
Set oMail = oApp.CreateItem(olMailItem)

With oMail
.To = Sheets("Endringsskjema UE").Range("J4").Value
.CC = Sheets("Info og PK-plan").Range("E4").Value
.Subject = "Varsel om endring eller utrekk fra kontrakt_" & pdfFileName
.body = "Se vedlagt varsel om endring eller uttrekk fra kontrakt " 'This is the text that will appear in the body of the email. Remove it if not needed.
.Attachments.Add Source:=pdfFullName, Type:=xlTypePDF
.Display 'This will display the email so you can review it before sending. If you want to send it automatically replace .Display with .Send
End With

clean_exit:
Set oMail = Nothing
Set oApp = Nothing
Exit Sub


err_handler:
'Something has gone wrong, spit out an error messsage
MsgBox "The following error has occured: " & vbNewLine & Err.Number & ": " & Err.Description, vbCritical, "Error!"
GoTo clean_exit

End Sub

Outlook:
1642013291147.png


Sharepoint:
1642013339632.png
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top