Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

Trouble Instantiating Word.Document Object in VB.NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Phil Galey - 25 Aug 2006 00:00 GMT
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.
Jonathan West - 25 Aug 2006 08:25 GMT
> I'm trying to automate MS Word 9 on Windows 2000 from VB.NET.  I'm using
> the
[quoted text clipped - 11 lines]
> 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)
[quoted text clipped - 3 lines]
> dependably instantiate a new Word.Document object without getting this
> error?  Thanks.

Signature

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

Cindy M. - 25 Aug 2006 14:51 GMT
Hi Jonathan,

> Try changing the line above to this
>  
> wDoc = WApp.Documents.Add()

That's going to be a bit difficult if he hasn't assigned
wdApp to a Word application instance, yet...

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
:-)
Cindy M. - 25 Aug 2006 09:35 GMT
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:
[quoted text clipped - 17 lines]
> 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 :-)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.