VBA to save a worksheet to a PDF in 2007

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
I tried to use the macro recorder for this. I have a workbook with 7 sheet. Lets call the sheets one, two, three, four, five, six, and seven.

I want to save 6 of these sheets as PDFs. Lets say the sheets that I want to save are two, three, ..., seven. I do not want the save as prompt as part of the macro (here is where I run into problems with the macro recorder). I just want to run the macro and have them all saved.
(Even better, I would like them saved to sub directory "My Online Files" - but that is a minor point that I could live without).

Thanks for any help here

Gene Klein
 
I have double checked the path for errors. However, I just checked and it does run if I change strPath to just be equal to "C:\"

Use again Debug.Print, for instance
Code:
Debug.Print strPath & wks.Name & ".pdf"</pre>

It should not be impossible to check manually if that is correct or not.
I guess this will be the problem.

Or maybe you are not allowed to write in the said directory?
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You would think that typo's are spotted easily with a Debug.Print for someone approaching 2,000 posts over here. Hmmm.

How would a debug.print reveal that I am not allowed to write to the directory - your suggestion above?

Gene Klein
 
Upvote 0
Whether you have Write rights to the directory or not, is easily verified and should not be done with a Debug.Print: browse to the directory and try to make a new file there (a simple text file suffices).
 
Upvote 0
The following code is amended to check whether the specified path exists...

Code:
Sub CreatePDFs()
    
    Dim strPath As String
    Dim wkbSource As Workbook
    Dim wks As Worksheet
    
    strPath = "C:\My Documents\Broward College\MAT 1033 Online Class Documents\"
    
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    
    [COLOR="Red"]If Len(Dir(strPath, vbDirectory)) = 0 Then
        MsgBox "The path does not exist!", vbExclamation
        Exit Sub
    End If[/COLOR]
    
    Set wkbSource = ActiveWorkbook
    
    For Each wks In wkbSource.Worksheets
        Select Case wks.Name
            Case "Entire Course for viewing", "Entire Course for printing"
                 'Do nothing
            Case Else
               wks.ExportAsFixedFormat xlTypePDF, strPath & wks.Name & ".pdf"
        End Select
    Next wks
    
End Sub
 
Upvote 0
The following code is amended to check whether the specified path exists...

Code:
Sub CreatePDFs()
 
    Dim strPath As String
    Dim wkbSource As Workbook
    Dim wks As Worksheet
 
    strPath = "C:\My Documents\Broward College\MAT 1033 Online Class Documents\"
 
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
 
    [COLOR=red]If Len(Dir(strPath, vbDirectory)) = 0 Then[/COLOR]
[COLOR=red]       MsgBox "The path does not exist!", vbExclamation[/COLOR]
[COLOR=red]       Exit Sub[/COLOR]
[COLOR=red]   End If[/COLOR]
 
    Set wkbSource = ActiveWorkbook
 
    For Each wks In wkbSource.Worksheets
        Select Case wks.Name
            Case "Entire Course for viewing", "Entire Course for printing"
                 'Do nothing
            Case Else
               wks.ExportAsFixedFormat xlTypePDF, strPath & wks.Name & ".pdf"
        End Select
    Next wks
 
End Sub

You are the best Dom. Thanks

Gene Klein
 
Upvote 0
You're very welcome! Glad I could help!

It seems that if you do not "tell" vba anything other than worksheet name, it gives you some extra pages at the end when it creates the PDF. Is there anyway to fix that? I.e can you add to the code a selection?

Gene Klein
 
Upvote 0

Forum statistics

Threads
1,216,810
Messages
6,132,834
Members
449,761
Latest member
AUSSW

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