Babu Annamalai

Environment: Windows 2003 running under VPC,Office 2007 RTM,VS2005

I have a schema attached to the document in question and here is the code snippet which errors out with a COM exception.

Dim nd As MSWord.XMLNode = wordDoc.XMLNodes.Add("dummyNode", <<namespace>>, CObj(rng))

Dim img As MSWord.InlineShape = nd.Range.InlineShapes.AddPicture("c:\blah.jpg", SaveWithDocument:=True) 'COM Exception occurs here when trying to add a picture

NOTE: The 'rng' object points to a bookmark inside a text box placed under the first page header section

The exact exception trace is shown below:

System.Runtime.InteropServices.COMException was caught
ErrorCode=-2147467259
Message="Error HRESULT E_FAIL has been returned from a call to a COM component."
Source="Microsoft.Office.Interop.Word"
StackTrace:
at Microsoft.Office.Interop.Word.InlineShapes.AddPicture(String FileName, Object& LinkToFile, Object& SaveWithDocument, Object& Range)
at <<Removed the debug source code level stack trace>>

I tried the same using VBA and it worked but with interop scenario it fails. It definitely looks like a regression in Word 2007 because the very same interop code did work properly in Word 2003.

Any quick workaround or a fix would be much appreciated. Thanks in advance.

Cheers

Babu



Re: Visual Studio Tools for Office COM exception when trying to add picture as inline shape in Word 2007 RTM

Cindy Meister

Hi Babu

I can't duplicate the problem. I didn't have time to attach a schema and insert an XML element into the textbox. But I did create a textbox (of the Drawing tool type - you don't specify if that's the kind you used) in the document Header and put a bookmark in it. I had no trouble inserting an InlineShape.

FWIW, during beta there was a problem with InlineShapes.AddPicture, but it appears to have been corrected. you're absolutely certain you're working with the RTM version of Office 2007, on a clean installation (one that hasn't had the beta installed)






Re: Visual Studio Tools for Office COM exception when trying to add picture as inline shape in Word 2007 RTM

Babu Annamalai

Hi Cindy,

To be very specific, this issue only occurs when you try and insert an inline picture inside a xml node. I used a simple text box.

The first occurance which I posted earlier was on a machine which was not a clean installation(meaning uninstalled Office 2007 beta and installed RTM on the same machine). Today, just to confirm, I went ahead and did a clean installation(Windows XP SP2+ Office 2007 RTM) and the COM exception still occurs.

Cheers

Babu





Re: Visual Studio Tools for Office COM exception when trying to add picture as inline shape in Word 2007 RTM

Cindy Meister

Hi Babu

I still don't see a problem. I tried inserting into an existing node, as well as creating one on-the-fly, using the Add method (as you show in your code snippet).

That means there's something different in your configuration. The problem during beta was apparently one of timing (asynchronous insertion of the picture). If you put a break point in your code on the line to insert the picture, then STEP through it (F11), do you still get an error

Here's the test code I used. The bookmark "Target" is in a textbox in a document's header. I'm starting in the Print Layout view.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wdApp As Word.Application = GetActiveObject("Word.Application")
Dim wdDoc As Word.Document = wdApp.ActiveDocument
Dim bkmName As String = "Target"
Dim bkm As Word.Bookmark
Dim rng As Word.Range
Dim ils As Word.InlineShape
Dim nd As Word.XMLNode
Dim ns As Word.XMLNamespace
Dim nsBook As Word.XMLNamespace
For Each ns In wdApp.XMLNamespaces
If ns.URI = "BookSchema" Then
nsBook = ns
Exit For
End If
Next
If wdDoc.Bookmarks.Exists(bkmName) Then
bkm = wdDoc.Bookmarks(bkmName)
rng = bkm.Range
'nd = rng.XMLNodes(2)
' nd.Range.Select()
nd = rng.XMLNodes.Add("BOOK", nsBook.URI)
ils = nd.Range.InlineShapes.AddPicture("c:\test\test.bmp", SaveWithDocument:=True)
Else
MsgBox("bookmark does not exist")
End If
End Sub






Re: Visual Studio Tools for Office COM exception when trying to add picture as inline shape in Word 2007 RTM

Babu Annamalai

Hi Cindy,

The above code snippet which you have added perfectly works fine. In my case I am not using Marshal.GetActiveObject(...) but for I create an instance of Word.Application. Replace the first 2 lines of your code snippet by the lines shown below,

------------------------------------------------------------------------------------

Dim wdApp As Word.Application = New Word.Application

wdApp.Visible = True

Dim wdDoc As Word.Document = wdApp.Documents.Add(Visible:=True)

------------------------------------------------------------------------------------

Now you will be able to reproduce the issue raised by me originally. This error occurs on that line when I STEP through the code as well in debug mode.

I wont be able to use Marshal.GetObject(...) because I get a document instance via SmartDocument API(ISmartDocument.SmartDocInitialize)

Cheers

Babu





Re: Visual Studio Tools for Office COM exception when trying to add picture as inline shape in Word 2007 RTM

Cindy Meister

Hi Babu

Even with the change you suggest I can't reproduce the problem :-(

You mention you're using the ISmartDocument interface. Perhaps the problem is related to that. Would any events be firing in that interface They'd run asynchronously with graphics in Word, I'm guessing...






Re: Visual Studio Tools for Office COM exception when trying to add picture as inline shape in Word 2007 RTM

Marcus_dotnet_programmer

Hello,

I am running into the same problem. When porting our Office 2003 Add-in solution to Office 2007 (happens with the Office 2003 or with Office 2007 PIA's) we get a COM Exception (HRESULT E_FAIL) and the same code works perfectly with Office 2003...

Have you found a solution yet

Marcus