Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


vb - Create .pdf from .doc

Posted on 2009-04-01
Medium Priority
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

705 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