vb - Create .pdf from .doc

Posted on 2009-04-01
Last Modified: 2013-11-13

How do I create a .pdf file from a Word .doc?
I'm using PDFCreator and VB 08 Pro.

The code below creates the .pdf file, but when I try to open it, I get the following error message:

"Adobe could not open 'tt.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)...

Thank you for your help.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim msword As word.application
        Dim msDoc As Word.Document
        Dim wordDoc As String = "C:\tt.doc"
        Dim pdfDoc As String = "C:\tt.pdf"
        msword = CreateObject("Word.Application")
        msDoc = msword.Documents.Open(wordDoc)
        msword.Application.Visible = False
        msword.ActivePrinter = "PDFCreator"
        msDoc.PrintOut(, , , pdfDoc)
        CType(msword, Word._Application).Quit()
        msDoc = Nothing
        msword = Nothing
    End Sub

Open in new window

Question by:Rick
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 13

Expert Comment

ID: 24055871

good morning!

i have used PDFCreator to create a pdf file, unfortunately i used vb6 not .net.
and it works correctly...

LVL 13

Author Comment

ID: 24056373
Could you give me the vb6 code?
That would help a lot.
LVL 44

Expert Comment

ID: 24057310
could it just be setting the background property?

now word is closed before the print is finished
msDoc.PrintOut(, , , pdfDoc)

msDoc.PrintOut(false, , , pdfDoc)

hope this helps a bit
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

LVL 13

Expert Comment

ID: 24058028
Option Explicit

Private WithEvents myPDFCreator As PDFCreator.clsPDFCreator
Private pdfError As PDFCreator.clsPDFCreatorError
Private pdfOpt As PDFCreator.clsPDFCreatorOptions
Private Const maxTime As Long = 20
Private readyState As Boolean

Private Sub commands_Click(Index As Integer)
    Select Case Index
        Case 0
            PrintIt 0
    End Select
End Sub

Private Sub Form_Load()
    Set pdfError = New PDFCreator.clsPDFCreatorError
    Set myPDFCreator = New PDFCreator.clsPDFCreator
    'Set pdfOpt = New PDFCreator.clsPDFCreatorOptions
    With myPDFCreator
        '.cVisible = True
        If .cStart("/NoProcessingAtStartup") = False Then
            If .cStart("/NoProcessingAtStartup", True) = False Then
                commands(0).Enabled = False
                Exit Sub
            End If
          '.cVisible = True
        End If
        ' Get the options
        Set pdfOpt = .cOptions
        'readyState = False
    End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If readyState = False Then
    End If
    Set myPDFCreator = Nothing
    Set pdfError = Nothing
    Set pdfOpt = Nothing
End Sub
Private Sub PrintIt(ByVal Filetyp As Long)
        Dim fname As String, fi As String, DefaultPrinter As String
        Dim opt As New PDFCreator.clsPDFCreatorOptions
        With Me.CommonDialog1
            '.MultiSelect = False
            '.CheckFileExists = True
            '.CheckPathExists = True
            .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
        End With
        If CommonDialog1.FileName <> "" Then
            fi = (CommonDialog1.FileName)
'            If CommonDialog1.Name <> "" Then
'                If InStr(CommonDialog1.Name, ".", VbCompareMethod) > 1 Then
'                    fname = Mid(fi.Name, 1, InStr(CommonDialog1.Name, ".", CompareMethod.Text) - 1)
'                Else
'                    fname = CommonDialog1.Name
'                End If
'            End If
            If Not myPDFCreator.cIsPrintable(fi) Then
                MsgBox "File is not printable!", vbExclamation
                Exit Sub
            End If
            Set opt = myPDFCreator.cOptions
            With opt
                .UseAutosave = 1
                .UseAutosaveDirectory = 1
                .AutosaveDirectory = "C:\" 'fi.DirectoryName
                .AutosaveFormat = Filetyp
                If Filetyp = 5 Then
                    .BitmapResolution = 72
                End If
                .AutosaveFilename = "alvin"
            End With
            With myPDFCreator
                DefaultPrinter = .cDefaultPrinter
                .cDefaultPrinter = "PDFCreator"
                .cPrintFile (Me.CommonDialog1.FileName)
                readyState = False
                .cPrinterStop = False
            End With
            Set myPDFCreator.cOptions = opt
            With Timer1
                .Interval = maxTime * 1000
                .Enabled = True
                Do While Not readyState And .Enabled
                .Enabled = False
            End With
            If Not readyState Then
                MsgBox "Creating printer test page as pdf." & vbCrLf & vbCrLf & _
                 "An error is occured: Time is up!", vbExclamation
            End If
            myPDFCreator.cPrinterStop = True
            myPDFCreator.cDefaultPrinter = DefaultPrinter
        End If
        Set opt = Nothing
    End Sub

Private Sub mypdfcreator_eReady()
    myPDFCreator.cPrinterStop = True
    readyState = True
End Sub

Private Sub mypdfcreator_eError()
    Set pdfError = myPDFCreator.cError
End Sub

Private Sub Timer1_Timer()
    Timer1.Enabled = False
End Sub

i hope this code give u idea...

LVL 44

Expert Comment

ID: 24058095
that's interesting game-master, you do all the printing outside of word

that may be even more stable too
LVL 13

Accepted Solution

Rick earned 0 total points
ID: 24088450
game-master and bruintje,

Thank you both for your input. I couldn't translate the vb06 code into vb08... bruintje's suggestion didn't help me solve the problem.

I ended up finding my own solution through (

Thank you,

LVL 13

Expert Comment

ID: 24093026

im glad i could give u some idea...


Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question