473,545 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.c om
http://www.dotnetmonster.com/Uwe/For...b-net/200601/1
Jan 16 '06 #1
3 30767
Adam -

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

ParseWordDoc(So urceFilename, DestinationFile name)

Where SourceFilename is the file you are going to extract pages from, and
DestinationFile name 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(By Val Filename As String, ByVal NewFileName As
String)
Dim WordApp As Microsoft.Offic e.Interop.Word. Application = New
Microsoft.Offic e.Interop.Word. Application
Dim BaseDoc As Microsoft.Offic e.Interop.Word. Document
Dim DestDoc As Microsoft.Offic e.Interop.Word. Document

Dim intNumberOfPage s As Integer
Dim intNumberOfChar s 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.Documen ts.Open(Filenam e)
BaseDoc.Repagin ate()

'Loop through pages
intNumberOfPage s = BaseDoc.BuiltIn DocumentPropert ies("Number of
Pages").value
intNumberOfChar s = BaseDoc.BuiltIn DocumentPropert ies("Number of
Characters").va lue

For intPage = 1 To intNumberOfPage s
If intPage = intNumberOfPage s Then
WordApp.Selecti on.EndKey(wdSto ry)
Else
WordApp.Selecti on.GoTo(wdGoToP age, 2)
Application.DoE vents()

WordApp.Selecti on.MoveLeft(Uni t:=wdCharacter, Count:=1)
End If

Application.DoE vents()

WordApp.Selecti on.HomeKey(wdSt ory, wdExtend)
Application.DoE vents()

WordApp.Selecti on.Copy()
Application.DoE vents()

'Create New Document
DestDoc = WordApp.Documen ts.Add
DestDoc.Activat e()
WordApp.Selecti on.Paste()
DestDoc.SaveAs( NewFileName & intPage.ToStrin g & ".doc")
DestDoc.Close()
DestDoc = Nothing

WordApp.Selecti on.GoTo(wdGoToP age, 2)
Application.DoE vents()

WordApp.Selecti on.HomeKey(wdSt ory, wdExtend)
Application.DoE vents()

WordApp.Selecti on.Delete()
Application.DoE vents()
Next

BaseDoc.Close(F alse)
BaseDoc = Nothing

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

"Adam Faulkner via DotNetMonster.c om" <u3667@uwe> wrote in message
news:5a709aa6d8 961@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.c om
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(So urceFilename, DestinationFile name)

Where SourceFilename is the file you are going to extract pages from, and
DestinationFil ename 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(By Val Filename As String, ByVal NewFileName As
String)
Dim WordApp As Microsoft.Offic e.Interop.Word. Application = New
Microsoft.Offi ce.Interop.Word .Application
Dim BaseDoc As Microsoft.Offic e.Interop.Word. Document
Dim DestDoc As Microsoft.Offic e.Interop.Word. Document

Dim intNumberOfPage s As Integer
Dim intNumberOfChar s 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.Documen ts.Open(Filenam e)
BaseDoc.Repagin ate()

'Loop through pages
intNumberOfPage s = BaseDoc.BuiltIn DocumentPropert ies("Number of
Pages").valu e
intNumberOfChar s = BaseDoc.BuiltIn DocumentPropert ies("Number of
Characters").v alue

For intPage = 1 To intNumberOfPage s
If intPage = intNumberOfPage s Then
WordApp.Selecti on.EndKey(wdSto ry)
Else
WordApp.Selecti on.GoTo(wdGoToP age, 2)
Application.DoE vents()

WordApp.Selecti on.MoveLeft(Uni t:=wdCharacter, Count:=1)
End If

Application.DoE vents()

WordApp.Selecti on.HomeKey(wdSt ory, wdExtend)
Application.DoE vents()

WordApp.Selecti on.Copy()
Application.DoE vents()

'Create New Document
DestDoc = WordApp.Documen ts.Add
DestDoc.Activat e()
WordApp.Selecti on.Paste()
DestDoc.SaveAs( NewFileName & intPage.ToStrin g & ".doc")
DestDoc.Close()
DestDoc = Nothing

WordApp.Selecti on.GoTo(wdGoToP age, 2)
Application.DoE vents()

WordApp.Selecti on.HomeKey(wdSt ory, wdExtend)
Application.DoE vents()

WordApp.Selecti on.Delete()
Application.DoE vents()
Next

BaseDoc.Close(F alse)
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.c om
http://www.dotnetmonster.com/Uwe/For...b-net/200601/1
Jan 16 '06 #3

"Adam Faulkner via DotNetMonster.c om" <u3667@uwe> wrote in message
news:5a73a79e41 ad1@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.Ac tivePane.View.S eekView = wdSeekCurrentPa geHeader ' (or
Footer)
Selection.EndKe y Unit:=wdLine, Extend:=wdExten d
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
1927
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 like the secretary at a school to update a Word document concerning homework assignments. This Word document would reside on the server. When changes...
1
5670
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 Word.Range
3
1745
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 containing multiple User records. The external supplier validates this entire document using schema 1. This document is then uploaded to our system. b)...
2
7833
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 framework 1.1). I've successfully loaded the document into a XmlDocument DOM parser (I can dump the contents using OuterXML). And, I've...
0
1516
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 seems to happen when the document is opened from inside office; if I just put a hyperlink to the word document in a web page, it seems to generate be a...
0
5256
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 one 3x3 table Dim App As New Word.Application() Dim Doc As Word.Document = App.Documents.Add() Dim oTable1 As Word.Table ...
0
1032
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
8176
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 to open a NEW doc ' Creates a new document and pastes Clipboard contents. ' Saves the document in the Word directory and closes...
6
4430
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 $Word->Visible=0;
0
7479
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7926
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7439
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.