Is Word COM Automation *extremely* flakey, or is it just me?

N

NickP

Hi there,

I have a class that basically enumerates the pages of a Word document
and creates thumbnails from them. It does not always work, and the error
message does not make any sense...

Clip...

ImageStream = CreateObject("ADODB.Stream")
With ImageStream
.Type = 1
.Open()
Dim pObjTemp As Object =
cObjDocument.ActiveWindow.Panes(1).Pages(iIndex).EnhMetaFileBits
.Write(pObjTemp)
.SaveToFile("c:\test.emf")
.Close()
ImageStream = Nothing
End With

Every so often it fails on the 5th line. I recieve the following
error...

http://msdn2.microsoft.com/en-us/library/aa211944(office.11).aspx

Strange thing is, without even changing the code, and a few attempts
later it works! So I added the following 2 lines...

Dim pIntPaneCount As Integer = cObjDocument.ActiveWindow.Panes.Count()
Dim pIntPageCount As Integer =
cObjDocument.ActiveWindow.Panes(1).Pages.Count()

That way the next time the error occured I could check the limits as
according to the error; I am outside of them, which (excuse my French, is
complete bull5h1t).

Crazy thing is, adding these 2 lines temporarily caused it to start
working again! Until now, with these 2 lines in, and my values without a
doubt within the limits, it has stopped working again, and no doubt it will
start again.

I have checked for running instances of Word, which sometimes helps if I
terminate them, especially if they aren't visible. But that doesn't always
help.

Has anyone got any idea what could be happening here?

Nick.
 
N

NickP

Here is a code sample that replicates my problem,


Imports Microsoft.Office.Interop.Word

Public Class PageThumbs

Public Shared Sub enumeratePageThumbs()
Dim opendialog As New OpenFileDialog()
If (opendialog.ShowDialog = DialogResult.OK) Then

Dim app As New Application
Dim doc As Document = app.Documents.Open(opendialog.FileName,
ReadOnly:=True, Visible:=False)

Dim failed As Boolean
Dim activepane As Pane = doc.ActiveWindow.Panes.Item(1)
Dim docpages As Pages = activepane.Pages
Dim pagecount As Integer = docpages.Count()

Dim curpage As Integer
For curpage = 1 To pagecount
Try
Console.WriteLine("Getting emf bits for page '" &
curpage.ToString & "/" & pagecount.ToString & "'.")

Dim emfbits = docpages.Item(curpage).EnhMetaFileBits
'<<This line doesn't always work for pages > 1

Console.WriteLine("Got bits for page.")
Catch ex As Exception
Console.WriteLine("Failed to get bits for page.")
failed = True
MessageBox.Show(ex.ToString)
End Try
Next

If (failed) Then
MessageBox.Show("Failed!")
Else
MessageBox.Show("Succeeded!")
End If

Call doc.Close()
Call app.Quit()

End If
End Sub

End Class

The strange thing is that page 1 *always* works! But the other pages
don't. Very strange, has anyone else experienced this?

BTW I am using Office 2007.

Nick.
 

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