473,396 Members | 1,865 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,396 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 30744
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.