Macro for pdf

pklimchuk

Board Regular
Joined
Oct 8, 2010
Messages
101
Hello


I need a Macro that would Copy each Worksheet in a Workbook into a Separate PDF file and sale to a Folder
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Make sure that the workbook containing the worksheets is the active workbook, then try...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] WorksheetsToPDF()

    [color=darkblue]Dim[/color] wks [color=darkblue]As[/color] Worksheet
    [color=darkblue]Dim[/color] strDestPath [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] strFile [color=darkblue]As[/color] [color=darkblue]String[/color]
    
    strDestPath = "C:\Users\Domenic\Desktop\"  [color=green]'change the destination path accordingly[/color]
    
    [color=darkblue]If[/color] Right(strDestPath, 1) <> "\" [color=darkblue]Then[/color] strDestPath = strDestPath & "\"
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] wks [color=darkblue]In[/color] ActiveWorkbook.Worksheets
        strFile = strDestPath & wks.Name & ".pdf"
        wks.ExportAsFixedFormat xlTypePDF, strFile
    [color=darkblue]Next[/color] wks
    
    MsgBox "Completed...", vbInformation
        
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]

Note that the ExportAsFixedFormat method has other parameters. Have a look at the help file for details.
 
Upvote 0
Is it anyway Possible when the File is saved show a date after the Files Name for the PDF , Date has to be prior month.

EX Book 7-2011
 
Upvote 0
Try the following changes in red...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] WorksheetsToPDF()

    [color=darkblue]Dim[/color] wks [color=darkblue]As[/color] Worksheet
    [color=darkblue]Dim[/color] strDestPath [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] strFile [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=red]Dim strDate As String[/color]
    
    strDestPath = "C:\Users\Domenic\Desktop\"  [color=green]'change the destination path accordingly[/color]
    
    [color=darkblue]If[/color] Right(strDestPath, 1) <> "\" [color=darkblue]Then[/color] strDestPath = strDestPath & "\"
    
    [COLOR="Red"]strDate = Format(DateAdd("m", -1, Date), "m-yyyy")[/COLOR]
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] wks [color=darkblue]In[/color] ActiveWorkbook.Worksheets
        [COLOR="Red"]strFile = strDestPath & wks.Name & " " & strDate & ".pdf"[/COLOR]
        wks.ExportAsFixedFormat xlTypePDF, strFile
    [color=darkblue]Next[/color] wks
    
    MsgBox "Completed...", vbInformation
        
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
Domenic

I got a new computer and now the macro will not work. Getting an error massage.

Run Time Error '1004':
document not saved. the document may be open, or an error many have been encountered when saving.

When I hit "DEBUG"

IT Points to;

wks.ExportAsFixedFormat xlTypePDF, strFile
 
Upvote 0
Just out of curiosity, because you said you got a new computer, you do have adobe reader or something installed right? I was just wondering because of where the debug is hitting.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,267
Members
452,902
Latest member
Knuddeluff

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