Word.Interop Print to pcl file.
I'm working on an application which automatically generates faxes. The system uses word interop to fill a template .doc file with values (which works fine), but when it comes to be time to print the resulting file to pcl, I'm having issues.
So, we've got a HP Laserjet printer set up which prints .pcl files.
Dim appWord As New Word.Application
Dim doc As New Word.Document
appWord.ActivePrinter = PCL_PRINTER
doc = appWord.Documents.Open(APPLICATION_DIR & "LTL_" & n.Language & ".doc")
... (Fill the file with values)
outFile = APPLICATION_DIR & "Faxout\DROPDIR\" & n.Order & ".pcl"
doc.PrintOut(True, True, , outFile, , , , , , , True)
'cleanup...
Problem is that when the doc.PrintOut line runs, word complains, popping up a message:
I've tried all sorts of combinations of things, but the error message persists. Very occasionally, the thing actually works and generates a usable pcl file, but 98% of the time, the error message pops 开发者_StackOverflowup and the whole process grinds to a halt.
We have other applications on the same server which do almost exactly the same thing, except they print from excel instead of word, and they work without throwing the error.
Can anyone help?
Don't know if this helps, but do you have an antivirus software on that machine? If so, did you try to disable it? There are sometimes issues with virus scanners accessing files in background, blocking access or deletion of temporary files.
I just figured it out. Apparently you have to create the file before you attempt to write to it.
eg:
Dim fs As FileStream
fs = System.IO.File.Create(outFile)
fs.Close()
fs = Nothing
doc.PrintOut(True, True, , outFile, , , , , , , True)
I'd tried creating the file earlier, but didn't realize that Create returned a filestream which needed to be closed before word would print out to it.
Is there any way to create a file in vb.net that doesn't create a lock on a file with a filestream object? This version works, but it'd be nice to clean up the code a bit.
精彩评论