Solved

Garbage printing problem with VB6.0 and MS Word objects

Posted on 2002-03-21
13
296 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
  • 8
  • 3
  • 2
13 Comments
 
LVL 22

Accepted Solution

by:
cookre earned 300 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

860 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