normal.dot trying to save when instantiating new Word.Application

J

JimLad

Hi,

In my application, I open a document, change some values, print, then
exit without saving. This works fine if there are no WINWORD processes
already open. However, if word is already open (with unchanged default
doc) then I get ''Word cannot save this file as it is already open
elsewhere. (Normal.dot)". Is there a setting that I can change to
prevent normal.dot getting changed or alternatively is there any way
of suppressing the message? At the moment the only thing I've managed
to do is make the app sleep for a second so the user has time to close
the prompt before the app crashes - the app then continues fine. But
it's a bit of a rubbish workaround - the user sees the message and has
to take swift action to avoid a crash.

Sorry. I know there are a lot of posts on normal.dot issues, but most
are when closing a document or instance - this is when opening an
instance. I'm not really up on Word automation...

Cheers,

James

Dim wApp As Word.Application
Dim strPath As String
Dim objWord As Word.Document

'Open Word
Try
wApp = New Word.Application ' Message shown when this line
is executed.
Catch ex As Exception
...
End Try

'TODO: This is a workaround for a dialogue box that gets shown
if Word is already open.
'"Word cannot save this file as it is already open elsewhere.
(Normal.dot)"
'This sleep statement gives the user time to close the dialog
box and continue.
'If dialog box is not closed in time then objWord is created
successfully but all properties return {"Call was rejected by callee.
(Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))"}) and the
application will crash when attempting the find and replace.
System.Threading.Thread.Sleep(1000)

Try
'Open separator sheet
objWord = wApp.Documents.Open(strPath, ReadOnly:=True)

Try
'find an replace the fields in the document
objWord.Content.Find.Execute("pclient", False, False,
False, False, False, False, False, True,
CStr(objMember.FilenetClientName), True)
wApp.Visible = False

'print the result
objWord.PrintOut()
MsgBox("Print out has been sent to the default
printer.")
Catch e As Exception
...
Finally
'Close document
objWord.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
objWord = Nothing
End Try
Catch e As Exception
...
Finally
'close word
'CType to _Application interface to avoid Quit method
problems.
CType(wApp,
Word._Application).Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
wApp = Nothing
End Try
 
J

JimLad

I've now stopped it crashing, by looping until the dialog box is close
by the user. Is there any Status property I can use to check when the
dialog box is closed?

Obviously I'd still like to stop the message appearing at all...

'TODO: This is a workaround for a dialogue box that gets shown
if Word is already open.
'"Word cannot save this file as it is already open elsewhere.
(Normal.dot)"
'Check whether doc was correctly open. If not, then sleep for
a second and then try again.
Try
'Open separator sheet template - keep trying until object
opens correctly.
While 1 <> 2
objWord = wApp.Documents.Open(strPath, ReadOnly:=True,
Visible:=False)
Try
Dim obj As Object = objWord.Content
Catch ex As Exception
'Remove ref.
objWord = Nothing
'Wait 1 second
System.Threading.Thread.Sleep(1000)
Continue While
End Try
Exit While
End While
 
C

Cindy M.

Hi JimLad,
In my application, I open a document, change some values, print, then
exit without saving. This works fine if there are no WINWORD processes
already open. However, if word is already open (with unchanged default
doc) then I get ''Word cannot save this file as it is already open
elsewhere. (Normal.dot)". Is there a setting that I can change to
prevent normal.dot getting changed or alternatively is there any way
of suppressing the message?
Mmm. Tricky...

You can set Application.NormalTemplate.Saved = true which would tell
Word that the file is "not dirty". Problem is, this could lose any
changes the user may have made (such as AutoText, macros).

OTOH, you don't really want to save it without the user's permission, as
he might have been testing things that he doesn't want to keep.

Is there an important reason you're starting a new instance of the Word
application, rather than using one that's already running? (You can open
a document with the Visible argument set to False so that the user isn't
involved in the process.)

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 :)
 
J

JimLad

Better late than never! :) Soz for the late reply.
Is there an important reason you're starting a new instance of the Word
application, rather than using one that's already running? (You can open
a document with the Visible argument set to False so that the user isn't
involved in the process.)

Good point about opening a 2nd instance.

In which case, how do I check for a running instance of Word and then
hook into it?

Presumably I use
System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") ?

Anyone got any comments on whether it is best to start your own
instance or to use an existing one?

Cheers,

James
 
C

Cindy M.

Hi JimLad,
In which case, how do I check for a running instance of Word and then
hook into it?

Presumably I use
System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") ?
Yes.

Anyone got any comments on whether it is best to start your own
instance or to use an existing one?
I think an important point is whether what you're doing is something the user
expects (like, a process the user started from within the outside
application). If it is, then by all means "share" the instance the user
started.

OTOH, if you're using Word in the background for something that the user
shouldn't notice, and what you're doing would be noticeable, this would be
confusing (or even a hindrance). In that case, a separate instance would be
better. In that case, it would probably be better to not continually quit and
restart the application, but to leave it open until, say, the user logs off.

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