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
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
Comment