Runtime error 1004 for code that worked fine - excel

I have the following code in a spreadsheet that's been used for months in the office:
Private Sub CommandButton2_Click()
With Sheets("Summary").Range("B2:H83")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="F:\Export.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End With
End Sub
Today, when I try to execute I get a Runtime error 1004.
We did a recent Office 365 update, could that be the culprit?

Related

Run-time error '-2147024773 (8007007b) Document not saved

Could anyone pls help regarding below, i am unable to save as pdf
AF26 (=TEXT("1st half"&AE36,)
sub hihihi()
Dim fName As String
fName = Range("AF26").Value
Range("A1:Q22").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\user\Google 雲端硬碟 (staff01.phc#gmail.com)\xxx\fax\" & fName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

Create PDF compile error "Application: defined or object-defined error"

I have a VBA userform where the user searches the database sheet for certain terms. It generates the result in a separate sheet and when the user clicks the print button it creates a PDF that includes all the data in the search result sheet.
It is giving the compile error
"Application: defined or object-defined error"
Sub CreatePDF()
Sheet4.Select
Range("A1:N25").Select
ThisWorkbook.Sheets("SearchData").Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="D:\AliBinAli\Security Visitor App\temp.pdf", _
Quality:=xlQualityStandard, _
Orientation:=xlLandscape, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
Sheet4.Select
Range("A1").Select
End Sub
The highlighted line is:
Selection.ExportAsFixedFormat _ Type:=xlTypePDF, _
Filename:="D:\AliBinAli\Security Visitor App\temp.pdf", _
Quality:=xlQualityStandard, _
Orientation:=xlLandscape, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
Orientation
PageSetup.Orientation
Range.Orientation
There is no Orientation argument in the Worksheet.ExportAsFixedFormat method.
You could try this:
Option Explicit
Sub CreatePDF()
With ThisWorkbook.Worksheets("SearchData")
.PageSetup.Orientation = xlLandscape
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="D:\AliBinAli\Security Visitor App\temp.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End With
With Sheet4
.Activate
.Range("A1").Select
End With
End Sub
or if you wanted to print the range:
Sub CreatePDF2()
With Sheet4.Range("A1:N25")
.Worksheet.PageSetup.Orientation = xlLandscape
.Orientation = xlHorizontal
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="D:\AliBinAli\Security Visitor App\temp.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
.Worksheet.Activate
.Cells(1).Select
End With
End Sub

Saving Range Selection as PDF on Mac

I have the following code to try and export a specific selection as a one-page PDF. I was able to grant access but I can't seem to be able to export the selection as a PDF. I get a 1004 error. Any advice?
Sub PrintTest()
ChDir "/Users/Mr_Madoff/Desktop/"
Sheets("Sheet1").Range("A1:F53").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/Users/Mr_Madoff/Desktop/Book1.pdf", Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub

PDF print to file path VBA

I have the below code I have researched and got to work in one of my workbooks. The idea of the code is to automatically print a sheet to PDF and save down in a directory of my choosing naming it the contents of a specific cell.
All works fine until I try to copy into a new workbook (I designed in a test workbook as not to corrupt the original) then I get a
run time error 1004
and the ThisWorkbook.Sheets(Array("sheet 2")).Select is highlighted when I debug. Am I missing something simple? as the code works in the original workbook but not in if I paste into a new module in a new workbook? Sorry i'm quite new to this!
sub PrintPDFRT()
Sheets("test").Activate
ActiveSheet.UsedRange.Select
Sheets("malbru1").Activate
ActiveSheet.UsedRange.Select
Sheets("sheet 2").Activate
Range("A1:j137").Select
ThisWorkbook.Sheets(Array("sheet 2")).Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="FIle path\" & Range("L7").Value, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
Try it like this: (without Array)
sub PrintPDFRT()
WorkSheets("test").Activate
ActiveSheet.UsedRange.Select
WorkSheets("malbru1").Activate
ActiveSheet.UsedRange.Select
WorkSheets("sheet 2").Activate
Range("A1:j137").Select
ThisWorkbook.WorkSheets("sheet 2").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="FIle path\" & Range("L7").Value, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub

Saving sheet as PDF on PC and Mac

In trying to get a simple function as "Save active sheet to PDF" running on an Apple Mac Office 2016 I came across the code below.
However, whilst it runs perfectly on Windows it fails on Mac with a "Print - Error while printing" message followed by Run-time error 1004: Method 'ExportAsFixedFormat' of object '_Worksheet' failed.
I have tried many variations but none seem to work.
Sub CreatePDF()
Dim wksSheet As Worksheet
Dim TheOS As String
TheOS = Application.OperatingSystem
If InStr(1, TheOS, "Windows") > 0 Then
Set wksSheet = ActiveSheet
wksSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & "Sheet1 on Windows", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
Exit Sub
Else
Set wksSheet = ActiveSheet
wksSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & ":" & "Sheet1 on Mac", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
Exit Sub
End If

Resources