dcsimg
Results 1 to 6 of 6

Thread: How to convert word to PDF in VB6.0

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    3

    How to convert word to PDF in VB6.0

    I have tried following code. But it gives error "Invalid procedure call or argument". Any help would be appreciated.Thank you

    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


    objDoc.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:=False, _
    BitmapMissingFonts:=True, UseISO19005_1:=True


    objDoc.PrintOut FileName = "C:\Report.pdf"
    Set objDoc = Nothing

    .visible = True

    .WindowState = wdWindowStateMaximize

    End With
    Set WordApp = Nothing

  2. #2
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    366

    Re: How to convert word to PDF in VB6.0

    What OS are you using, and what Office library version reference you have selected? Press F2, and from the dropdown that shows "<All libraries>", select "Office", then at the bottom of the window, select what you see, then copy & paste. Example:

    Library Office
    C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE12\MSO.DLL
    Microsoft Office 12.0 Object Library


    Also, does the code compiles? It seems that you are mixing early and late bound, which is fine, as long as the code compiles.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    3

    Re: How to convert word to PDF in VB6.0

    It works.....Thank you very much. I just added reference of Microsoft Office 12.0 Object Library

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    3

    Re: How to convert word to PDF in VB6.0

    Instead of adding through project, the reference of Microsoft Office 12.0 Object Library, Is there any other way to add reference? like using "ShellExecute" function or any other API call?
    Last edited by dip_j; May 21st, 2019 at 06:19 AM.

  5. #5
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    366

    Re: How to convert word to PDF in VB6.0

    Quote Originally Posted by dip_j View Post
    Instead of adding through project, the reference of Microsoft Office 12.0 Object Library, Is there any other way to add reference? like using "ShellExecute" function or any other option.
    No. The code wasn't working because Enums such as "wdExportFormatPDF" aren't declared, and since you don't have "Option Explicit"; these Enum values are treated as 0, so that's why you get that error. I strongly recommend using Option Explicit to avoid similar problems in the future. Also, in VB6 IDE, go to Tools->Options, then enable "Require variable declaration", so VB auto inserts Option Explicit at the beginning of new files, but you have to add it to existing files manually.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,298

    Re: How to convert word to PDF in VB6.0

    if you do not want to use a reference to Word you can manage without if you set all the word constants that are currently in valid

    if you put option explicit at the top of the code module, any invalid constants will cause an error, as un-declared variables, as will any variables that are not dimensioned

    also you should remove the line
    Set objDoc = CreateObject("Word.Document")
    as it likely leaves and instance of word floating around, not being used or disposed
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width