Trouble Instantiating Word.Document Object in VB.NET

P

Phil Galey

I'm trying to automate MS Word 9 on Windows 2000 from VB.NET. I'm using the
following code:
Dim TmpltPath As String = Path.Combine(Application.StartupPath,
"SpanishForm.doc")
Dim pcss(), pcs As Process
Dim WDoc As Word.Document
Dim WApp As Word.Application

If Process.GetProcessesByName("WINWORD").Length = 0 Then
Process.Start(mstrWord)
Threading.Thread.Sleep(3000)
End If
WDoc = New Word.Document() <=== Interop COM Exception: Call was
rejected by callee.
WApp = WDoc.Application
WApp.Documents.Close(False)
WApp.Documents.Open(strDocPath)

but about 50% of the time, it errors out as indicated above. How can I
dependably instantiate a new Word.Document object without getting this
error? Thanks.
 
J

Jonathan West

Phil Galey said:
I'm trying to automate MS Word 9 on Windows 2000 from VB.NET. I'm using
the
following code:
Dim TmpltPath As String = Path.Combine(Application.StartupPath,
"SpanishForm.doc")
Dim pcss(), pcs As Process
Dim WDoc As Word.Document
Dim WApp As Word.Application

If Process.GetProcessesByName("WINWORD").Length = 0 Then
Process.Start(mstrWord)
Threading.Thread.Sleep(3000)
End If
WDoc = New Word.Document() <=== Interop COM Exception: Call was
rejected by callee.

Try changing the line above to this

wDoc = WApp.Documents.Add()


WApp = WDoc.Application
WApp.Documents.Close(False)
WApp.Documents.Open(strDocPath)

but about 50% of the time, it errors out as indicated above. How can I
dependably instantiate a new Word.Document object without getting this
error? Thanks.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
C

Cindy M.

Hi Phil,

Do you have to use GetProcessByName rather than using Reflection and
GetActiveObject? The problem is, you really should assign the instance of Word
to an application object, then start the new document from there. Something
more like (off the top of my head, so possible syntax errors!)

Imports word = Microsoft.Office.Interop.Word
Imports marshal = System.Runtime.InteropServices.Marshal

Private MySub()
Dim wdApp as Word.Application =
CType(marshal.GetActiveObject("Word.Application"), word.Application)

'And if no Word app is running, then
'wdApp = New Word.Application
I'm trying to automate MS Word 9 on Windows 2000 from VB.NET. I'm using the
following code:
Dim TmpltPath As String = Path.Combine(Application.StartupPath,
"SpanishForm.doc")
Dim pcss(), pcs As Process
Dim WDoc As Word.Document
Dim WApp As Word.Application

If Process.GetProcessesByName("WINWORD").Length = 0 Then
Process.Start(mstrWord)
Threading.Thread.Sleep(3000)
End If
WDoc = New Word.Document() <=== Interop COM Exception: Call was
rejected by callee.
WApp = WDoc.Application
WApp.Documents.Close(False)
WApp.Documents.Open(strDocPath)

but about 50% of the time, it errors out as indicated above. How can I
dependably instantiate a new Word.Document object without getting this
error?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top