Solved

vb - Create .pdf from .doc

Posted on 2009-04-01
7
2,437 Views
Last Modified: 2013-11-13
Hello,

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

0
Comment
Question by:Rick
  • 3
  • 2
  • 2
7 Comments
 
LVL 13

Expert Comment

by:game-master
Comment Utility

good morning!

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


game-master
0
 
LVL 13

Author Comment

by:Rick
Comment Utility
Could you give me the vb6 code?
That would help a lot.
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
could it just be setting the background property?

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

try
msDoc.PrintOut(false, , , pdfDoc)

hope this helps a bit
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 13

Expert Comment

by:game-master
Comment Utility
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
        .cClearCache
        'readyState = False
       
    End With
   
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If readyState = False Then
        DoEvents
        myPDFCreator.cClose
    End If
    DoEvents
    myPDFCreator.cClose
    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 (*.*)|*.*"
            .ShowOpen
        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
                .cClearCache
                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
                    DoEvents
                Loop
                .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...

game-master
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
that's interesting game-master, you do all the printing outside of word

that may be even more stable too
0
 
LVL 13

Accepted Solution

by:
Rick earned 0 total points
Comment Utility
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 groupes.codes-sources.com (http://groupes.codes-sources.com/article-pdfcreator-385514.aspx).


Thank you,
Rick


0
 
LVL 13

Expert Comment

by:game-master
Comment Utility


im glad i could give u some idea...


game-master
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now