Link to home
Start Free TrialLog in
Avatar of LSpiker
LSpiker

asked on

How do I save a word doc to pdf from access using vb?

This I'm sure isn't hard but I can't find any documentation on it.  I'm using Access 2007, Word 2007.  They are both set up to write to pdf.  I have data dumping into Word but now I must save the word document as a pdf.  All of this under one buttion.  Everything is working fine except how to covert my finished Word doc to pdf.  Any Help?
Avatar of hello_everybody
hello_everybody
Flag of South Africa image

There is no code to convert a Word document to a PDF file. You have to install 3rd party software, set a refrence to its library and call its functions. If you do web search you  should come up with many software you can use.
Try this: http://www.verypdf.com/artprint/word-ole-visual-basic-sdk-sample.htm
Or this: http://www.print-driver.com/sdk/examples/vb6/index.html.
There are many more just search for a Pdf SDK.
Avatar of LSpiker
LSpiker

ASKER

Even if that option exists on Word?  I can press a button and create a pdf from word.  Is there a reason I can't script that same command to Word?
Avatar of Jeffrey Coachman
You can install the PDF add in for Office 2007.
http://www.microsoft.com/downloads/details.aspx?FamilyID=f1fc413c-6d89-4f15-991b-63b07ba5f2e5&displaylang=en

Then you have an option under the Office button to Save as a PDF.

;-)

JeffCoachman
untitled.JPG
ooops, sorry LSpiker I mis-read your post.

Basically you have to tack some code on to open the Word Doc in the background and save the file as a PDF

Something roughly like this "Air Code:

Dim objWord As Object
 
Set objWord = CreateObject("word.application")
objWord.Documents.Open FileName:="C:\YourFolder\WordPDF.docx"

objWord.Visible = True
objWord.ExportAsFixedFormat OutputFileName:="C:\YourFolder\PDF22.pdf", ExportFormat:=wdExportFormatPDF

objWord.Quit.wdSaveChanges
Set objWord = Nothing


The above code probably wont work exactly.
Please click the "Request Attention" Button/Link and ask that the Word Zone be added to this question.
Perhaps a Word Expert can flesh this out for you.

;-)

JeffCoachman
Convert Word to PDF using VBA

Word 2007 has a new method - Document.ExportAsFixedFormat, which saves the document as PDF or XPS format.
The following code will save the current document as PDF in the same path
 

Sub Convert_2_PDF()
 
 ActiveDocument.ExportAsFixedFormat OutputFileName:=Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 5) & "\" & ActiveDocument.Name & ".pdf", ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:=True, UseISO19005_1:=False
 
End Sub


See: http://msdn.microsoft.com/en-us/library/bb256835.aspx
ASKER CERTIFIED SOLUTION
Avatar of hello_everybody
hello_everybody
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LSpiker

ASKER

Hello_Everybody has the type of solution I'm looking for but I'm not getting it to work properly.  
Whats the problem?
Avatar of LSpiker

ASKER

Hello everybody:  Sorry for the time lapse.  I've been bounced around a few other projects and finally got back to this one.  OK your last solution on the msdn library looks like exactly what I'm looking for.  
I am having a little trouble implementing it however.    Step one I have we are good with referencing. Step two which imports the Microsoft.Office.Interop.Word peice I'm not getting.  Where does this live in my code?  or is this an external piece?
Avatar of LSpiker

ASKER

Also on the code in your heading called "Runtime err 429" "Convert Word to PDF using VBA" when I run this from Access I get an error that states "ActiveX componenet can't create object.   The following is what I have:

       ActiveDocument.ExportAsFixedFormat "C:\Program Files\MCSGAPPS\StrategicReviewSLUG.pdf",    wdExportFormatPDF, False, wdExportOptimizeForPrint, wdExportAllDocument, wdExportDocumentContent, True, True, wdExportCreateNoBookmarks, True, True, False
 
Avatar of LSpiker

ASKER

I have also tried this:  and got "Runtime Err 438 Object doesn't support this property or method"

Dim objWord As Object
 
Set objWord = CreateObject("word.application")
objWord.Documents.Open FileName:="C:\Program Files\MCSGAPPS\StrategicReviewSLUG.docx"
objWord.Visible = True
ocuments and Settings\fhi9283\Desktop\Yippie.pdf", ExportFormat:=wdExportFormatPDF
objWord.ExportAsFixedFormat OutputFileName:="C:\Documents and Settings\fhi9283Desktop\Yippie.pdf", ExportFormat:=wdExportFormatPDF  
objWord.Quit.wdSaveChanges
Set objWord = Nothing
Avatar of LSpiker

ASKER

SOLUTION!!!!!!!!!!!!!!!!!!  With the information recieved I was able to track down/ work and redevelop.  this is for Future users.....  

Dim objWord As Object
Dim frmt

Set objWord = CreateObject("word.application")
objWord.Documents.Open Filename:="C:\XYZ.docx"

  objWord.ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "C:\XYZ\Yippie.pdf", ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
        wdExportAllDocument, from:=1, To:=1, Item:=wdExportDocumentContent, _
        IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
        wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
        True, UseISO19005_1:=False
   
objWord.Quit
Set objWord = Nothing
Avatar of LSpiker

ASKER

Very good information and with the info from other folks and research and trial and error I found the entire solution and posted it.  Thank you specialists, I wouldn't have been able to finish it on my own.