Solved

Garbage printing problem with VB6.0 and MS Word objects

Posted on 2002-03-21
13
291 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This is about my first experience with programming Arduino.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now