Word Automation

V

Vik

Hello again
I have a problem with Word trying to shut down before the document is saved. I put a sleep statement between the commands and that helps. The size of the document varies and the larger documents cause more problems. Below is a snippit of code showing the saving and closing code

Info.Caption = "Word is creating the password
oWD.WritePassword = (frmTab0.Text0(4).Text) & CStr(Left$(CDec(Now()), 5)) & CStr(Right$(CDec(Now()), 1)

oWA.Activate 'Shift focus to Wor
Info.Caption = "Word is saving the file
oWA.ActiveDocument.SaveAs FileName:="c:\program files\tabs\ards\" & frmTab0.Text0(4) & ".doc
Sleep 200

Info.Caption = "Word is closing the file
oWA.ActiveDocument.Clos
oWA.Qui
Set oWA = Nothin

As always, any help is appreciated, Vik

Please don't respond to the email address since it is my MSDN alias.
 
S

Sonny Kocak[MSFT]

Do you mean "how to tell the when the print job is done writing to the
spooler?" or "how to tell when the print job is actually printed?"
What is the application doing that it need to be wait for printing? Below
is article that you may want to read.

ACC: How to Use Automation to Print a Microsoft Word Document
http://support.microsoft.com/default.aspx?scid=kb;en-us;154569

However, you probably famliar with most of this infomation. Please read
however before responding.


Sonny Kocak
Microsoft Developer Community Support
Email : (e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
| Thread-Topic: Word Automation
| thread-index: AcQrxQXyVxf+24yNQra2BJ/11DbXtA==
| X-WN-Post: microsoft.public.office.developer.automation
| From: =?Utf-8?B?Vmlr?= <[email protected]>
| References: <[email protected]>
| Subject: RE: Word Automation
| Date: Mon, 26 Apr 2004 12:31:08 -0700
| Lines: 3
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.office.developer.automation
| Path: cpmsftngxa10.phx.gbl
| Xref: cpmsftngxa10.phx.gbl
microsoft.public.office.developer.automation:9056
| NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
| X-Tomcat-NG: microsoft.public.office.developer.automation
|
| Sorry, I was in a different world and did not state my problem clearly.
What I would like to do is test to see if Word has finished printing before
moving to the next step in program execution. Can I get an event from
Word? I use late binding because my users have Word 8, 9, and 10 installed
so any solution would have to work with all three. I have looked over all
the Word MVP sites I could find for a point in the right direction as well
as MSDN. Again, any assistance is greatly appreciated, Vik.

Please don't reply to the email address since it is my MSDN alias.
|
 
V

Vik

Hi Sonny. Thanks for the reply. Again, I left out part of the code. The code prints the document, creates a password, activates the application, and saves the document with the password. All of these activities are transparent to the user. Finally, Word closes the active document, quits the Word application, and sets the reference to the Word application to nothing. The problem occurs when Word tries to save the document before the document has finished printing. Or, in some cases, tries to quit Word while the document is still being saved. I have inserted Sleep statements to increase the time the program takes to move to the next line of program execution. This has helped, but it is trial and error and adds unnecessary delays to short documents and doesn't fix the problem on very long documents. I hope there is some way to check for completion of the printing and saving of Word documents

MDIForm1.Info.Caption = "Word is sending document to printer
oWD.Printout Range:=0, Background:=Fals

Info.Caption = "Word is creating the password
oWD.WritePassword = (frmTab0.Text0(4).Text) & CStr(Left$(CDec(Now()), 5)) & CStr(Right$(CDec(Now()), 1)

oWA.Activate 'Shift focus to Wor
Info.Caption = "Word is saving the file
oWA.ActiveDocument.SaveAs FileName:="c:\program files\tabs\ards\" & frmTab0.Text0(4) & ".doc
Sleep 200

Info.Caption = "Word is closing the file
oWA.ActiveDocument.Clos
oWA.Qui
Set oWA = Nothin

Here is the updated code for printing, generating a password, saving, and closing the document and quitting Word and setting the Word object to nothing. Again, all of this is transparent to the user, requiring no input or intervention by the user. When the the save tries to execute before the print has finished, the user is prompted for the password which the user does not know. When the code executes with the proper timing, there are no user prompts. I hope this explains the problem a little better. Thanks, Vik.
 
P

Peter Huang

Hi Vik,

It seems that I can not reproduce the problem.
Here goes my test code.

Dim strPath As String
strPath = "c:\test.doc"
Dim fso As New FileSystemObject

'If we saveas a file to a pre-existed password protected file, we will get
password prompt dialog, so we delete it here.
fso.DeleteFile strPath, True
Dim o As New Word.Application
o.Visible = True
o.Documents.Add
Dim i As Integer
Dim str As String
'generate a big test file
For i = 0 To 1000
str = str + "/****************Test line*******************/"
Next
Dim pwd As String
pwd = "PassWord"
o.ActiveDocument.Content = str
'If we use the Backgroud argument to false the word will not return until
we finish sending the document to the print spooling.
o.PrintOut False
o.ActiveDocument.Password = pwd
o.Activate
o.ActiveDocument.SaveAs FileName:="c:\test.doc"
o.ActiveDocument.Close
o.Quit
Set o = Nothing

Based on my research, Word Object Modal did not provide such event(e.g.
Document_AfterPrint) for us.
If you still have any concern, please post a simple reproduce sample for us
to further troubleshooting.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
V

Vik

I owe ya'll an apology. I have mis-stated the problem. The problem is in saving the file, not printing the file. When the user selects <Print to File>, the program bypasses the oWD.PrintOut statement and saves the document. Sometimes, at this point the user gets prompted for the password. I think it is because Word is trying to close the application before the save is completed. The problem only occurs when the document is large, i.e. several supplements have been added. Adding the Sleep 2000 helped, but I hope to find a better solution or a cause to the problem. Thanks again for all the help, Vik

MDIForm1.Info.Caption = "Starting Print Application

'Create a separate instance of Word applicatio
Set oWA = CreateObject("Word.Application"

MDIForm1.Info.Caption = "Adding The ARD Document
oWA.Visible = Fals
Set oWD = oWA.Application.Documents.Add(App.Path & "\ard10.dot"
oWA.Application.Options.PrintBackground = Fals
oWD.Application.Options.Pagination = Tru
oWD.Application.Options.CheckGrammarAsYouType = Fals
oWD.Application.ActiveDocument.ShowGrammaticalErrors = Fals
oWD.Application.ActiveDocument.ShowSpellingErrors = Fals

If oWA.ActiveDocument.ProtectionType <> -1 Then 'wdNoProtectio
oWA.ActiveDocument.Unprotec
End I

This is an example of how I add supplementary pages to the main document as needed by the user. There can be up to 30 supplements added dynamically depending upon the student being evaluated

'This section will print the REED Supplemen
'if bREED is tru
'*************************************************
If bReed The
Call Print_REE
End I
'**** end print REED *************************

Private Sub Print_REED(
Dim i As Intege
Dim j As Intege
On Error GoTo er

MDIForm1.Info.Caption = "Printing REED
Set owdDoc2 = oWA.Application.Documents.Add(Template:=App.Path & "\reed.dot"
owdDoc2.Application.Options.Pagination = Tru
owdDoc2.Application.Options.CheckGrammarAsYouType = Fals
owdDoc2.Application.ActiveDocument.ShowGrammaticalErrors = Fals
owdDoc2.Application.ActiveDocument.ShowSpellingErrors = Fals
owdDoc2.Activat

'***********************
'Fill in all the bookmarks her
'***********************

Set owdRng = oWD.Conten
With owdRn
.Collapse Direction:=
.FormattedText = owdDoc2.Content.FormattedTex
End Wit
owdDoc2.Close SaveChanges:=
Exit Su
err
Debug.Print "Reed Print: " & err.Descriptio
Resume Nex
End Su

After all the documents are added then the following code executes

If bPrintToFile The
On Error Resume Nex
bPrintToFile = Fals
Els
MDIForm1.Info.Caption = "Word is sending document to printer
oWD.Printout Range:=0, Background:=Fals
End I

Info.Caption = "Word is creating the password
oWD.WritePassword = (frmTab0.Text0(4).Text) & CStr(Left$(CDec(Now()), 5)) & CStr(Right$(CDec(Now()), 1)

oWA.Activate 'Shift focus to Wor
Info.Caption = "Word is saving the file
oWA.ActiveDocument.SaveAs FileName:="c:\program files\tabs\ards\" & frmTab0.Text0(4) & CStr(Left$(CDec(Now()), 5)) & Right$(CDec(Now()), 1) & ".doc
Sleep 200

Info.Caption = "Word is closing the file
oWA.ActiveDocument.Clos
oWA.Qui
Set oWA = Nothin

MDIForm1.Info.Caption = "Word has Printed & Saved " & (frmTab0.Text0(4).Text) & " and has shut down

I hope this clarifies what I am trying to do in my code, Vik.
 
V

Vik

I increased the Sleep 2000 after the document save to Sleep 4000 and the prompt for the password did not occur. This leads me to believe that the issue is a timing problem. I hope there is a good resolution. Thanks, Vik.
 
P

Peter Huang

Hi Vik,

I can not reproduce the behavior, can you try my code first to see if the
problem persists on your side, also you can change my code based on your
senario to reproduce the problem and then post here for us to furhter
troubleshooting. (In my test, I will add a few large pictuer bmp file into
the word file which will cause my word to take a long time to save the
file).

As a suggestion, if you do not want to use Sleep, I think you may try to
wait for the Saved property of the document to be true after you call the
SaveAs function.

While Not o.ActiveDocument.Saved
DoEvents
Wend

You may try my suggestion and let me know the result.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
V

Vik

Thanks Peter
I think the ActiveDocument.Saved is what I have been looking for. I discovered similar timing problems while working with MSDN, using one of my incidents, involving saving tables in Access. It seems Visual Basic wants to move on instead of waiting until the current activity? is finished. I really appreciate you help, Vik.
 

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