Friday, July 13, 2007

Error inserting images into Word document

On calls to Range.InlineShapes.Add("C:\blah.jpg") and Range.Shapes.Add("C:\blah.jpg") I kept getting a very helpful, precise error:

The server threw an exception.
(Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))

Thanks for that!

After playing around with the code for awhile, I found that my particular problem was being caused by how I instantiated the Word.Document object. My code read:

Dim mobjWordApp As New Word.Application()
mobjWordApp.Visible = False
Dim objDocSet As Word.Documents = mobjWordApp.Documents
Dim objDoc As Word.Document = objDocSet.Add(Visible:=MsoTriState.msoFalse)


Removing the Visible parameter from the Documents.Add() call fixed the error. It was unnecessary anyway, since I had already set Application.Visible to False.

2 comments:

Steve said...

Wow, that's a weird, seemingly unrelated cause! Thanks for posting, that would have taking me a bunch of time to find. :)

Geoff said...

Thanks. This worked for me too.