VBA refine code .PrintOut to define pdf Filename and current location

cnhtractor

New Member
Joined
Jul 17, 2014
Messages
5
I have a macro that selects worksheets to be printed from worksheets that carry a value in cell A1.

I am trying to adjust the code so that it will print the pdf file with a predetermined name, eg "Output.pdf" and into the folder that the excel file is currently saved

I do not have the VBA skills to do it - I have been trying to find code in various forums, without luck.

My code currently is:


Rich (BB code):
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Sub Print_All_Worksheets_With_Value_In_A1()
Dim Sh As Worksheet
Dim Arr() As String
Dim N As Integer
N = 0 
Application.ActivePrinter = "Adobe PDF on Ne07:"

For Each Sh In ActiveWorkbook.Worksheets    
If Sh.Visible = xlSheetVisible And Sh.Range("A1").Value <> "" Then
        N = N + 1
        ReDim Preserve Arr(1 To N)
        Arr(N) = Sh.Name
    End If
Next
With ActiveWorkbook
      .Worksheets(Arr).PrintOut
End With
End Sub



</code>Any help with refining the following area in particular will be greatly appreciated

Rich (BB code):
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">With ActiveWorkbook
      .Worksheets(Arr).PrintOut
</code>
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I have now resolved my issues here with the following code:

Code:
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Sub Print_All_Worksheets_With_Value_In_A1()
    Dim Sh As Worksheet
    Dim Arr() As String
    Dim N As Integer
    N = 0
     Application.ActivePrinter = "Adobe PDF on Ne09:"
    
    For Each Sh In ActiveWorkbook.Worksheets
        If Sh.Visible = xlSheetVisible And Sh.Range("A1").Value <> "" Then
            N = N + 1
            ReDim Preserve Arr(1 To N)
            Arr(N) = Sh.Name
        End If
    Next
    
    Dim path As String


'Capture the path of the current workbook
path = ActiveWorkbook.path & "\"


'The copy method will create a NEW workbook with these sheets
ActiveWorkbook.Worksheets(Arr).Copy


'The NEW workbook is now "Active", so use ActiveWorkbook and exportAsFixedFormat
ActiveWorkbook.ExportAsFixedFormat xlTypePDF, path & Range("sheet1!a2").Value


'Closes the temporary workbook without warning
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
    
    
End Sub
</code>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,445
Messages
6,130,685
Members
449,585
Latest member
Nattarinee

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