PDA

View Full Version : Try to find the end of ducument



enhung
08-15-2009, 12:55 PM
I am new and still learning.

Trying to write a do loop but failed.

Problem is to find certain text string, find and ask for delete.

I try to check the End of file while loop,

but EOF(argument) is wrong, and I don''t know how to check the current document.

In short, my porblem is how to check the end of document of the current document.

Thanks.

CreganTur
08-17-2009, 06:44 AM
Welcome to the forum- always good to have new members.

Your question is extremely vague- we need more specific information to understand what's wrong.

You mention EOF- are you working with a recordset? Why do you say EOF is 'wrong?' How are you checking for EOF? Seeing the code would help tremendously.

When you post code, be sure to use VBA tags- click the green VBA button. This will format your code according to VBIDE, which makes it easier to read.

Thanks:thumb

Paul_Hossler
08-17-2009, 10:23 AM
Also, a small sample document would help.

However,it sounds like you want to replace some text in a document.

That's usually a .Find or a .Replace operation.

Paul

enhung
08-30-2009, 10:39 AM
I post the other day without my code, sorry.
I try to execute a macro at a opened document.
That macro help me to find certain words and transfer them into item and give them style. Here is what I did , kind of newbie code.
Actually I just want to check the end of file.


Public Sub FindNumberItem()
With Selection.Find
.Wrap = wdFindContinue
.Execute FindText:="X"
End With
If Selection.Text = "X" Then
Selection.MoveRight unit:=wdCharacter, Count:=1
End If
For i = 1 To 15
If Selection.Text = "Y" Then
Exit For
Else
End If
Selection.MoveRight unit:=wdCharacter, Count:=1
Next i
Selection.MoveRight unit:=wdCharacter, Count:=1
Selection.MoveRight unit:=wdCharacter, Count:=1
If Selection.Text = " " Then
Selection.MoveRight unit:=wdCharacter, Count:=1
Selection.TypeParagraph
End If
Selection.MoveUp unit:=wdLine, Count:=1
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.Range.Paragraphs.OutlineLevel = wdOutlineLevel5
Selection.Style = ActiveDocument.Styles("ZZ 5")
Selection.MoveDown unit:=wdLine, Count:=2
End Sub

EDIT: Added VBA Tags for readability Tommy

joms
09-02-2009, 01:41 AM
check out this link:
http://vbadud.blogspot.com/2008/03/identify-end-of-document-using-word-vba.html

according to that link above, they created a bookmark to check the end of the document.. so maybe you can use that too... if that's the one you're trying to achieve.. :)

macropod
09-02-2009, 05:49 AM
Hi enhung,

If I understand your code correctly, it's trying to find an 'X', followed by up to 14 characters then a 'Y', then another character and a space. Then replace the space with a paragraph break and format with paragraph in which the string was found in a certain way. If so, try the following code:
Sub Demo()
With Selection.Find
.ClearFormatting
.Text = "(X[\!-XZ-~]{1,14}Y?) "
With .Replacement
.ClearFormatting
.Text = "\1^p"
.Style = ActiveDocument.Styles("Action")
.ParagraphFormat.OutlineLevel = wdOutlineLevel5
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End SubThe above tests for any printable characters from the lower 127 ASCII set. If the permissible characters are a subset of that, or different in some other way, the code can be modified to suit.

macropod
09-03-2009, 12:21 AM
Hi enhung,
Oops: replace 'Action' with 'ZZ 5'
Similarly, you might want to use .MatchCase = True instead of .MatchCase = False

Also, for a simpler expression to handle the characters between the 'X' and 'Y', you could replace "(X[\!-XZ-~]{1,14}Y?) " with "?(X[!Y]{1,14}Y?) ".

enhung
09-06-2009, 06:20 PM
Well, my post the other day with code doesn't help. Newbie, sorry.

Well, I simply try to find certain words in a specific form. for instance, chaper 1 ,chapter 2, chapter 3, section 1, section.....................

I can't use selection, find and replace them all, becuase I trigger this macro in an opened document, each time I find it, i need time to look at it, and decide what to do with it.

Thus, I simply need some funtion to check the end of document, I've heard EOF and bookmark checking. But I figure there must be better way to do this.

I am willing to study document, can anyone give me a clue ? May be a method or funtion, or property.

Thanks.

macropod
09-06-2009, 06:52 PM
Hi enhung,

You should keep posts on the same topic in the original thread; otherwise no-one who's already replied will be notified that you've responded.

Using Find/Replace to find Chapter 1, Chapter 2, Chapter 3, etc or Section 1, Section 2, Section 3, etc is quite possible - and simple. Likwise, running such a process upon opening a document isn't proplematic either.

Perhaps you could explain what you're trying to do in more detail, rather than assuming it has to be done in a particular way.

FWIW, you can find the end of a document in a variety of ways. The most appropriate way depends on the context in which you're doing it.

enhung
09-06-2009, 07:24 PM
Thank you Macropod.

I manage to find the specific words like section 1 ,2 and etc.

However, I have problem to find those words and check the end of file.

I know there could be ways of doing it, I you could kindly give me a clue, like any method or property to use, then I can study on that.

Thank you.

macropod
09-06-2009, 11:07 PM
Hi enhung,

OK, here's some code but, without knowing what you're trying to do, I have no way of knowing if it'll be useful:
Sub Test()
Dim oRng As Range
Set oRng = Selection.Range
oRng.End = ActiveDocument.StoryRanges(wdMainTextStory).End
MsgBox Selection.Range.End = oRng.End
End SubThe above code tests whether the last character in the document (which can only be a paragraph break) is selected.

I'll reiterate my previous advice also: You can find any given block of text in a document without having to know where the end of the document is, using Find. To find the last instance, if that's what you're after, just tell Word to search backwards through the document.

lucas
09-07-2009, 08:04 AM
All three threads merged.

enhung, please keep all questions on a single topic in one thread.

Tinbendr
09-09-2009, 01:24 PM
Well, I simply try to find certain words in a specific form. for instance, chaper 1 ,chapter 2, chapter 3, section 1, section...Won't the standard Find (Ctrl-F) accomplish this?


.., find and replace them all, becuase I trigger this macro in an opened document, each time I find it, i need time to look at it, and decide what to do with it.It's still unclear what you are trying to accomplish. First you're talking about finding each 'section', then finding and replacing all of them. If you're talking about stopping at each find and editing the text in some manner, then continuing the macro from the breakpoint, then sounds like CTRL-F to me.


Thus, I simply need some function to check the end of document...
I've heard EOF
You don't use EOF in Word on a Word document, you use ranges. (Well, I guess you could, but why would you. That would be trying to do it the VERY, VERY hard way.)

Until we can understand the WHOLE process you're trying to describe, EOD or EOF is the probably the least of the solution.

Can provide a sample doc, and explain in greater detail what you're trying to accomplish.

fumei
09-10-2009, 10:31 AM
Sorry, but IMO, specifically finding, or locating, the end of the document is 99% of the time utterly irrelevant.

Why?

Because 99% of the time you are searching for something speciific - a string, or Sections, or tables, or whatever.

NOT the "end of the document".

What that means logically is: "Look for X (string, Section, table...whatever) until you can not find any more."

Where in that statement is anything - specifically, explicitly - about the end of the document?

No where.

Therefore, for the most part, using things like EOF, or the defined bookmark, is a waste of code. Properly done, a search will do its search to the end of the document automatically, without ever having to explicitly know (or point to) the end.

Sub YaddaYadda()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
Do While .Execute(FindText:="Section", Forward:=True) _
= True
' do whatever it you need to do
Loop
End With
End Sub
That will look for "Section" - and whatever it is you are doing to every .Found "Section" - right up to the end of the document. No need to define that end.

The beauty of using a range object is you can define the range. In the above, it is the whole document. You could just as easily execute code for ANY range. All you have to do is define it.

Say your document has 15 Sections, including Section1 being the title page, Section2 being the Table of Contents, Section3 being a Summary, Section14 being a Glossary and Section15 an Index. Whatever.

You want to search for stuff in Sections 4 to 13, skipping the first three (and the last two) as they are not relevant to the search. Simple. Define the range.
Sub YaddaYadda_2()
Dim r As Range
Set r = ActiveDocument.Range( _
Start:=ActiveDocument.Sections(4).Range.Start, _
End:=ActiveDocument.Sections(13).Range.End)
With r.Find
.ClearFormatting
Do While .Execute(FindText:="Section", Forward:=True) _
= True
' do whatever it you need to do
Loop
End With
End Sub
The range used is Section 4 (start) to Section13 (end).

Or say you only want to search tables.

Dim oTable As Table
Dim r As Range

For Each oTable In ActiveDocument.Tables()
Set r = oTable.Range
With r.Find
' etc. etc.
End With
That only searches tables. No determination of "end of document" is required.

macropod
09-10-2009, 03:01 PM
Hi fumei,

I fully agree. Unfortunately, enhung seems fixated on a particular method rather than the most logical one - and it seems enhung doesn't even want to share what it is he/she wants to achieve.

fumei
09-11-2009, 12:42 PM
Yes I agree. We have asked for clear understanding of what they want to do, but we are not getting a clear answer.

enhung, let's try again. You wrote:

"Problem is to find certain text string, find and ask for delete."

OK, this is easy and does not need to have anything to do with finding the end of the document. The following code searches for the text string "whatever", and asks if you want to delete THAT instance found. The following selects the range (something I rarely do) because that way you will be able to SEE the instance the messagebox is asking about.

Option Explicit

Sub AskMe()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(FindText:="whatever", _
Forward:=True) = True
r.Select
If MsgBox("Delete this????", vbYesNo) _
= vbYes Then
With r
.Delete
.Collapse 0
End With
End If
Loop
End With
End Sub
There, that does exactly what you asked about, and there is no end of document requirement in sight.