I had a problem before extracting pages from an existing word document and
then inserting the content into a new word document.
The following code below works with Microsoft Word 2000
Function ParseWordDoc(ByVal Filename As String) As String
Dim sNewFileName As String
Dim WordApp As Word.Application = New Word.Application
Dim BaseDoc As Word.Document
Dim DestDoc As Word.Document
Try
Dim intNumberOfPages As Integer
Dim intNumberOfChars As String
Dim intPage As Integer
'Word Constants
Const wdGoToPage = 1
Const wdStory = 6
Const wdExtend = 1
Const wdCharacter = 1
'Show WordApp
WordApp.ShowMe()
'Load Base Document
BaseDoc = WordApp.Documents.Open(Filename)
BaseDoc.Repaginate()
'Loop through pages
intNumberOfPages = BaseDoc.BuiltInDocumentProperties("Number of
Pages").Value
intNumberOfChars = BaseDoc.BuiltInDocumentProperties("Number of
Characters").Value
BaseDoc.ShowRevisions = False
If intNumberOfPages > 1 Then
For intPage = 1 To intNumberOfPages
If intPage = intNumberOfPages Then
WordApp.Selection.EndKey(wdStory)
Else
WordApp.Selection.GoTo(What:=Word.WdGoToItem.
wdGoToPage, _
Which:=Word.WdGoToDirection.wdGoToAbsolute, _
Count:=2)
Application.DoEvents()
WordApp.Selection.MoveLeft(Unit:=wdCharacter, Count:
=1)
End If
Application.DoEvents()
WordApp.Selection.HomeKey(wdStory, wdExtend)
Application.DoEvents()
WordApp.Selection.Copy()
Application.DoEvents()
'Create New Document
DestDoc = WordApp.Documents.Add
DestDoc.Activate()
' set margins
If BaseDoc.PageSetup.TopMargin <= 1584 Then
DestDoc.PageSetup.TopMargin = BaseDoc.PageSetup.
TopMargin
End If
If BaseDoc.PageSetup.BottomMargin <= 1584 Then
DestDoc.PageSetup.BottomMargin = BaseDoc.PageSetup.
BottomMargin
End If
If BaseDoc.PageSetup.LeftMargin <= 1584 Then
DestDoc.PageSetup.LeftMargin = BaseDoc.PageSetup.
LeftMargin
End If
If BaseDoc.PageSetup.RightMargin <= 1584 Then
DestDoc.PageSetup.RightMargin = BaseDoc.PageSetup.
RightMargin
End If
If Not BaseDoc.PageSetup.Orientation = Word.WdOrientation.
wdOrientLandscape And Not BaseDoc.PageSetup.Orientation = Word.WdOrientation.
wdOrientPortrait Then
DestDoc.PageSetup.Orientation = WordApp.Selection.
Orientation
Else
DestDoc.PageSetup.Orientation = BaseDoc.PageSetup.
Orientation
End If
DestDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4
DestDoc.PageSetup.PageHeight = BaseDoc.PageSetup.
PageHeight
DestDoc.PageSetup.PageWidth = BaseDoc.PageSetup.PageWidth
DestDoc.ShowRevisions = False
' paste selecton
WordApp.Selection.Paste()
sNewFileName = Filename
sNewFileName = sNewFileName.Replace(".doc", "_Page" &
intPage.ToString & ".doc")
DestDoc.SaveAs(sNewFileName)
DestDoc.Close()
DestDoc = Nothing
WordApp.Selection.GoTo(wdGoToPage, 2)
Application.DoEvents()
WordApp.Selection.HomeKey(wdStory, wdExtend)
Application.DoEvents()
WordApp.Selection.Delete()
Application.DoEvents()
Next
BaseDoc.Close(False)
BaseDoc = Nothing
Else
BaseDoc.Close(False)
BaseDoc = Nothing
Return "OK"
End If
Return "OK"
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
WordApp.Quit()
WordApp = Nothing
End Try
End Function
However, there is a problem if I have a word document with numbered
paragraphs.
On Page 1 of the document has paragraphs numbered from 1 to 2.9
On Page 2 of the document has paragraphs numbered from 2.10 to 2.16
After running the page extraction sample, the Page 2 document now has
numbered paragraphs from 1 to 1.7 because Word is changing the paragraph
numbering to start from 1 rather than maintain the initial value from where
the numbered paragraphs were copied from.
Is there a method within Word Interop for VB.NET to override the value of the
paragraph number in a new document to take the initial value of what it is
copied from a previous document.
Regards
Adam Faulkner
Croner Software
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...b-net/200601/1