VBA, PDF code. Previously working, now inexplicably stopped

sanantonio

Board Regular
Joined
Oct 26, 2021
Messages
124
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Happy new year and all that. For the past 6 months and right up until the holidays I've been using a VBA code passed onto me by a colleague that automatically PDF's 1 worksheet, attached to an email and sends it out.

I've come back after the holidays and now get an error?

VBA Code:
Sub Button10_Click()

  Dim IsCreated As Boolean
  Dim i As Long
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object

 
  ' Not sure for what the Title is
  Title = Range("Q2")
 
  ' Define PDF filename
  PdfFile = Range("Q2")
  i = InStrRev(PdfFile, ".")
  If i > 1 Then PdfFile = Left(PdfFile, i - 1)
  PdfFile = PdfFile & "_" & ".pdf"
 
  ' Export activesheet as PDF
  With Worksheets("Data")
        ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With
 
  ' Use already open Outlook if possible
  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  On Error GoTo 0
 
  ' Prepare e-mail with PDF attachment
  With OutlApp.CreateItem(0)
  
    ' Prepare e-mail
    .Subject = Title
    .To = "xxxx@xxxx.us" ' <-- Put email of the recipient here
    .CC = "xxxx@xxxx.us" ' <-- Put email of 'copy to' recipient here
    .Body = "Hi," & vbLf & vbLf _
          & "Please see the attached." & vbLf & vbLf _
          & "Regards xxxx Team" & vbLf _
          & Application.UserName & vbLf & vbLf
    .Attachments.Add PdfFile
  
    ' Try to send
    On Error Resume Next
    .Send
    Application.Visible = True
    If Err Then
      MsgBox "E-mail was not sent", vbExclamation
    Else
      MsgBox "E-mail successfully sent", vbInformation
    End If
    On Error GoTo 0
  
  End With
 
 On Error Resume Next
 
  ' Delete PDF file
  Kill PdfFile
 
  On Error Resume Next
 
  ' Quit Outlook if it was created by this code
  If IsCreated Then OutlApp.Quit
 
  ' Release the memory of object variable
  Set OutlApp = Nothing
 
End Sub

The line .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False being the one that all of a sudden is showing an error despite months of it working?

Q2 contains the name. Which is a concat of "Subrange Audit and the date". (Thought it might be this as the date has changed to the new year? But it doesn't seem to have a difference. And "Data" is the tab it PDFs, that tab still exists in the same exact form it did previously.

Any advice?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
What error do you get?
Also you missing the period from the beginning of that line.
 
Upvote 0
What error do you get?
Also you missing the period from the beginning of that line.
Sorry that would help. It's this error:

1641830442359.png


Yeah period is there in the code but deleted here when copying and pasting.
 
Upvote 0
In that case what is the exact value of Q2?
 
Upvote 0
You are using slashes in the date portion of the file name. Slashes are not valid characters for file names.
Maybe try using the text format of "mm-dd-yyyy" instead.
That was it!

Not sure how it was working previously or how it got changed to / but that's solved it! Thanks muchly
 
Upvote 0

Forum statistics

Threads
1,215,805
Messages
6,126,998
Members
449,351
Latest member
Sylvine

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