• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 142
  • Last Modified:

checking printer status and pausing

Hi All,

I am trying to print a lot of Word 2000 documents using the folling code:

'keep looping until all printed
InvoiceObj.Selection.GoTo what:=-1, Name:="ContactTitleLastName"'InvoiceObj.Selection.Font.Bold = wdToggle
InvoiceObj.Selection.InsertAfter " " & sContactTitle & " " & sContactLastName
InvoiceObj.Selection.Font.Color = 0

InvoiceObj.Application.Visible = False
InvoiceObj.ChangeFileOpenDirectory App.Path & "\"
InvoiceObj.ActiveDocument.SaveAs FileName:="LetterTo" & sContactCompany & sContactTitle & sContactFirstName & sContactLastName & ".doc"

Set InvoiceObj = Nothing


Basically, I am opening a Word document, inserting some data, saving the doc as a different name, and then printing it.

2 problems occur. I get an error saying that a previous copy has been open, even though I closed it. Second, the printer does not like it when too many jobs come at once.

So, I would like to be able to wait until one print job is done before sending the next. AND I would like to be able to open the document and close it without any problems. The code within the ... gets repeated in a loop. Other code checks to make sure the file is NOT-read-only. It also, opens rather than creates the document.

Any ideas how to solve these two problems. Successful Working Sample code will get you the points.

  • 3
  • 2
  • 2
  • +2
1 Solution
Ryan ChongCommented:
See this if it's help:


I would make a couple of suggestions... put a doevents in your loop, then maybe just create a pause by calling API Sleep occasionally to slow the thing down, ESPECIALLY BEFORE A NEW OPEN, and/or after a close - to allow the close operations to complete.  These may seem like too simple measures, but it sounds to me like you are just pushing too hard... Your docs aren't going to get off the printer any faster than the printer can print them and your program can certainly generate them much faster than the printer can print... I just think you need some breathing space for starters.
DoughBoyAuthor Commented:
Hi ryancys,
I am way too lazy (and often too dumb) to use API's.

Hi trkcorp,
I tried using doevents. That is the most dangerous line of code to use in VB. It makes my computer go crazy with all kinds of lockups.

Any other ideas?
Thanks for your concern.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

The sleep api is utterly simple, no fancy parameters or return codes:

Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

Delclare it in a module as public, or in a form as private.

Use like this:

Call Sleep(1000)

Where 1000 = 1000 milliseconds which is 1 second...
You can of course adjust that value up or down as required.
From VB Help...
DoEvents Function:
Yields execution so that the operating system can process other events.
DoEvents( )

The DoEvents function returns an Integer representing the number of open forms in stand-alone versions of Visual Basic, such as Visual Basic, Professional Edition. DoEvents returns zero in all other applications.

DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent.

DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file. For long-running processes, yielding the processor is better accomplished by using a Timer or delegating the task to an ActiveX EXE component. In the latter case, the task can continue completely independent of your application, and the operating system takes case of multitasking and time slicing.

Caution   Any time you temporarily yield the processor within an event procedure, make sure the procedure  is not executed again from a different part of your code before the first call returns; this could cause unpredictable results. In addition, do not use DoEvents if other applications could possibly interact with your procedure in unforeseen ways during the time you have yielded control.
Hi DoughBoy,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept trkcorp's comment(s) as an answer.

DoughBoy, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer
DoughBoyAuthor Commented:
Fair enough. Sorry for the delay
You can finalize this yourself.  Jusr click the "Accept Comment as Answer" link that is next to the post.
-- Dan
Moving to the paq

Experts Exchange Moderator

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now