0

I set up my Excel workbook to generate Outlook emails with either a Word or pdf attachment from data entered into a table using VBA.

When I enter the criteria to generate the email with attachment, the attachment name puts "John%20Doe" instead of "John Doe".

How can I get rid of the %20 and have the space between first and last name instead?


Sub CreateWordDocuments()

    'CREATE A WORD DOCUMENT TO TRANSFER INFORMATION FROM FILTERED DATA INTO A WORD 
TEMPLATE
Dim VSCRow, VSCCol, LastRow, TemplRow, MonthNumber, FromMonth, ToMonth, DaysOfMonth, 
FromDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim WordContent As Word.Range
With Sheet5


If .Range("B3").Value = Empty Then
   MsgBox "Please select the correct template from the drop down list"
.Range("F4").Select
Exit Sub
End If
TemplRow = .Range("B3").Value ' Set the Template Value
TemplName = .Range("F4").Value ' Set Template Name
MonthNumber = .Range("V4").Value 'Set the Month Number
FromMonth = .Range("W4").Value
ToMonth = .Range("Y4").Value
DaysOfMonth = .Range("AA4").Value
FromDays = .Range("AC4").Value
ToDays = .Range("AF4").Value
DocLoc = Sheet10.Range("F" & TemplRow).Value ' Word Document Filename

'Open Word Template
On Error Resume Next 'If Word is already open
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
' Launch a new instance of Word
Err.Clear
'On Error GoTo Error_Handler
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True 'Make the application visible to the user
End If

LastRow = .Range("E99999").End(xlUp).Row 'Determine the last Row
For VSCRow = 8 To LastRow
    MonthNumber = .Range("X" & VSCRow).Value
    DaysOfMonth = .Range("AF" & VSCRow).Value
    If TemplName <> .Range("Z" & VSCRow).Value And MonthNumber >= FromMonth And 
MonthNumber <= ToMonth And DaysOfMonth >= FromDays And DaysOfMonth <= ToDays Then
        Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) ' Open 
Template
        For VSCCol = 5 To 42 'Move through the colunms for information
            TagName = .Cells(7, VSCCol).Value 'Tag Name
            TagValue = .Cells(VSCRow, VSCCol).Value 'Tag Value
            With WordDoc.Content.Find
                .Text = TagName
                .Replacement.Text = TagValue
                .Wrap = wdFindContinue
                .Execute Replace:=wdReplaceAll   'Forward:True, Wrap:=wdFindContinue
            End With
        Next VSCCol
        
    If .Range("H4").Value = "PDF" Then
        FileName = ThisWorkbook.Path & "\" & .Range("E" & VSCRow).Value & ".pdf" ' 
Create full filename and path with current workbook
        WordDoc.ExportAsFixedFormat OutputFileName:=FileName, 
ExportFormat:=wdExportFormatPDF
        WordDoc.Close False
    Else:
        FileName = ThisWorkbook.Path & "\" & .Range("E" & VSCRow).Value & ".docx"
        WordDoc.SaveAs FileName
    End If
      
.Range("Z" & VSCRow).Value = TemplName 'Template Name to use
.Range("AA" & VSCRow).Value = Now

If .Range("S4").Value = "Email" Then
Set OutApp = CreateObject("Outlook.Application") 'Create Outlook Application
Set OutMail = OutApp.CreateItem(0) 'Create The Email
With OutMail
.To = Sheet5.Range("Y" & VSCRow).Value
.Subject = "Performance Metrics Verification,  " & Sheet5.Range("R" & VSCRow).Value & " 
- " & Sheet5.Range("S" & VSCRow).Value & ", " & Sheet5.Range("T" & VSCRow).Value
.Body = "Good afternoon, " & Sheet5.Range("E" & VSCRow).Value & ", here are your " & 
Sheet5.Range("R" & VSCRow).Value & " - " & Sheet5.Range("S" & VSCRow).Value & ", " & 
Sheet5.Range("T" & VSCRow).Value & " performance metrics as captured by the WFW database 
systems. Please review and sign. Comments may be included in the email body. Please 
return to me by COB " & Sheet5.Range("AG" & VSCRow).Value & ", If this date falls on a 
holiday, return on the next business day following the holiday."
.Attachments.Add FileName
.Display 'To send without displaying .Display to .Send
End With

Else
    WordDoc.PrintOut
    WordDoc.Close
End If
    Kill (FileName) 'Deletes the PDF or Word that was just created
End If '3 conditions are met
Next VSCRow
WordApp.Quit
End With
End Sub

Community
  • 1
  • 1
  • 2
    Does this answer your question? [Does VBA have any built in URL decoding?](https://stackoverflow.com/questions/4998715/does-vba-have-any-built-in-url-decoding) – Jeremy Thompson Feb 24 '22 at 22:50
  • I agree with Jeremy. If you can't avoid URL encoding your text in the first place you will need to decode it. I recommend stopping the URL encoding at the source so you don't have to do that. – HackSlash Feb 24 '22 at 23:07

0 Answers0