[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Garbage printing problem with VB6.0 and MS Word objects

Posted on 2002-03-21
13
Medium Priority
?
301 Views
Last Modified: 2013-11-13
We are using Microsoft word 9.0 object library with VB 6.0 to create and print letters.

In the first part,
1) We create word templates (.dot files) with bookmarks.
2) We open these templates in our VB program using the word objects.
3) We set the values of bookmarks and save the document with .doc extension.

This part is working fine.

In the second part,
1) We open the document saved above, and print it using, once again, word objects.

Now, here, most of the times, the document prints fine, but once in a while, what's happening
is that it prints the document all messed up, kind of like when the paper gets stuck in the printer
and all the text in a line gets printed at the same spot leaving a black square. When we print the same
document again, it prints fine.
0
Comment
Question by:Fundu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 2
13 Comments
 
LVL 22

Accepted Solution

by:
cookre earned 900 total points
ID: 6887237
I'm assuming that you're printing from within the program.

That print behavior indicates file corruption - either during transmission to the printer or of the file itself, the latter being the most likely.

Now, you know the file you're ready is good because sometimes it prints OK.  This would tend to implicate the program doing the printing.

Is the doc object loaded asynchronously, either explicitly or unintentionally (e.g., API behavior), so that, conceivably, print could begin without the doc being fully loaded?

One wonders if the successful rerun is serendipitously suceeding by virtue of cache getting the object loaded faster the second time around.
0
 

Author Comment

by:Fundu
ID: 6887344
Cookre,

Thanks for your comments.

We also thought on similar lines and decided to put a delay of 4-5 secs before printing the document, but that doesn't help either.

I wasn't aware of the possibility of synchronization of the steps. May be I will try that out.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6888097
Are you sure that you are saving and closing the documents properly? Word is famous for messing things up if you don't do it THEIR way.

CJ
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Fundu
ID: 6888405
CJ,

Can you elaborate on that more? I think we are closing and everything properly, since the winword.exe instance closes in task manager.

I would also like to add another small detail here that also might be causing the problem:

The requirements are such that we have to print the first page from Tray 3 (Letterhead) and the rest of the pages from Tray 4. Now the codes for these trays comes out different for different machines (different drivers, Win NT, Win 2000 platforms). So we are dynamically determining the codes by using windows API calls each time we print. Here is what we are doing:

1) Set aside the default printer of the user.
2) Set this central printer as the user's default.
3) Determine and set the tray codes for him.
4) Execute doc.printout.
5) Reset the default printer.

Also, the same routine is being used by other modules, too, that are running absolutely fine.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6888414
Could you show the code to save and close the document?

CJ
0
 

Author Comment

by:Fundu
ID: 6888421
I will post it from my office in a couple of hours.
0
 

Author Comment

by:Fundu
ID: 6888789
Here is another small detail that I just came to know about. The problem happens only from win2000 machines. NT is fine.
0
 

Author Comment

by:Fundu
ID: 6888971
CJ,

Here is part of the code:

Public gWordObj As Object

Dim objDoc As Word.Document

fResult = CheckObject("Word.Application")

Set objDoc = gWordObj.Documents.Open(strFileName)
                       
'Put a delay to give time for the file to open
For intDelayI = 1 To 100000
    For intDelayJ = 1 To 500
    Next intDelayJ
Next intDelayI

'This routine does all the tray setting stuff and print
hResult = DoPrintRoutine(objDoc, gstr_SQLConnectStr, 1, True, Printer2)

objDoc.Close SaveChanges:=wdDoNotSaveChanges
Set objDoc = Nothing

'Delay to let word finish doing what it's doing
For intDelayI = 1 To 100000
    For intDelayJ = 1 To 500
    Next intDelayJ
Next intDelayI

gWordObj.Quit
Set gWordObj = Nothing
0
 
LVL 22

Expert Comment

by:cookre
ID: 6889104
That method of delaying is, uh, unreliable at best.

If VB has no sleep/delay/pause function, then you should use a timer.

Not only is that method of looping extraordinarily inconsistent, it hogs the CPU.  A real sleep/wait/delay function, or a timer, allows other things to take place while the subject process or thread is in dreamland.
0
 

Author Comment

by:Fundu
ID: 6889142
Cookre,

The asynchronous operation looks like one of the possible culprits here. We have noticed something that suggests of the same. We have noticed that the file itself shows up corrupt when the printing is corrupt. How do I do a synchronous operation, though. Does word provide some functionality for that?
0
 

Author Comment

by:Fundu
ID: 6889500
The problem wasn't during the time of loading, but had to do with waiting until the printing is over.
0
 
LVL 22

Expert Comment

by:cookre
ID: 6890181
Did you use something like this for the wait?

do while obj.BackgroundPrintingStatus>0
   DoEvents()
   end

Not being a VB programmer (but having the documentation), I trust this is reasonably close to reality.
0
 

Author Comment

by:Fundu
ID: 6890216
No, I set the background printing option to false, so word automatically waits until the the spooling is over.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question