Announcement

Collapse
No announcement yet.

Memory Access Violation when running the Word COM sample

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Memory Access Violation when running the Word COM sample

    Maybe I am missing the obvious, but when I run the PB 9.0 Word COM Sample my Win XP computer (Word 2000 installed), I get a "Memory Access Violation" when I try to save the document to disk. But if I replace the "SaveAs" method with the "Save" method, the user is prompted with a "Save File" dialog and all ends well.

    The PB 8.0 Word COM sample runs fine. Of course I see that the PB 9.0 sample was designed for and tested with a later Office Version (2003) than the version I use, but aren't the object definitions in the word.inc file kept "version independent"?

    Any help would be appreciated!

    Heinz Salomon

    Code:
    '==============================================================================
    '
    '  oWord.bas example for PowerBASIC For Windows Compiler
    '  Copyright (c) 2002-2008 PowerBASIC, Inc.
    '  All Rights Reserved.
    '
    '  Run a short demonstration of sending data to Microsoft Word.
    '  Tested with MS Word v11.00 (Office 2003)
    '
    '==============================================================================
    
    #COMPILER PBWIN 9
    #COMPILE EXE
    #DIM ALL
    
    ' oWord.inc contains the Micorosft Word Interface defintions
    ' created with the PowerBASIC COM Browser
    #INCLUDE "oWord.inc"
    
    ' Main application entry point...
    FUNCTION PBMAIN()
      LOCAL oWordApp     AS Int__Application ' Application Interface
      LOCAL oWordDoc     AS Int__Document    ' Document Interface
      LOCAL oWordSel     AS Selection        ' Selection Interface
      LOCAL sProgID_Word AS STRING
      LOCAL iDocCount    AS LONG
      LOCAL tText        AS STRING
      LOCAL vText        AS VARIANT
      LOCAL vFileFmt     AS VARIANT
    
      MSGBOX "About to Open MSWORD..."
    
      ' Open an instance of MSWORD
      oWordApp = NEWCOM $PROGID_Application
    
      ' Could MSWORD be opened? If not, terminate this app
      IF ISFALSE ISOBJECT(oWordApp) THEN
        MSGBOX "Unable to open or start MSWORD!"
        EXIT FUNCTION
      END IF
    
      ' Make MSWORD visible in for example normal show state
      oWordApp.Visible = 1
      oWordApp.WindowState = %wdWindowStateNormal
    
      ' Get the current document count from the "documents collection"
      MSGBOX "About to request the MSWORD document count..."
      iDocCount = oWordApp.Documents.Count
      MSGBOX "MSWORD has " & FORMAT$(iDocCount) & " File(s) loaded!"
    
      ' Add a document to the "documents collection", and obtain a reference
      ' to the document Interface for the new document
      MSGBOX "Adding a new document to MSWORD..."
      oWordDoc = oWordApp.Documents.Add
      IF ISFALSE ISOBJECT(oWordDoc) THEN
        MSGBOX "MSWORD was not able to start a new document." & $CR & _
                      "Please ensure MSWord and VBA are installed."
        GOTO Terminate
      END IF
    
      ' Get the current document count from the "documents collection"
      ' This should be one value higher than before
      MSGBOX "Checking the MSWORD document count again..."
      iDocCount = oWordApp.Documents.Count
      MSGBOX "MSWORD has " & FORMAT$(iDocCount) & " File(s) loaded!"
    
      ' Get the Interface to the currect selection of the document
      oWordSel = oWordApp.Selection
    
      ' Enter some text into the document at the selection position
      MSGBOX "..and placing some text at that selection point..."
      oWordSel.TypeText(UCODE$("PowerBASIC controls Microsoft Word!"))
    
      ' Set a paragraph marker (end of paragraph)
      oWordSel.TypeParagraph
    
      ' Another paragraph
      oWordSel.TypeText(UCODE$("COM client controls a COM server!"))
      oWordSel.TypeParagraph
    
      ' Save the new document to disk
      MSGBOX "About to save the document and close MSWORD..."
      vText = "Test.doc"
      vFileFmt = %wdFormatDocument
      oWordDoc.SaveAs(vText, vFileFmt)  <======= Exception
         'Memory Access Violation; Program tried to read or write an invalid
         'memory address
         'oWordDoc.Save would work
    
      MSGBOX "All done! The COM automation stuff is pretty cool!"
    
    Terminate:
      ' Close the current document and then close MSWORD completely
      oWordApp.ActiveWindow.CLOSE
      oWordApp.Quit
    
      ' Close all of the Interfaces
      oWordSel  = NOTHING
      oWordDoc  = NOTHING
      oWordApp  = NOTHING
    END FUNCTION

  • #2
    The PB 8.0 Word COM sample runs fine. Of course I see that the PB 9.0 sample was designed for and tested with a later Office Version (2003) than the version I use, but aren't the object definitions in the word.inc file kept "version independent"?
    They should be, but they aren't. Generate a new include file for your version.
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      Heureka! Issue solved. Thank you very much, José!

      Is there at least some "upward" version independency i.e. would the inc file derived from the MSWORD9.OLB library file also work with Word 10 or 11?

      One last question: Just out of interest, I wanted to test the Word COM sample on my Windows 95 machine with Office 97 installed. PB/WIN ran fine under Windows 95, but the PB COM Browser wouldn't start.

      I just wondered, if PB/WIN runs under Windows 95, then why would the PB COM Browser need a higher Windows version? Ok, I know, Windows 95 is hopelessly outdated nowadays, but did anybody have the same issue? Is this maybe just a missing/outdated DLL problem?

      Heinz Salomon

      Comment


      • #4
        I just wondered, if PB/WIN runs under Windows 95, then why would the PB COM Browser need a higher Windows version
        Probably the same reason you could not use the 'install' program provided for PB/DLL 2.0 (the 16 bit compiler) except on a 32-bit machine.

        You had to 'install' on a 32-bit machine, then copy the required files to the 16-bit machine.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Is there at least some "upward" version independency i.e. would the inc file derived from the MSWORD9.OLB library file also work with Word 10 or 11?
          Only if you use OBJECT CALL/GET/SET/LET and late binding. As usual, M$ has broken his own rules by renaming methods such SaveAs as SaveAs2000 and adding a new SaveAs method.
          Forum: http://www.jose.it-berater.org/smfforum/index.php

          Comment


          • #6
            Thank you for your answers!

            Heinz Salomon

            Comment


            • #7
              Quote:
              Is there at least some "upward" version independency i.e. would the inc file derived from the MSWORD9.OLB library file also work with Word 10 or 11?
              Only if you use OBJECT CALL/GET/SET/LET and late binding. As usual, M$ has broken his own rules by renaming methods such SaveAs as SaveAs2000 and adding a new SaveAs method.
              Microsoft engineers told me years ago (I can't remember which conference) that using the library from the oldest version of an Office program you plan to support would maintain upward-compatibility. As José notes, you can't always get what MS promises.

              That said, many methods & properties remain unchanged. Depending on which you call, there may be no problems.

              When "inconsistencies" arise, I have found that you can maintain Int__Application/Int__Document and IDISPATCH references to the same Word applicication/document. That way you minimize the need for OBJECT CALL/GET/SET/LET calls to only the changed methods and properties.

              Ron

              Comment

              Working...
              X