Vbscript Print To Pdf

I have a code that prints a selected area in a worksheet to PDF and allows user to select folder and input file name. There are two things I want to do though: • Is there a way that the PDF file can create a folder on the users desktop and save the file with a file name based on specific cells in the sheet? • If multiple copies of the same sheet are saved/printed to PDF can each copy have a number eg. Hopefully this is self explanatory enough. Use the comments in the code to help understand what is happening. Pass a single cell to this function. The value of that cell will be the base file name.

VBScript example of printing HTML to PDF. How can I convert an HTML document to PDF using VBScript? Sub Print_HTML_Page. Re: Printing PDF files from vbscript ravishq Jun 17, 2009 11:47 PM (in response to tcAtlanta) One way of doing it is to use Acrobat's command line print options.

If the cell contains 'AwesomeData' then we will try and create a file in the current users desktop called AwesomeData.pdf. If that already exists then try AwesomeData2.pdf and so on. In your code you could just replace the lines filename = Application. With filename = GetFileName(Range('A1')) Function GetFileName(rngNamedCell As Range) As String Dim strSaveDirectory As String: strSaveDirectory = ' Dim strFileName As String: strFileName = ' Dim strTestPath As String: strTestPath = ' Dim strFileBaseName As String: strFileBaseName = ' Dim strFilePath As String: strFilePath = ' Dim intFileCounterIndex As Integer: intFileCounterIndex = 1 ' Get the users desktop directory. StrSaveDirectory = Environ('USERPROFILE') & ' Desktop ' Debug.Print 'Saving to: ' & strSaveDirectory ' Base file name strFileBaseName = Trim(rngNamedCell.Value) Debug.Print 'File Name will contain: ' & strFileBaseName ' Loop until we find a free file number Do If intFileCounterIndex >1 Then ' Build test path base on current counter exists. StrTestPath = strSaveDirectory & strFileBaseName & Trim(Str(intFileCounterIndex)) & '.pdf' Else ' Build test path base just on base name to see if it exists. StrTestPath = strSaveDirectory & strFileBaseName & '.pdf' End If If (Dir(strTestPath) = ') Then ' This file path does not currently exist.

StrFileName = strTestPath Else ' Increase the counter as we have not found a free file yet. IntFileCounterIndex = intFileCounterIndex + 1 End If Loop Until strFileName ' ' Found useable filename Debug.Print 'Free file name: ' & strFileName GetFileName = strFileName End Function The debug lines will help you figure out what is happening if you need to step through the code.

Remove them as you see fit. I went a little crazy with the variables but it was to make this as clear as possible. In Action My cell O1 contained the string 'FileName' without the quotes. Cimplicity Scada Manual. Used this sub to call my function and it saved a file.

Sub Testing() Dim filename As String: filename = GetFileName(Range('o1')) ActiveWorkbook.Worksheets('Sheet1').Range('A1:N24').ExportAsFixedFormat Type:=xlTypePDF, _ filename:=filename, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End Sub Where is your code located in reference to everything else? Perhaps you need to make a module if you have not already and move your existing code into there. Your If statement is flawed unless you are really comparing to the string false. If filename ' Then would usually suffice in cases like that. You have two sections of sheet code. My function does not affect those in any way.

You need to update both references of the filename depending on your needs. Also depending on where this code is being called from you might need to reference the Sheet filename = GetFileName(Sheets('Rental').Range('O1')) for example. Debug some more and let me know. – Dec 1 '14 at 5:10 •. Sorry I am still fairly new to VBA and not sure where I have gone wrong.

I am still getting a 1004 Error. I removed the IF statement and changed the string names - Worksheets('Rental').Activate filenamerental = GetFileName(Sheets('Rental').Range('O1')) With ActiveWorkbook.Worksheets('Rental').Range('A1:N24').ExportAsFixedFormat Type:=xlTypePDF, _ filename:=filenamerental, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End With – Dec 1 '14 at 22:13 •.

Attention, Internet Explorer User Announcement: Jive has discontinued support for Internet Explorer 7 and below. In order to provide the best platform for continued innovation, Jive no longer supports Internet Explorer 7. Jive will not function with this version of Internet Explorer. Please consider upgrading to a more recent version of Internet Explorer, or trying another browser such as Firefox, Safari, or Google Chrome.