Convert to PDF

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Convert to PDF

Post by MSingh »

Hi Hans,

I want to like to print the word document to PDF.
Actually, what I have are 2 Word documents:
1. is a mail-merged document (for large volumes)
2. Word document prepared using doc props (for small quantities)

if I could output (1) and (2) to PDF it would be ideal.

Kind Regards,
Mohamed

User avatar
HansV
Administrator
Posts: 78548
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Convert to PDF

Post by HansV »

I have split the above post from the thread print txt file in dir, because it's a new question.

Mohamed, do you want to do this in VBA? And from which application? From Word itself, or from Access, or ...?
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: Convert to PDF

Post by MSingh »

Thanks Hans,

I should have been clearer:
I would like to do this in VBA from MS Access (2007).

Basically, I have strSaveNamePath (word document) which I would like to convert to pdf.

Kind Regards,
Mohamed

User avatar
HansV
Administrator
Posts: 78548
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Convert to PDF

Post by HansV »

Copy the following procedure to a VBA module:

Code: Select all

Sub ConvertToPDF(strFile As String)
    Dim objWrd As Object
    Dim objDoc As Object
    Dim blnStart As Boolean
    Dim strPDFPath As String
    Dim lngPos As Long
    On Error Resume Next
    Set objWrd = GetObject(Class:="Word.Application")
    If objWrd Is Nothing Then
        Set objWrd = CreateObject(Class:="Word.Application")
        If objWrd Is Nothing Then
            MsgBox "Cannot start Word. Exiting...", vbCritical
            Exit Sub
        End If
        blnStart = True
    End If
    On Error GoTo ErrHandler
    Set objDoc = objWrd.Documents.Open(FileName:=strFile, AddToRecentFiles:=False)
    lngPos = InStrRev(strFile, ".")
    strPDFPath = Left(strFile, lngPos) & "pdf"
    objDoc.ExportAsFixedFormat OutputFileName:=strPDFPath, _
        ExportFormat:=17, OpenAfterExport:=False, OptimizeFor:=0
ExitHandler:
    On Error Resume Next
    objDoc.Close SaveChanges:=False
    If blnStart Then
        objWrd.Quit SaveChanges:=False
    End If
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbCritical
    Resume ExitHandler
End Sub
Call it like this:

Code: Select all

    ConvertToPDF strSaveNamePath
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: Convert to PDF

Post by MSingh »

Too Good Hans :clapping:

Many Thanks,

Kind Regards,
Mohamed