Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have a code to convert word document in PDF in VB 6.0 But when i try to convert it gives error invalid procedure call

What I have tried:

VB
Dim objDoc As Object                                  
    Dim rngRange As Object                               
    Dim WordApp As Object                      
    
    Set objDoc = CreateObject("Word.Document")
    Set WordApp = CreateObject("Word.Application")
    
 With WordApp
     Set objDoc = .Documents.Open(g_strPath & "\Documents\Report.doc")
        objDoc.activate
    

        WordApp.ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        App.Path & "\Documents\Report.pdf", ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
        wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
        IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
        wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
        True, UseISO19005_1:=False
        
        objDoc.PrintOut FileName = "C:\MyReport.pdf"
        Set objDoc = Nothing
        Set rngRange = Nothing
        
        .visible = True
end with
Posted
Updated 19-Dec-19 13:11pm
v2
Comments
Richard MacCutchan 18-May-19 9:43am
   
Where does the error occur?
djs83 20-May-19 7:14am
   
When I call WordApp.ActiveDocument.ExportAsFixedFormat OutputFileName........
For this call i get error "object doesn't support this property or call"
Richard MacCutchan 20-May-19 7:53am
   
Check the documentation to see which part is not valid. It may be that the ExportAsFixedFormat method does not exist, or one of the parameters is incorrect.
DerekT-P 18-May-19 13:39pm
   
You're specifying both range and from / to parameters; but from / to are only relevant if the selected range is wdExportRange. At the end of the routine you're calling PrintOut with a filename parameter. You're not using a named parameter and, if you were, the parameter is named OutputFileName not FileName. The filename is only of any relevance if you set "PrintToFile" true, which you're not; so the final step just prints the document to the default printer. Now, if you have setup the default printer as PrintToPdf then that may be all you need to do, i.e. no need to attempt the previous export.
objDoc.PrintOut OutputFileName:="C:\myReport.pdf",PrintToFile:=True
djs83 20-May-19 7:17am
   
@DerekTP123
I have used some bookmarks in my word document to show data. And I want to convert Whole word doc to PDF. Should I remove to/from parameters
DerekT-P 20-May-19 9:33am
   
You've explicitly specified you want to export the whole document. The MS documentation doesn't explicitly state it, but since To:= and From:= are only relevant when wdExportRange is used, any other time they must be ignored. For clarity if nothing else, I'd remove the to/from parameters. And you're not "converting" the document; you're exporting a copy in a different format (or logically "printing" the document). But anyway see Rick's solution below... was assuming you had this installed since you're referencing the wdExport.... constants, but maybe you'd just defined them yourself?

You have to install the microsoft office add on for export, see example 2 here:
visual-basic-6 - convert Word file to Pdf file in vb6.0 [SOLVED] | DaniWeb[^]
   
Thanks for the example. It worked for me in VB6. I added a Call statement and here are the values I used...

Dim lngPageCount As Long
lngPageCount = 1
Call objDoc.ExportAsFixedFormat(strReportName & ".pdf", wdExportFormatPDF, False, _
wdExportOptimizeForPrint, wdExportAllDocument, 1, lngPageCount, _
wdExportDocumentContent, False, True, wdExportCreateNoBookmarks, True, _
True, False)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900