PDA

View Full Version : Lock working instance of Word


deanna
04-26-2010, 07:03 PM
My code opens and writes to a Word document. If another Word document is already open, a new instance of Word is created and I don't have any issues. Where I run into a problem is when processing my document. If another Word document is opened after I begin editing the first document, the second doc runs off the initial instance of Word and I lose my reference to the first doc with the error "The requested member of the collection does not exist".

Is there a way when I initiate Word for the document I am editing that I can lock that instance so that if Word is used to open any other documents while processing the subsequent documents will start up in a new instance of Word?

Or is there some way other than what I am doing for specifying the document I want to edit so that I don't lose reference to it when another document is opened during processing?

[CODE]
Public Class WordDoc
Private m_WordApp As Microsoft.Office.Interop.Word.ApplicationClass = Nothing
Private m_WordDoc As Document = Nothing
...

Public Sub OpenWordDocument(ByVal filename As String, ByVal OpenAsFormat As WdOpenFormat)
m_WordDoc = m_WordApp.Documents.Open(DirectCast(m_sWordDocFileName, Object), m_MISSINGVALUE, _...
...

Private Sub BuildWordDocument()
...
objWordDoc = New WordDoc()
objWordDoc.OpenWordDocument(sDocPath, WdOpenFormat.wdOpenFormatDocument)
...
[CODE]

Mike_O
04-26-2010, 08:27 PM
Hey deanna,

Okay, how about this. Temporarily set the file which you're processing as read-only, then change it back when you're done.

System.IO.File.SetAttributes("[your file path]", FileAttributes.ReadOnly);
System.IO.File.SetAttributes("[your file path]", FileAttributes.Normal);

Mike

deanna
04-28-2010, 07:10 PM
Thanks for the response, Mike.

No luck. I put the read only just after opening the doc and then set to normal just before saving. Opened another doc while processing and still get the same error.

Mike_O
04-28-2010, 08:31 PM
Hey deanna,

Are you able to narrow down exactly which line of code is causing this. Thing is, you say this occurs somewhere after you begin "editing" or "processing", and it I think it would be helpful to know exactly where. I would debug the entire word automation process line by line, from beginning to end. When stepping through each and every line, I would keep opening Word and see if this problem occurs.

Mike