PDA

View Full Version : Solved: How to suppress "Printing" dialog when printed from invisble "winword"



Mohana Mural
12-04-2006, 10:46 PM
Hello experts,

Target: Need to print documents without the word application getting displayed or without any dialogs or messageboxes shown to the user.

I am almost near the target, with the following vba code snippet.
It creates a new "winword" instance invisible to the user, and prints the opened document to the default printer and quits.


Set oWord = CreateObject("Word.Application")
oWord.Visible = False
oWord.Documents.Open "C:\Test.doc"
oWord.Application.DisplayAlerts = wdAlertsNone
oWord.PrintOut Background:=False
oWord.Quit

At present, a dialog box with the title "Printing" is shown to the user. It contains the text that "Test.doc" is being printed on the printer. It also has a "Cancel" button. The dialog box automatically gets closed once the printing is over.

Would like to get your suggestions on the way to avoid the display of "Printing" dialog box.

Thanks in advance :hi:
Mohan

Mohana Mural
12-05-2006, 03:37 AM
Hello Experts,

I was able to suppress the printing dialog with the following code:


Set oWord = CreateObject("Word.Application")

oWord.Visible = False
oWord.Application.DisplayAlerts = wdAlertsNone

oWord.Documents.Open "C:\Test.doc"

oWord.PrintOut Background:=True

While oWord.Application.BackgroundPrintingStatus <> 0
Wend

oWord.Application.DisplayAlerts = wdAlertsAll

oWord.Quit


Though, this is successful, in some cases of document printing, the user is shown a dialog of "Do you want to save the changes to "doc name" with yes, no and cancel button.

I understand that it may be due to the following cause: Seems like windows word is maintaining the last printed time of the document in the document itself and since the document gets printed now, the document needs to be updated.

Well, I may be wrong. Any help to avoid such a dialog would be very helpful.

Thanks in advance, :hi:
Mohan

fumei
12-05-2006, 06:35 AM
You do not seem to be actually closing the document. You Quit the app, but do not explicitly close the document. If you explicitly close it with a DoNotSaveChange you should avoid that dialog.Set oWord = CreateObject("Word.Application")
With oWord
.Visible = False
.Application.DisplayAlerts = wdAlertsNone
.Documents.Open "C:\Test.doc"
.PrintOut Background:=True
While .Application.BackgroundPrintingStatus <> 0
Wend
.Application.DisplayAlerts = wdAlertsAll
.ActiveDocument.Close wdDoNotSaveChanges
.Quit
End With

mdmackillop
12-05-2006, 01:20 PM
Hi Mohana,
Welcome to VBAX.
If you select your code and click the VBA button, it will format as shown, making it more readable.
Regards
MD

Mohana Mural
12-06-2006, 01:11 AM
:hi: Fumei, and other experts,

Sincere thanks for pointing the mistake. I should have closed the document, irrespective of the case whether it was changed or not.

I came across one more hurdle namely the way to skip password protected documents without user intervention. At present, when I run this code, although, word is requested to be invisible, when an attempt is made to open a password protected document, the word application appears in the screen and the dialog box to enter the password is shown to the user. I would like to skip the document without any user intervention.

I searched many resources and forums, but could not find something useful to solve the issue. I also saw "haspassword" property, but it seemed to me that it could be used only for an already opened document rather than an un-opened document. I may be wrong.

Finally, by trial and error, I was able to succeed. The following is the code. The trick that I used was to provide a dummy password. If the document is not password protected, then this dummy password is ignored. If it is really password protected, then there occurs an error and word application is closed, but without any user intervention.


Set oWord = CreateObject("Word.Application")
On Error GoTo ErrorHandler
With oWord
.Visible = False
.Application.DisplayAlerts = wdAlertsNone
.Documents.Open Filename:="C:\Test.doc", PasswordDocument:=" "
.PrintOut Background:=True
While .Application.BackgroundPrintingStatus <> 0
Wend
.ActiveDocument.Close wdDoNotSaveChanges
.Application.DisplayAlerts = wdAlertsAll
ErrorHandler:
.Quit
End With


Now here comes the long list of questions to experts.

1) Is there any other better way to do this? I am just two days old with respect to learning/using VBA. So, probably, there may be something better.

2) Is the following statement really required? My understanding is that the previous wdAlertsNone that was set is only valid for this word instance that is newly getting created. Again, I may be wrong in this understanding.

.Application.DisplayAlerts = wdAlertsAll


3) When the document is just opened, printed and then the application is quit, why do we sometimes get the "save dialog"? Ofcourse, if I close the document with "wdDoNotSaveChanges" and then quit the application, I don't see the dialog. But, I would like to know the "internals" of why word sometimes throws such a dialog for certain documents and no such dialogs for certain other documents. Basically, was my previous guess valid, viz, word storing the last printed date in the document data itself and due to this reason, user is shown a dialog after printing. But, I doubt that guess, because it does not seem to be valid :( Infact, I opened that particular test document in "readonly" mode, in winword application, printed that doc and closed winword. I am prompted with a "save dialog" and so I am really not sure what is happening there!!! I opened the document in readonly mode, and then just closed it. Again, I am getting that save dialog!!!

4) Can the "HasPassword" property be used to check whether a document is password protected or not without opening the document?

5) Should the statement

On Error GoTo ErrorHandler
be the second statement or the first statement. Programatically, it makes sense to be in the second statement.

6) Is this code worth to be added to a KB. Well, I think so, as I didn't find such a code in the KB or in any net resources. But, I would like to hear it from experts, as I am a newbie.

7) I would like to mark this thread as "Solved" tomorrow after hearing any suggestions from you or other experts.

Thanks, :)
Mohan


:hi: MD,

Thanks for your advise. I have used the VBA button for formatting and will use it in the future too!!!

Thanks,
Mohan