Home All Groups Group Topic Archive Search About

How do I extract a page from word and insert into a new word document using VB

Author
16 Jan 2006 11:24 AM
Adam Faulkner via DotNetMonster.com
I want to create a method within a class that opens a Microsoft Word 2000
Document and has the facility to Create a new word document and then extract
a Page that exists within the original Word Document and save it to a new
Word Document.

I would need to generate a loop for each page found within a word document to
create a new word document and insert the existing page into the new word
document and then save as a new word document.

Any recommendations would be appreciated.

Regards
Adam Faulkner
Croner Software


Author
16 Jan 2006 4:11 PM
Jay Taplin
Adam -

Here is some code you can use.  I'm not terribly impressed with it, but it
works.  Call the routine with:

        ParseWordDoc(SourceFilename, DestinationFilename)

Where SourceFilename is the file you are going to extract pages from, and
DestinationFilename is the the base filename you are going to create; for
instance, "Page" would create Page1.doc, Page2.doc, Page3.doc, etc.

The actual code to do the work:

    Private Sub ParseWordDoc(ByVal Filename As String, ByVal NewFileName As
String)
        Dim WordApp As Microsoft.Office.Interop.Word.Application = New
Microsoft.Office.Interop.Word.Application
        Dim BaseDoc As Microsoft.Office.Interop.Word.Document
        Dim DestDoc As Microsoft.Office.Interop.Word.Document

        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

        For intPage = 1 To intNumberOfPages
            If intPage = intNumberOfPages Then
                WordApp.Selection.EndKey(wdStory)
            Else
                WordApp.Selection.GoTo(wdGoToPage, 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()
            WordApp.Selection.Paste()
            DestDoc.SaveAs(NewFileName & intPage.ToString & ".doc")
            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

        WordApp.Quit()
        WordApp = Nothing
    End Sub
End Class

"Adam Faulkner via DotNetMonster.com" <u3667@uwe> wrote in message
news:5a709aa6d8961@uwe...
Show quoteHide quote
>I want to create a method within a class that opens a Microsoft Word 2000
> Document and has the facility to Create a new word document and then
> extract
> a Page that exists within the original Word Document and save it to a new
> Word Document.
>
> I would need to generate a loop for each page found within a word document
> to
> create a new word document and insert the existing page into the new word
> document and then save as a new word document.
>
> Any recommendations would be appreciated.
>
> Regards
> Adam Faulkner
> Croner Software
>
> --
> Message posted via DotNetMonster.com
> http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-vb-net/200601/1
Author
16 Jan 2006 5:14 PM
Adam Faulkner via DotNetMonster.com
Jay

The code has provided a great step toward what we want to achieve, however is
it possible on the selection method of word to include headers and footers of
the page when the selection is copied.

Regards
Adam Faulkner
Croner Software

Jay Taplin wrote:
Show quoteHide quote
>Adam -
>
>Here is some code you can use.  I'm not terribly impressed with it, but it
>works.  Call the routine with:
>
>        ParseWordDoc(SourceFilename, DestinationFilename)
>
>Where SourceFilename is the file you are going to extract pages from, and
>DestinationFilename is the the base filename you are going to create; for
>instance, "Page" would create Page1.doc, Page2.doc, Page3.doc, etc.
>
>The actual code to do the work:
>
>    Private Sub ParseWordDoc(ByVal Filename As String, ByVal NewFileName As
>String)
>        Dim WordApp As Microsoft.Office.Interop.Word.Application = New
>Microsoft.Office.Interop.Word.Application
>        Dim BaseDoc As Microsoft.Office.Interop.Word.Document
>        Dim DestDoc As Microsoft.Office.Interop.Word.Document
>
>        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
>
>        For intPage = 1 To intNumberOfPages
>            If intPage = intNumberOfPages Then
>                WordApp.Selection.EndKey(wdStory)
>            Else
>                WordApp.Selection.GoTo(wdGoToPage, 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()
>            WordApp.Selection.Paste()
>            DestDoc.SaveAs(NewFileName & intPage.ToString & ".doc")
>            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
>
>        WordApp.Quit()
>        WordApp = Nothing
>    End Sub
>End Class
>
>>I want to create a method within a class that opens a Microsoft Word 2000
>> Document and has the facility to Create a new word document and then
>[quoted text clipped - 12 lines]
>> Adam Faulkner
>> Croner Software

Author
26 Feb 2006 2:28 AM
Homer J Simpson
"Adam Faulkner via DotNetMonster.com" <u3667@uwe> wrote in message
news:5a73a79e41ad1@uwe...
> Jay
>
> The code has provided a great step toward what we want to achieve, however
> is
> it possible on the selection method of word to include headers and footers
> of
> the page when the selection is copied.

Something like

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader ' (or
Footer)
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Copy

may do it. Note that headers and footers extend over multiple pages.