Creating PDF File from Excel using VBA Script

Ask a Question related to Adobe Acrobat Windows, Design and Development.

  1. #1

    Default Creating PDF File from Excel using VBA Script

    I have Excel 2000 and Adobe Acrobat v6.0 Standard.

    I'm trying to use a Visual Basic for Applications (VBA) macro to output reports to PDF files.

    As you probably know, creating a PDF file from Excel requires "printing" to an "Adobe PDF" selection as though it were a printer. A dialog box named "Save PDF File As" appears. The default PDF file name will be same as the source Excel file.

    Here's where my VBA skills get stuck. It seems that VBA won't work within the "Save PDF File As" box. But I need VBA to create the PDF file with a pre-determined name, not the default, which changes with each report that is generated.

    Below is Excel's "recording" of the keystrokes where I output a report to PDF and named the file "XYZtest.pdf". Notice that Excel VBA failed to record the XYZtest.pdf file name into the script, nor account for any file name at all.

    Sub Macro1()
    Application.ActivePrinter = "Adobe PDF on Ne01:"
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
    "Adobe PDF on Ne01:", Collate:=True
    End Sub

    So how can I get Excel VBA to work with Adobe Acrobat to name the PDF file what I want?

    Thank you!
    Bob Barber
    [email]eatdrinksleepdream@sbcglobal.net[/email]
    Bob_Barber@adobeforums.com Guest

  2. Similar Questions and Discussions

    1. Creating Forms from Excel
      I'm kinda new to Acrobat - I'm trying to create some pdf forms - namely from Excel 2004 - in the forms Wizard - it says I can create forms from...
    2. Creating Excel worksheets
      Hi, I need to create dynamic excel worksheets using coldfusion. Using cfcontent tag, we can create single worksheet. But my requirement is, i...
    3. Creating a tab delimited file for excel import
      I am sending CF email where the contents is a tab delimited content that can be pasted into a plain text file - then imported into excel.... When I...
    4. Place Excel into table with ability to link when native Excel file is updated.
      Is it possible to place an Excel file into InDesign and create a link so that any changes made later to the Excel file can be updated in InDesign? ...
    5. Creating Excel file + inserting values
      Hello, I am trying to archieve the following: - copy an excel file present on the server - insert values into named ranges of the copy I am...
  3. #2

    Default Re: Creating PDF File from Excel using VBA Script

    [email]Bob_Barber@adobeforums.com[/email] wrote in message news:<2cd11e49.-1@webx.la2eafNXanI>...
    > I have Excel 2000 and Adobe Acrobat v6.0 Standard.
    >
    > I'm trying to use a Visual Basic for Applications (VBA) macro to output reports to PDF files.
    >
    > As you probably know, creating a PDF file from Excel requires "printing" to an "Adobe PDF" selection as though it were a printer. A dialog box named "Save PDF File As" appears. The default PDF file name will be same as the source Excel file.
    >
    > Here's where my VBA skills get stuck. It seems that VBA won't work within the "Save PDF File As" box. But I need VBA to create the PDF file with a pre-determined name, not the default, which changes with each report that is generated.
    >
    > Below is Excel's "recording" of the keystrokes where I output a report to PDF and named the file "XYZtest.pdf". Notice that Excel VBA failed to record the XYZtest.pdf file name into the script, nor account for any file name at all.
    >
    > Sub Macro1()
    > Application.ActivePrinter = "Adobe PDF on Ne01:"
    > ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
    > "Adobe PDF on Ne01:", Collate:=True
    > End Sub
    >
    > So how can I get Excel VBA to work with Adobe Acrobat to name the PDF file what I want?
    >
    > Thank you!
    > Bob Barber
    > [email]eatdrinksleepdream@sbcglobal.net[/email]
    Bob:

    You need to change the registry settings for Adobe. Here is a bit of
    VBA that I use to do just that:

    'declarations:
    Global Const dhcRegSz = 1


    Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
    "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
    ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _
    Long) As Long

    Public Declare Function RegSetValueEx _
    Lib "advapi32.dll" Alias "RegSetValueExA" _
    (ByVal hKey As Long, ByVal lpValueName As String, _
    ByVal dwReserved As Long, ByVal dwType As Long, _
    lpData As Any, ByVal cbData As Long) As Long

    Public Declare Function RegCloseKey Lib "advapi32.dll" _
    (ByVal hKey As Long) As Long

    Sub Printme()
    dim strDefaultPrinter as string, strOutFile as string
    strDefaultPrinter = Application.ActivePrinter
    Application.ActivePrinter = "Adobe PDF on Ne01:"
    'Open key
    lngRegResult = RegOpenKeyEx(dhcHKeyCurrentUser,
    "Software\Adobe\Acrobat PDFWriter", 0&, dhcKeyAllAccess, lngResult)

    'Turn view result off
    lngRegResult = RegSetValueEx(lngResult, "bExecViewer", 0&,
    dhcRegSz, ByVal "0", 1)
    lngRegResult = RegSetValueEx(lngResult, "bDocInfo", 0&, dhcRegSz,
    ByVal "0", 1)

    'Set Output File name and path
    strOutFile = "c:\Temp\PDFTemp\mypdf.pdf"

    lngRegResult = RegSetValueEx(lngResult, "PDFFileName", 0&,
    dhcRegSz, ByVal strOutFile, Len(strOutFile))
    'close key
    lngRegResult = RegCloseKey(lngResult)

    ActiveWindow.SelectedSheets.PrintOut
    Application.ActivePrinter = strDefaultPrinter
    End sub
    Chuck Reed Guest

  4. #3

    Default Re: Creating PDF File from Excel using VBA Script

    I work with excel files quite often and once my nephew asked me about one excel problem. I couldn't help him and started looking for the solution. And luckily found out it yesterday evening, he thanked me a lot and in my view in this situation the tool will be helpful
    Last edited by Ravish; September 4th at 03:34 PM. Reason: removed spam link
    zlatan24 is offline Junior Member
    Join Date
    Sep 2010
    Location
    New York
    Posts
    6

  5. #4

    Default Re: Creating PDF File from Excel using VBA Script

    Hi,

    It seems the above codes don't work for Adobe Acrobat 7 Pro and Excel 2003 :(. Can anyone please advice me which part of the codes should be changed to suit Adobe 7?

    Thanks a lot!
    Unregistered Guest

  6. #5

    Default Creating PDF File from Excel using VBA Script

    I just follow the code written here, and my "Device and Printer" is disappear
    does any one can help

    Thanks
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139