Saving Word pages as thumbnails fails after first page

N

NickP

Hi there,

I had written this in another thread but I guess the title doesn't
help...

I have a class below that is supposed to get the EMF bits from each page
of a Word document, unfortunately
it failes after the first page, but not *all* the time. I have had exactly
the same code working on more than one occasion.

------------

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.
 
C

Cindy M.

Hi NickP,

Can you discern any pattern, when it works and when not? You are opening in a
Word application window, and not in any kind of embedded object, I assume. And
you make sure the app window is visible? Does the View make a difference? Or
what's on the pages? How about if you first "GoTo" each page so that it's
surely on the screen?

Ah, wait a minute. I see:

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

If you make the document Visible, does that help? Word relies on physically
laying out the document on-screen for many things. I'd be surprised if "taking
a picture" would work if Word can't lay things out...
I had written this in another thread but I guess the title doesn't
help...

I have a class below that is supposed to get the EMF bits from each page
of a Word document, unfortunately
it failes after the first page, but not *all* the time. I have had exactly
the same code working on more than one occasion.

------------

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.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
N

NickP

Hi Cindy,

Unfortunately I have to be performing this process in the background,
hidden from the user as otherwise the program would look very
inprofessional.

The thing is that I have always had visibility set to false and
sometimes it has worked. I'll try going to the page before I thumbnail it
and see what happens then, although I think that it telling me that I am
accessing a member that isn't there is a bit strange. It's essentually
telling me that the page isn't there even though in this particular
instance, I have 19 pages...

I'll mess about with the settings you suggest and see what happens :)

Thanks for your time and help.

Nick.
 
C

Cindy M.

Hi NickP,
The thing is that I have always had visibility set to false and
sometimes it has worked. I'll try going to the page before I thumbnail it
and see what happens then, although I think that it telling me that I am
accessing a member that isn't there is a bit strange. It's essentually
telling me that the page isn't there even though in this particular
instance, I have 19 pages...
If Word for some reason hasn't been able to repaginate, then it will, indeed,
not "have" this page. You could try forcing repagination, see if that makes
any difference.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
N

NickP

Hi Cindy,

Thanks for your help, it apppears that you were correct with saying that
word has to be visible in order to thumbnail the pages correctly. Which is
a shame as it would be great to be able to do this while word is hidden.

I've got a temporary solution which makes the application visible during
thumbnailing and then hides it afterwards. Cheers for your time, it's most
appreciated.

Nick.
 
Top