Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
Programming
 EXP Systems Forum : PDF reDirect : Programming
Topic: Create PDF using VBA (Word, Excel, etc.) Post Reply Post New Topic
Author Message
ziplock
Newbie
Newbie


Joined: 28 Jul 04
Posts: 1
Quote ziplock Replybullet Topic: Create PDF using VBA (Word, Excel, etc.)
    Posted: 28 Jul 04 at 11:57AM

This is a really sweet product you have here, my question is this; can the creation process be automated with MS Office Visual Basic? 

I'm wanting to create a Excel macro that will create a PDF file using the SelectedSheet.Name and save it in a pre-selected folder without continously having to select the folder and type in the name of the file.

I found a similar PDF creation utility (FreeDist) which provided an answer that ought to have solved this issue, but VB does not recognize the naming solution (highlighted in red below).

Sub Excel_FreeDist_PDF()
     'Stephan van der Palen 07-11-2003
     'Macro to print directly to PDF from MS-Excel in combination with FreeDist
     Dim NewFileName As String
     Dim retval
     Dim FreeDistFolder As String
     Dim FreeDistWatchedFolder As String
     Dim FreeDistWatchedFolderDrive As String
     'The folder where FreeDist.exe is placed
     FreeDistFolder = "C:\My Documents\"
     'The watched folder of FreeDist
     FreeDistWatchedFolder = "C:\My Documents\FreeDist-Watch\"
     'Make FreeDists Watched Folder the current folder
     ChDir FreeDistWatchedFolder
     'Determine the name of the printed document
     NewFileName = ActiveWorkbook.Name & "_" & ActiveSheet.Name & ".prn"
     'Print "to file" to the current active printer
     'Make sure it's got good postscriptdrivers attached!!!!!!

     ActiveWindow.SelectedSheets.PrintOut Copies:=1, PrintToFile:=True, _
     Collate:=True, prttofilename:=NewFileName
     'Startup FreeDist with the newly created postscriptfile
     retval = Shell(FreeDistFolder & "freedist.exe " & FreeDistWatchedFolder & NewFileName)
End Sub

Any assistance would be greatly appreciated.  Thank you.

IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1160
Quote Michel_K17 Replybullet Posted: 31 Jul 04 at 1:55PM

I have written a small Visual Basic class and some Sample Code to show how to create a PDF file from Microsoft Office VBA.


You can download the following sample code as follows:





Edited by Michel_K17
IP IP Logged
ShowMasters
Newbie
Newbie
Avatar

Joined: 11 Jul 06
Location: United States
Posts: 2
Quote ShowMasters Replybullet Posted: 11 Jul 06 at 6:27PM
Try this... it can be fussy, but it works. I'm presently looking for a more robust code:

    Dim YourFileName
     YourFileName = Whatever you are wanting to name your file

C:\Document Bin\in\ is the "Watched Folder" that needs to be set in

    Dim PSFileName As String, PDFFileName As String, DistillerCall As String
    Dim ReturnValue As Variant
    PSFileName = "C:\Document Bin\in\" & YourFileName & ".PS"
    PDFFileName = "C:\Document Bin\out\" & YourFileName & ".PDF"
    If Dir(PSFileName) <> "" Then Kill (PSFileName)
    If Dir(PDFFileName) <> "" Then Kill (PDFFileName)
    SendKeys PSFileName & "{ENTER}", False
    ActiveSheet.PrintOut , PrintToFile:=True
    PSFileName = Chr(34) & PSFileName & Chr(34)
    PDFFileName = Chr(34) & PDFFileName & Chr(34)
    DistillerCall = "C:\Program Files\Adobe\Acrobat 5.0\Distillr\Acrodist.exe"
    ReturnValue = Shell(DistillerCall, vbNormalFocus)
    If ReturnValue = 0 Then MsgBox "Creation of " & PDFFileName & "failed."
IP IP Logged
ShowMasters
Newbie
Newbie
Avatar

Joined: 11 Jul 06
Location: United States
Posts: 2
Quote ShowMasters Replybullet Posted: 11 Jul 06 at 6:45PM
BTW... you have to set Distiller as your default printer.
IP IP Logged
dansam
Newbie
Newbie


Joined: 23 Dec 06
Posts: 1
Quote dansam Replybullet Posted: 23 Dec 06 at 11:47AM
how to create a txt file using a macro in excel?
can i make it copy protected?Wink
IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1160
Quote Michel_K17 Replybullet Posted: 25 Dec 06 at 6:32PM
Hi,
 
   I am not sure I understand what you mean. PDF reDirect is for creating PDF files, not txt files. Sorry.
 
   No, txt files cannot be protected.
Michel Korwin-Szymanowski
EXP Systems LLC
IP IP Logged
jaywang66
Newbie
Newbie
Avatar

Joined: 16 Apr 09
Location: United States
Posts: 1
Quote jaywang66 Replybullet Posted: 16 Apr 09 at 12:54PM
A little while ago I was able to develop excel script to print excel page as PDF file through intensive google search.

Sub Print_PDF()
    
    'need to check Reference to Acrobat Distiller in Tools --> References
    'Define the postscript and .pdf file names.
       
    Dim PSFileName As String
    Dim PDFFileName As String
    PSFileName = "c:\VBAPrintPDF\Sample.ps"
    PDFFileName = "c:\VBAPrintPDF\Sample.pdf"

    'Print the Excel range to the postscript file
    ActiveWindow.SelectedSheets.PrintOut copies:=1, preview:=False, ActivePrinter:="Acrobat Distiller", _
    printtofile:=True, collate:=True, prtofilename:=PSFileName


    'Convert the postscript file to .pdf
    Dim myPDF As PdfDistiller
    Set myPDF = New PdfDistiller
    myPDF.FileToPDF PSFileName, PDFFileName, ""
    
End Sub

But last week I found I couldn't print out color in PDF - only black and white. Then I used several days to search on google again but didn't find directly answers. With the help of all the different info I changed activePrinter from Acrobat Distiller to Adobe PDF. Then the problem was solved - I got colorful PDF file. Here is the new code - only a tiny change:

Sub Print_PDF()
    
    'need to check Reference to Acrobat Distiller in Tools --> References
    'Define the postscript and .pdf file names.
       
    Dim PSFileName As String
    Dim PDFFileName As String
    PSFileName = "c:\VBAPrintPDF\Sample.ps"
    PDFFileName = "c:\VBAPrintPDF\Sample.pdf"

    'Print the Excel range to the postscript file
    ActiveWindow.SelectedSheets.PrintOut copies:=1, preview:=False, ActivePrinter:="Adobe PDF", _
    printtofile:=True, collate:=True, prtofilename:=PSFileName


    'Convert the postscript file to .pdf
    Dim myPDF As PdfDistiller
    Set myPDF = New PdfDistiller
    myPDF.FileToPDF PSFileName, PDFFileName, ""
    
End Sub
IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1160
Quote Michel_K17 Replybullet Posted: 16 Apr 09 at 7:35PM
Hi,

   Thanks for the tip.

   In case anyone is wondering, the code above will work if you have Adobe Acrobat installed on your computer. If you are using PDF reDirect Pro, you should use the sample code provided [here] instead.

   Cheers!

Michel Korwin-Szymanowski
EXP Systems LLC
IP IP Logged
DeanB
Newbie
Newbie
Avatar

Joined: 25 Sep 09
Location: United States
Posts: 1
Quote DeanB Replybullet Posted: 25 Sep 09 at 11:48AM
Greetings all,
 
I was able to use this command to export an Excel file as a pdf:
 
            ActiveWorkbook.ExportAsFixedFormat xlTypePDF, buildSaveDest

 
where buildSaveDest is a variable containnig both filename and path.
 
I actually use the Sheets().Copy command to export a sheet as excel, modify is as needed, then after saving to pdf, I close it without saving:
 
            ActiveWorkbook.Close SaveChanges:=False

 
HOWEVER, as I am working in SharePoint, I am having great difficulty finding code to checkin/checkout the pdf file in sharepoint.  So I have to do this manually for each file after the export.

Any help with the sharepoint issue would be great.
 
Please note that while I did have full Adobe installed, there is a free patch to allow handling of pdf files on the microsoft site.  The F1 help notes thats:
 
  Note
An error will occur if the PDF add-in is not currently installed.
But the add-in is readily available and free.
IP IP Logged
fmxds12
Newbie
Newbie


Joined: 09 Mar 10
Posts: 1
Quote fmxds12 Replybullet Posted: 09 Mar 10 at 8:19AM
try this plugin from MS, by using this, you should be able to Export any Office document to a PDF file.

http://www.microsoft.com/downloads/details.aspx?FamilyId=F1FC413C-6D89-4F15-991B-63B07BA5F2E5&displaylang=en



thanks - Lina
custom application developers | professional web services | remote dba consultants
IP IP Logged
Pepito
Newbie
Newbie


Joined: 07 Sep 10
Posts: 1
Quote Pepito Replybullet Posted: 07 Sep 10 at 5:59AM
hi
 
i am new to the forum as wellas into vba.
 
i have tried to use your macro below but it doesnt work.
 
is possible to get a code where i can just copy paste it?
IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1160
Quote Michel_K17 Replybullet Posted: 07 Sep 10 at 5:45PM
Hi Pepito,

   That will be tricky, but here is goes:

   1. Download just the ActiveX component from [here].

   2. Install it.

   3. Open an MS Word or an MS Excel document

   4. Open the VBA editor.

   5. Copy/Paste the following code. It won't do much, but it will confirm that the ActiveX was installed correctly.

Sub TestPDF()
    Dim myPDF As Object
    Dim UserSelection As String
    Set myPDF = New PDF_reDirect_v25002.Batch_RC_AXD
    UserSelection = myPDF.Utility_ShowAs_Dialog("My Title", "C:\", "MyFilename.pdf", 0)
    Debug.Print "FileLocation chosen is: " & UserSelection
    Set myPDF = Nothing
End Sub


   6. Look at the full code for more examples of what you can do like creating a PDF file using VBA.

I hope this helps.

Michel Korwin-Szymanowski
EXP Systems LLC
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum