Solved

vb - Create .pdf from .doc

Posted on 2009-04-01
7
2,450 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

815 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

11 Experts available now in Live!

Get 1:1 Help Now