runtime error code 1004

daveeps

New Member
Joined
Nov 15, 2021
Messages
2
Platform
  1. Windows
hi ,
can anyone help with the below code ?
the code is supposed to export 2 worksheets in the same workbook to separate pdfs ,
saved to an external drive, with creating a folder with the name of cell F1 .
then one of the pdf files added to a new outlook message .

but all i get is a "runtime error code 1004"
VBA Code:
' Generate a PDF file and attach to new email

Sub PublishProjectPDFs()

   Const olMailItem = 0

   Dim MyMailItem As Object ' MailItem
   Dim NewRecipient As Object ' Recipient
   Dim PDFFileName As String
   Dim OutlookApp As Object
   Dim Path As String
   
   Set OutlookApp = CreateObject("Outlook.Application")
   
   ThisWorkbook.Sheets(Array("COSTING", "QUOTATION")).Select
   
   Path = "Z:\quotations\" & Worksheets("COSTING").Range("F1") & "\"

   PDFFileName = Worksheets("COSTING").Range("A1")
   
   Worksheets("COSTING").ExportAsFixedFormat _
      Type:=xlTypePDF, _
      Filename:=Path & "COSTING", _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=False

   Worksheets("QUOTATION").ExportAsFixedFormat _
      Type:=xlTypePDF, _
      Filename:=Path & "QUOTATION", _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=False
   
   Set MyMailItem = OutlookApp.CreateItem(olMailItem)
   
   MyMailItem.display
   
   Set NewRecipient = MyMailItem.Recipients.Add("Recipient Name <email@address.com>")
   NewRecipient.Type = olTo
   NewRecipient.Resolve
   
   MyMailItem.Subject = "Your subject here"
   MyMailItem.body = "Message for email body here"
   MyMailItem.Attachments.Add Path & "QUOTATION"
   
End Sub

and when i debug it highlights this part of the code
VBA Code:
   Worksheets("COSTING").ExportAsFixedFormat _
      Type:=xlTypePDF, _
      Filename:=Path & "COSTING", _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=False

any ideas ?
will be much appreciated if anyone can help
thanks in advance
dave
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
What was the error description under the error code 1004?
 
Upvote 0
many thanks for your reply , but i have managed to fix this .
the error was that the folder was non existent and needed to be created by the code .
so added this bit of code to create a folder with the name that i wanted .

VBA Code:
      On Error Resume Next
   MkDir Path
   On Error GoTo 0
 
Upvote 0
Solution
Yes.. several reasons for error 1004. The description will tell normally.
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,882
Members
449,097
Latest member
dbomb1414

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