Export to pdf macro

satheo

New Member
Joined
Jun 10, 2014
Messages
34
I found a macro and made some tweaks to get it to export my workbook, but the issue I am having is that I often have multiple similar workbooks open that I want to export. However when I run this macro, it will not always export the workbook I have selected but some other random one that is open in the background.

How can I make this code run on the specific workbook I am working in?

VBA Code:
Sub export_PDF()
    Dim NameOfWorkbook
    With ActiveWorkbook
        ThisWorkbook.Activate
        NameOfWorkbook = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1)) 'removes ".xlsm" from file name
        ThisWorkbook.Sheets(Array("Page 1", "Page 2")).Select
        ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=ThisWorkbook.Path & "\" & NameOfWorkbook, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False
        ActiveWorkbook.Close SaveChanges:=False
    End With
End Sub

Thank you.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi satheo,

ActiveWorkbook: workbook which is active in Excel Window
ThisWorkbook: workbook with code

Try

VBA Code:
Sub export_PDF()
  Dim NameOfWorkbook As String
  With ActiveWorkbook
    .Activate 'not necessary as ActiveWorkbook is just that
    NameOfWorkbook = Left(.Name, (InStrRev(.Name, ".", -1, vbTextCompare) - 1)) 'removes ".xlsm" from file name
    .Sheets(Array("Page 1", "Page 2")).Select
    ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=.Path & "\" & NameOfWorkbook, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False
    .Close SaveChanges:=False
  End With
End Sub

Ciao,
Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,214,624
Messages
6,120,591
Members
448,973
Latest member
ksonnia

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