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

Garbage printing problem with VB6.0 and MS Word objects

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
Fundu
Asked:
Fundu
  • 8
  • 3
  • 2
1 Solution
 
cookreCommented:
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
 
FunduAuthor Commented:
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
 
CJ_SCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
FunduAuthor Commented:
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
 
CJ_SCommented:
Could you show the code to save and close the document?

CJ
0
 
FunduAuthor Commented:
I will post it from my office in a couple of hours.
0
 
FunduAuthor Commented:
Here is another small detail that I just came to know about. The problem happens only from win2000 machines. NT is fine.
0
 
FunduAuthor Commented:
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
 
cookreCommented:
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
 
FunduAuthor Commented:
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
 
FunduAuthor Commented:
The problem wasn't during the time of loading, but had to do with waiting until the printing is over.
0
 
cookreCommented:
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
 
FunduAuthor Commented:
No, I set the background printing option to false, so word automatically waits until the the spooling is over.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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