Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Here is the code i use to print a document using Word

Basically i open a ".dot" document, fill the bookmark, print the document then quit without

saving it

 

 

Dim oword As Object

Dim odoc As Object

Dim file As String

 

File="c:\test.dot"

oword = CreateObject("Word.Application")

oword.visible = False

Try

odoc = oword.documents.open(file)

Catch ex As Exception

MsgBox("Cannot find the template " & file & ". Operation cancelled")

oword.quit(0)

Exit Sub

End Try

 

odoc.bookmarks("Title").RANGE.TEXT = Title

odoc.bookmarks("SubTitle").RANGE.TEXT = SubTitle

odoc.printout(0)

odoc.APPLICATION.QUIT(0)

 

It works fine with office 2002

But when the programs runs with office 2003, i have some problems

If outlook 2003 is currently running on the machine, closing the word document doesn't work and hangs

with the following message

 

"Word cannot save this file because it is already open elsewhere

(C:Documents and Settings\...\Normal.dot)"

And the user has to click on button OK to close the word window

If Outlook 2003 is not running on the machine (just installed), everythings works fine

 

Any solution to this problem (except asking user to close Outlook before printing) ?

 

(PS : users have the option "use Word as email editor" selected

Posted
You could try to use the "odoc.quit()" instead of "odoc.APPLICATION.QUIT(0)", and you could set "odoc = nothing" & "oword = nothing"

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

Posted

If Outlook is set to use Word as the e-mail editor, Outlook starts up a hidden instance of Word.

 

Since Word is then already running, you could try and reuse that existing instance. Use GetObject instead of CreateObject. If GetObject fails (i.e. Word wasn't running) then use CreateObject.

 

Using odoc.Quit is unlikely to work, since the Word.Document object does not have a Quit method. :) It'll have to be oword.Quit or odoc.Application.Quit.

Posted

Why not create a new instance of the word object.

Dim oword As New Word.application

 

That way you would only be closing this instance of Word.

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

Posted

You might also want to have a look at the following thread.

 

http://www.xtremedotnettalk.com/showthread.php?t=91634

 

It will give you some examples of how to open a new instance of Word.

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...