473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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/For...b-net/200601/1
Jan 16 '06 #1
3 30722
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...
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/For...b-net/200601/1

Jan 16 '06 #2
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:
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


--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...b-net/200601/1
Jan 16 '06 #3

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

Feb 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: KnotKnormal | last post by:
I would like to dynamically load a HTML page (or a Word document), which is embedded in a table when the user clicks on a hyperlink to go from HTML page one to HTML page two. For example, I would...
1
by: JSL | last post by:
How to insert images into a Microsoft Word document using VB.NET?? my code: Public Function PictureReport() (it doesn't work!!!) On Error Resume Next Dim oDoc As Word.Document Dim range As...
3
by: Lord0 | last post by:
I *think* I need to be able to validate subsets of an XML document using different schema. The functionality I'm trying to implement is this. a) External suppliers produce an XML document...
2
by: Andy | last post by:
Hi, I have an XML document that uses namespaces (it is from a Word 2007 file). I want to retrieve all the "t" elements that belong to the "w" namespace (<w:t>) using XPath from VB.NET 2003 (.NET...
0
by: Andy Fish | last post by:
Hi, I have a web application and I would like to serve the user a word document and have word open it up using webdav. I have read kb 838028 about office protocol discovery, but this only...
0
by: shivapadma | last post by:
i want to know how multiple tables are added to the MS word document using vb.net i wrote following code to insert one table in MS word document using vb.net 1.opened MS word document 2.inserted...
0
by: deegeorge | last post by:
Hi, can i insert a word document using include directive in asp.net. Can u please give a simple example code Thanks in advance
6
by: Paul Mc | last post by:
Hi all, It's a little late in the day for me so please forgive as i need my bed.!! The issue is i need to open a word doc (say "c:\temp.doc) and paste into it, but i only can work out how...
6
by: Sajeena | last post by:
<?php // starting word $text = "My Text"; //Start MS Word $Word = new COM("word.application") or die("Failure: Word did not start"); echo("WORD has started."); //Formating the Font...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.