JoeShearn
08-01-2011, 06:43 AM
Hi,
I'm working on part of a project which involves turning Word documents in to HTML. I can just simply save the document as a web page to turn it in to HTML but I need the HTML to be aware of where pages begin and end within the word document.
To do this I have written a macro to add $PGSTRT$ at the start of each page and $PGEND$ at the end. I will then replace these strings with DIV tags after I have saved the document as HTML. The Macro currently only inserts $PGSTRT$ and it looks like this:
Dim pageCount As Integer
Dim pRange As Range
pageCount = ActiveDocument.Content.Information(wdActiveEndAdjustedPageNumber)
For counter = 1 To pageCount
On Error Resume Next
Selection.GoTo wdGoToPage, wdGoToNext, , counter
Set pRange = ActiveDocument.Bookmarks("\page").Range
pRange.InsertBefore "$PGSTRT$"
Selection.MoveRight , 8, wdExtend
Selection.Style = "ToBeShown"
Selection.Font.Bold = False
Selection.Font.Italic = False
Selection.Font.Hidden = True ' Need to hide it so that it doesn't offset the rest of the page
Debug.Print counter
Next
' Need to show it so that it is saved as HTML not ignored
ActiveDocument.Styles("ToBeShown").Font.Hidden = False
This works most of the time but if for instance a table spans multiple pages then it gets inserted in to one of the cells in the table. The Selection.MoveRight method also seems to select things like images that are next to the text that is meant to be selected.
Can anyone think of a better way of doing this?
Thanks,
Joe
I'm working on part of a project which involves turning Word documents in to HTML. I can just simply save the document as a web page to turn it in to HTML but I need the HTML to be aware of where pages begin and end within the word document.
To do this I have written a macro to add $PGSTRT$ at the start of each page and $PGEND$ at the end. I will then replace these strings with DIV tags after I have saved the document as HTML. The Macro currently only inserts $PGSTRT$ and it looks like this:
Dim pageCount As Integer
Dim pRange As Range
pageCount = ActiveDocument.Content.Information(wdActiveEndAdjustedPageNumber)
For counter = 1 To pageCount
On Error Resume Next
Selection.GoTo wdGoToPage, wdGoToNext, , counter
Set pRange = ActiveDocument.Bookmarks("\page").Range
pRange.InsertBefore "$PGSTRT$"
Selection.MoveRight , 8, wdExtend
Selection.Style = "ToBeShown"
Selection.Font.Bold = False
Selection.Font.Italic = False
Selection.Font.Hidden = True ' Need to hide it so that it doesn't offset the rest of the page
Debug.Print counter
Next
' Need to show it so that it is saved as HTML not ignored
ActiveDocument.Styles("ToBeShown").Font.Hidden = False
This works most of the time but if for instance a table spans multiple pages then it gets inserted in to one of the cells in the table. The Selection.MoveRight method also seems to select things like images that are next to the text that is meant to be selected.
Can anyone think of a better way of doing this?
Thanks,
Joe