Solved

vb - Create .pdf from .doc

Posted on 2009-04-01
7
2,446 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
ID: 24055871

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
ID: 24056373
Could you give me the vb6 code?
That would help a lot.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 24057310
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:game-master
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
        .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
ID: 24058095
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
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 groupes.codes-sources.com (http://groupes.codes-sources.com/article-pdfcreator-385514.aspx).


Thank you,
Rick


0
 
LVL 13

Expert Comment

by:game-master
ID: 24093026


im glad i could give u some idea...


game-master
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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

18 Experts available now in Live!

Get 1:1 Help Now