Random Exceptions when Automating Word

G

geekgrrl

I am automating Word in VB.NET (VS2008). I have added a reference to
the Word 11.0 COM object (early binding) and to another in-house
object.

My application is using a background worker thread to loop through a
directory of word documents, open and then print them to a specific
printer.

While debugging, I am randomly getting TYPE_E_LIBNOTREGISTERED
exceptions on various methods that just worked, i.e sometimes I get it
on Word.Open(...) and sometimes when I use the FilePrintSetup to set
the default printer, and also sometimes, but not as often, I get the
same message from the other COM object I am using.

This is the message I get:
"Library not registered. (Exception from HRESULT: 0x8002801D
(TYPE_E_LIBNOTREGISTERED))"} System.Exception a reference to the Word
object (11.0)

I know the library is registered, it just worked for the previous file
in my loop. Does anyone have any idea why I am getting this message?
I've included my code below.


Dim PrintSession As IPNPrintSession
Dim oWordApp As New Word.ApplicationClass
Dim oDoc As New Word.Document


' Initialize the Word object to minimize popups as much as possible
oWordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
oWordApp.AutomationSecurity =
Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable

'process each file
PrintSession = Nothing
For Each file_info As System.IO.FileInfo In file_infos
Dim wordStep As Integer
Dim printerName As String
Dim optMissing As Object

optMissing = System.Type.Missing

If bw.CancellationPending Then
e.Cancel = True
e.Result = 0
bw.ReportProgress(0, "Cancellation pending...")
Exit For
Else
' Open and print from word
Try
' open doc
bw.ReportProgress(0, "Open " + file_info.FullName)
wordStep = 0
'oDoc = oWordApp.Documents.Open
(FileName:=file_info.FullName, AddToRecentFiles:=False)
'Try passing all arguments
oDoc = oWordApp.Documents.Open(file_info.FullName,
optMissing, optMissing, _

optMissing, optMissing, optMissing, optMissing, _

optMissing, optMissing, optMissing, optMissing, _

optMissing, optMissing, optMissing, optMissing, optMissing)

PrintSession = Nothing
While PrintSession Is Nothing And bw.CancellationPending =
False
Try
SyncLock Session
' get a print session; 5 sec wait for
available , 1/4 sec wait for session to return to pool
bw.ReportProgress(0, "Getting new print
session...")
PrintSession = Session.NewPrintSession(5000,
250)
End SyncLock
Catch ex As Exception
bw.ReportProgress(0, "Failed to get PrinterSession
(" + ex.ToString() + ").")
Exit While
End Try
End While

If PrintSession Is Nothing Then
Exit For
End If

If bw.CancellationPending = True Then
Exit For
End If

' set active printer
' this is a dangerous action as it changes the system
default, not just the
' the sytem default. It also will not work when using the
Word object to
' print from multiple threads.
'oWordApp.ActivePrinter = PrintSession.PrinterName
wordStep = 1
printerName = PrintSession.PrinterName
With oWordApp.Dialogs
(Word.WdWordDialog.wdDialogFilePrintSetup)
.Printer = printerName
.DoNotSetAsSysDefault = True
.Execute()
End With

bw.ReportProgress(0, "Printing " + file_info.FullName)
wordStep = 2
oDoc.PrintOut(Background:=True) ' print in the background

' wait for Word to finish printing
bkgndPrintWait = 1
bw.ReportProgress(0, "Wait for background printing status
from Word...")
wordStep = 3
While ((oWordApp.BackgroundPrintingStatus <> 0) And
(bw.CancellationPending = False))
System.Threading.Thread.Sleep(250)
bw.ReportProgress(0, "Wait for background printing
status from Word (" & bkgndPrintWait.ToString & ")...")
bkgndPrintWait = bkgndPrintWait + 1
End While

If bw.CancellationPending = True Then
wordStep = 4
bw.ReportProgress(0, "Closing document")
oDoc.Close(SaveChanges:=False)
Exit For
End If


'Wait for the printSession to tell when the document is
finished spooling/printing/completed
'Need to wait for spooling at the very least
< code snippped for space>

' close document
bw.ReportProgress(0, "Closing document")
wordStep = 4
oDoc.Close(SaveChanges:=False)

Catch ex As Exception
bw.ReportProgress(0, "Exception printing from Word (" +
ex.ToString() + ").")
e.Result = 0

If Not PrintSession Is Nothing Then
PrintSession.Cancel()
End If

End Try

' release reference to com object
If Not PrintSession Is Nothing Then
Dm releasePrintSession As IPNPrintSession
releasePrintSession = PrintSession
PrintSession = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject
(releasePrintSession)
System.GC.Collect()
End If

End If ' cancel pending

bw.ReportProgress(1, "PROGSTEP")
Next file_info
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top