none
Access 2010 VBA and Word 2010

    Question

  • Hi There

    I have a very annoying problem with Access 2010 and Word 2010, I'm trying do create a Word document from Microsoft Access - works fine.

    Then I'm trying do save this word document as a PDF document, But I keep getting a message about 'Invalid procedure call or argument', and the error replicates on three different computers, two at work and my home computer.

    My complete VBA code is here :

    Dim wrd As Object
    Set wrd = CreateObject("Word.Application")

    wrd.Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
    wrd.Selection.TypeText Text:="Some Text"
    wrd.Visible = True

    wrd.ActiveDocument.ExportAsFixedFormat OutputFileName:= _
     "C:\Draft.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

    Thanks in advance for any hints... :-)

    Thomas from Copenhagen, Denmark


    Thomas Elbek

    Tuesday, September 04, 2012 3:00 PM

Answers

  • Do you have a reference set to the Word object library?  I see that you are using late binding, which would normally free you from the need to set the reference, but then I see that you are using Word-defined constants such as wdExportFormatPDF, wdExportOptimizeForPrint, wdExportDocumentContent, and wdExportCreateNoBookmarks.  So either you have to set a reference to the Word object library, or else you have to define those constants, with their proper values, in your own code.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by Thomas E Tuesday, September 04, 2012 7:48 PM
    Tuesday, September 04, 2012 7:40 PM

All replies

  • I am not sure you can do what you are trying but even if you could why? If you need a .pdf file created simply do it from Access directly.

    Private Sub Your ControlNameHere_YourEventHere()
        Dim frm As Form
        Set frm = Screen.ActiveForm
            frm.FilterOn = True
        DoCmd.OutputTo acOutputForm, frm.Name, acFormatPDF
        frm.FilterOn = False
    End Sub

    Or for a shorter example with a report:

    Private Sub YourControlNameHere_YourEventNameHere()
        DoCmd.OutputTo acOutputReport, Me.Name, acFormatPDF
    End Sub

    HTH


    Chris Ward


    • Edited by KCDW Tuesday, September 04, 2012 7:01 PM
    Tuesday, September 04, 2012 6:57 PM
  • Thanks for your response :-)

    Well - I'm not in charge of the design, - thankfully - I was drawn into it when a colleague got stuck.

    I do also doubt it can be done, I think it might confuse Access & Word method stubs.

    This solution is using Word 2010 mail merge from Access..., very bad design.


    Thomas Elbek

    Tuesday, September 04, 2012 7:04 PM
  • Do you have a reference set to the Word object library?  I see that you are using late binding, which would normally free you from the need to set the reference, but then I see that you are using Word-defined constants such as wdExportFormatPDF, wdExportOptimizeForPrint, wdExportDocumentContent, and wdExportCreateNoBookmarks.  So either you have to set a reference to the Word object library, or else you have to define those constants, with their proper values, in your own code.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by Thomas E Tuesday, September 04, 2012 7:48 PM
    Tuesday, September 04, 2012 7:40 PM
  • OMG - now I understand it - being a C#/.Net developer I loose control when using VBA, I will change it not to use late binding, also for the intellisense part.

    Great thanks for your advice - you will be in my evening prayer.


    Thomas Elbek

    Tuesday, September 04, 2012 7:50 PM
  • Now it works fine - after adding a reference to Microsoft Word 14 Object Library and Microsoft Office 14 Object Library.

    Thomas Elbek

    Tuesday, September 04, 2012 8:05 PM
  • You're welcome.  May I suggest also that you set the VB option "Require Variable Declaration" (a check box in the VB Editor's option), and add an Option Explicit statement at the top of all code modules you have already created?  Doing so will ensure that you get a compile-time error for any undeclared variable, which would have made the cause of your current problem more obvious.

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, September 04, 2012 8:08 PM