1

im trying to save multiple worksheets as single pdf file.
Following macro is using Sheets names, but it does not fit me well as my sheets have dynamic names. Was trying to use Sheets(1) addressing but did not work. Does anyone have any idea?

Sub export_to_pdf()  
  Sheets(Array("Configuration", "chart")).Copy   
  ActiveWorkbook.ExportAsFixedFormat _    
    Type:=xlTypePDF, _  
    Filename:="filename.pdf", _     
    Quality:=xlQualityStandard, _  
    IncludeDocProperties:=False, _   
    IgnorePrintAreas:=False, _   
  OpenAfterPublish:=True
  ActiveWorkbook.Close (False)  
End Sub
INOPIAE
  • 293
  • 1
  • 8
Pythonist
  • 109
  • 1
  • 8

2 Answers2

3

use

Sheets(1).ExportAsFixedFormat _
          Type:=xlTypePDF, _
          Filename:="filename.pdf", _
          Quality:=xlQualityStandard, _
          IncludeDocProperties:=False, _
          IgnorePrintAreas:=False, _
          OpenAfterPublish:=True

or if you have more

Sheets(Array(1, 2, 5)).Copy

ActiveWorkbook.ExportAsFixedFormat _
               Type:=xlTypePDF, _
               Filename:="filename.pdf", _
               Quality:=xlQualityStandard, _
               IncludeDocProperties:=False, _
               IgnorePrintAreas:=False, _
               OpenAfterPublish:=True

ActiveWorkbook.Close (False)
user3598756
  • 28,893
  • 4
  • 18
  • 28
0

Your program code works for me with minor changes indicate here-under.

Sub export_to_pdf()
  Sheets(Array("Sheet1", "Sheet2")).Copy 'Change to your sheets 
  ActiveWorkbook.ExportAsFixedFormat _
  Type:=xlTypePDF, _
  Filename:="filename.pdf", _
  Quality:=xlQualityStandard, _
  IncludeDocProperties:=True, _  'changed to True
  IgnorePrintAreas:=False, _
  OpenAfterPublish:=True
  ActiveWorkbook.Close (False)
End Sub

In such cases I set reference to Microsoft Word Object Library also in Tools Reference

skkakkar
  • 2,772
  • 2
  • 17
  • 30
  • How would you modify this code to use the sheets the user had `Selected` just before running the Sub ?? – Gary's Student May 15 '16 at 17:32
  • @Gary's Student I analyse the code on my sample data and for testing it changed to sheet1 and sheet2. This works for me as indicated. However , I am sorry that I misunderstood the OP requirement. It is that he wants to select sheets with dynamic names. I want to withdraw my answer but I do not know how it done here. – skkakkar May 15 '16 at 17:41
  • **Please do not delete this answer.....I was only asking a question!** the answer may be O.K. for Wariat....... – Gary's Student May 15 '16 at 17:43
  • @user3598756 program code meets OP requirements fully. Credit to him. My observations on OP code may please be ignored. – skkakkar May 15 '16 at 17:49