Read word document text page-by-page

D

Dana

Hi

Is there any method by which I can read a word file from my VB application
page-by-page?

Currently, I can open the file and save the text as a text file, but what I
need is a way to know the page numbers, so I though instead of saving the
text as a text file, I might be able to read the text from the word file
page-by-page and add the page numbers then save the text to a text file.




Any ideas?


Dana
 
H

Helmut Weber

Hi Dana,

have a look at this one:

Sub test14()
Dim lPag As Long ' a page number
Dim sPag As String ' a string of a page
With ActiveDocument
For lPag = 1 To .BuiltInDocumentProperties(wdPropertyPages)
Selection.GoTo what:=wdGoToPage, _
which:=wdGoToAbsolute, Count:=lPag
sPag = .Bookmarks("\page").Range.Text
MsgBox Left(sPag, 12) ' for testing
Next
End With
End Sub

The use of a word constant like wdPropertyPages
requires early binding. For late binding from VB
you may use the number 14 instead.

It is assumed, that your doc contains nothing but text,
and no complex formatting, like autonumbering, bulleted lists etc.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Doug Robbins - Word MVP

Use a modification of the following:

Sub splitter()

'

' splitter Macro

' Macro created 16-08-98 by Doug Robbins to save each page of a document

' as a separate file with the name Page#.DOC

'

Dim Counter As Long, Source As Document, Target As Document

Set Source = ActiveDocument

Selection.HomeKey Unit:=wdStory

Pages = Source.BuiltInDocumentProperties(wdPropertyPages)

Counter = 0

While Counter < Pages

Counter = Counter + 1

DocName = "Page" & Format(Counter)

Source.Bookmarks("\Page").Range.Cut

Set Target = Documents.Add

Target.Range.Paste

Target.SaveAs FileName:=DocName

Target.Close

Wend

End Sub

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Dana

Great, this is what I really wanted.

However, I found a slight problem when closing the document. Previously, I
was able to close it without been asked whether to save the changes or not!!
although, I did not do any changes.

Here is the code:

Dim MyDoc As Word.Application
Set MyDoc = New Word.Application

MyDoc.Documents.Open filename:=filename, ConfirmConversions:=False,
ReadOnly:=False, _
AddToRecentFiles:=False, _
PasswordDocument:="sss", _
PasswordTemplate:="", _
Revert:=False, _
WritePasswordDocument:="", _
WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto


Dim lPag As Long ' a page number
Dim sPag As String ' a string of a page

With MyDoc.ActiveDocument
For lPag = 1 To .BuiltInDocumentProperties(wdPropertyPages)
MyDoc.Selection.GoTo wdGoToPage, wdGoToAbsolute, lPag
sPag = .Bookmarks("\page").Range.Text
Debug.Print sPag
Next
End With
MyDoc.Quit 'Here I am asked now whether to save??????
Set MyDoc = Nothing


Any clues?


Dana
 
H

Helmut Weber

Hi Dana,

first, don't call your word.application "MyDoc",
which is rather confusing.
Create a word-object and maybe a document-object in addition.

As to the message you get,
I don't know what causes this behavior,
maybe it's setting options when opening.
Anway, this usually helps:

oDoc.Saved = True
oDoc.Close savechanges:=wdDoNotSaveChanges

I came across cases, were
oDoc.Saved = True
was required in addition to
oDoc.Close savechanges:=wdDoNotSaveChanges

Don't know why either.

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top