Run-time error 1004 - save as pdf problem

vvujke

New Member
Joined
Apr 27, 2012
Messages
3
Hi guys,

I have a problem when I run this script. don't know what the problem. Debugger gives me Run-time error 1004 and when I stop debugger excel crashes.



Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)Dim ActSheet As Worksheet
    Dim ActBook As Workbook
    Dim CurrentFile As String
    Dim NewFileType As String
    Dim NewFile As String
    Dim pdfName As String
    Dim i As Integer, j As Integer
    If SaveAsUI Then Cancel = True
    Application.ScreenUpdating = False    ' Prevents screen refreshing.
    Application.DisplayAlerts = False
    
    CurrentFile = ThisWorkbook.FullName
 
    NewFileType = "Excel Files 2007 (*.xlsx), *.xlsx," & _
                  "Excel Files 1997-2003 (*.xls), *.xls," & _
                  "All files (*.*), *.*"
 
    NewFile = Application.GetSaveAsFilename( _
        InitialFileName:=ThisWorkbook.Path & "\" & ActiveSheet.Range("F6") & " Invoice " & ActiveSheet.Range("B11"), _
        fileFilter:=NewFileType)
    
  
 If NewFile <> "" And NewFile <> "False" Then
        ActiveWorkbook.SaveAs Filename:=NewFile, _
            FileFormat:=xlNormal, _
            Password:="", _
            WriteResPassword:="", _
            ReadOnlyRecommended:=False, _
            CreateBackup:=False
 
 pdfName = ThisWorkbook.Path & "\PDF Archive\" & ActiveSheet.Range("F6") & " Invoice " & ActiveSheet.Range("B11")
If Dir(pdfName & ".pdf") <> "" Then
    If Dir(pdfName & " copy.pdf") <> "" Then
        i = 1
        j = 1
        Do While i = 1
         If Dir(pdfName & " copy (" & CStr(j) & ").pdf") <> "" Then
              j = j + 1
              i = 1
             Else
              i = 2
         End If
         Loop
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        pdfName & " copy (" & CStr(j) & ").pdf", _
          Quality:=xlQualityStandard, IncludeDocProperties:=True, _
          IgnorePrintAreas:=False, _
          OpenAfterPublish:=False
     End If
         ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        pdfName & " copy" & ".pdf", _
          Quality:=xlQualityStandard, IncludeDocProperties:=True, _
          IgnorePrintAreas:=False, _
          OpenAfterPublish:=False
End If
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        pdfName & ".pdf", _
          Quality:=xlQualityStandard, IncludeDocProperties:=True, _
          IgnorePrintAreas:=False, _
          OpenAfterPublish:=False
        
        Set ActBook = ActiveWorkbook
        Workbooks.Open CurrentFile
        ActBook.Close
    End If
    Application.DisplayAlerts = False
End Sub


Highlighted code is

Code:
<code style="margin: 0px; padding: 0px; font-style: inherit;"></code>    [COLOR=#daa520] ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
[/COLOR][COLOR=#daa520]        pdfName & ".pdf", _[/COLOR]
[COLOR=#daa520]          Quality:=xlQualityStandard, IncludeDocProperties:=True, _[/COLOR]
[COLOR=#daa520]          IgnorePrintAreas:=False, _[/COLOR]
       ->  [COLOR=#daa520]OpenAfterPublish:=False[/COLOR]

It manages to save pdf version of the file, but then excel show's popup with Run-time error 1004, with debug and end options. When I chose debug code is highlighted and if I chose end, Excel crashes.

Can you help, please? truly don't know what the problem tnx

P.S. It was working 'till some time ago and I haven't change anything in the code
 
Last edited:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Did you use:

Debug.Print pdfName & ".pdf"

to see what is in there at the moment of generating the PDF?
Don't you delete the PDF file in case it would already exist in the directory?
 
Upvote 0

Forum statistics

Threads
1,203,329
Messages
6,054,756
Members
444,748
Latest member
knowak87

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