![]() |
Active Topics Memberlist Search |
| |
| Programming | |
| Topic: Create PDF using VBA (Word, Excel, etc.) | |
| Author | Message | |
|
ziplock
Newbie
Joined: 28 Jul 04 Posts: 1 |
![]() 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() Any assistance would be greatly appreciated. Thank you. |
||
IP Logged |
||
|
Michel_K17
Moderator Group
Forum Administrator Joined: 25 Jan 03 Posts: 1160 |
![]() Posted: 31 Jul 04 at 1:55PM |
|
IP Logged |
||
|
ShowMasters
Newbie
Joined: 11 Jul 06 Location: United States Posts: 2 |
![]() 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 Logged |
||
|
ShowMasters
Newbie
Joined: 11 Jul 06 Location: United States Posts: 2 |
![]() Posted: 11 Jul 06 at 6:45PM |
|
|
BTW... you have to set Distiller as your default printer.
|
||
IP Logged |
||
|
dansam
Newbie
Joined: 23 Dec 06 Posts: 1 |
![]() Posted: 23 Dec 06 at 11:47AM |
|
|
how to create a txt file using a macro in excel?
can i make it copy protected?
|
||
IP Logged |
||
|
Michel_K17
Moderator Group
Forum Administrator Joined: 25 Jan 03 Posts: 1160 |
![]() 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 Logged |
||
|
jaywang66
Newbie
Joined: 16 Apr 09 Location: United States Posts: 1 |
![]() 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 Logged |
||
|
Michel_K17
Moderator Group
Forum Administrator Joined: 25 Jan 03 Posts: 1160 |
![]() 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 Logged |
||
|
DeanB
Newbie
Joined: 25 Sep 09 Location: United States Posts: 1 |
![]() 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:
|
||
IP Logged |
||
|
fmxds12
Newbie
Joined: 09 Mar 10 Posts: 1 |
![]() 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 Logged |
||
|
Pepito
Newbie
Joined: 07 Sep 10 Posts: 1 |
![]() 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 Logged |
||
|
Michel_K17
Moderator Group
Forum Administrator Joined: 25 Jan 03 Posts: 1160 |
![]() 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 Logged |
||
|
||
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 |
|