Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find/Replace in first three sentences only (Word2000) 2

Status
Not open for further replies.

aarondewberry

IS-IT--Management
Jul 20, 2005
148
GB
Hi All

I have scrolled my way through many posts and can't seem to find what i'm looking for. Being a VBA novice doesn't help as I'm sure I could possibly adapt another post to what I want. Anyways...
What I am trying to do is find any commas (,) in the first 3 sentences in my word doc. I then want to replace them with nothing. I have this code, which works, but replaces the commas in the whole document.
I only want to replace the commas in the first 3 sentences, and leave the other commas in the sentences after.

Many thanks

Code:
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ","
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
 



Make the Selection, the first three sentences. Sentences are Collections, so its easy to define the first three.


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
1. Why do you want to even use VBA for this? This is what Find/Replace is for. As Skip suggests, just select the first three sentences and use Replace. VBA can do this...but why?

2. Further, do you really mean sentences, or are you dealing with paragraphs? It makes a differnce.

3. If you really do want to use VBA - and again, this seems overkill, but you do not state what the situation is - then it would be better to set a range, rather than Selection.

Skip is not quite correct. Sentences is NOT (by itself) a Collection. There is no Sentence object. Sentences are a collection of ranges.
Code:
Dim r As Range
Dim j As Long

For j = 1 To 3
    Set r = ActiveDocument.Sentences(j)
    With r.Find
        .Text = ","
        .Replacement.Text = ""
        .Execute Replace:=wdReplaceAll
    End With
Next
replaces all commas with nothing in the first three sentences in the document. Although (like many things with VBA) there are other ways to go about this.

faq219-2884

Gerry
My paintings and sculpture
 




I am NEVER quite correct in MS Word, as Gerry deftly suggests. I defer to the guru.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Never? No Skip, that is not correct....oh....

Come on, I have seen excellent posts from you on Word.

I just like to kneedle Skip, as I get few chances. He is quite capable of giving correct Word answers. He simply has an Excel-centric view regarding object models. Which, quite frankly, is understandable. The Word object model can be...ummmm, odd.

faq219-2884

Gerry
My paintings and sculpture
 
Gerry, you talked about the Word Oddject model ?
 
you guys crack me up

Although, let us face it. ALL Office Application Object Models have their odd points. However, from what I understand (and I most certainly do NOT understand the Excel object model), most people seem to find it a little more sane than Word.

The issue with Word, IMO, is the basic concept that Microsoft has regarding what is a "document". Further, that this thing (a "document") is a continuum; there is an inherent conceptual fuzziness. The object model itself is built on that conceptual fuzziness. In other words, they built the model in order to get that fuzziness.

Of course, from Microsoft's POV, it is not "fuzziness", it is "feature-rich".

Excel has cells. A cell is a cell is a cell. The basic concept is fixed; it is not fuzzy. A cell is a cell is a cell. Sure, there are lots of things you can do with cells (and their contents), but the structure is consistent.

faq219-2884

Gerry
My paintings and sculpture
 
Just as a laugh, here is some code with legitimate and correct syntax.
Code:
Sub Ridiculous_Syntax()
MsgBox ActiveDocument.[b]Range[/b] _
   .Sections(2).[b]Range[/b] _
   .Bookmarks("Whatever") _
   .[b]Range[/b].FormFields(1) _
   .[b]Range[/b].Bookmarks("NewStuff") _
   .[b]Range[/b].Text
End Sub
Multiple levels of Range, across five different objects. The text of a Range within a Bookmark Range, within a Formfield Range, within a (bigger) Bookmark Range, within a Section Range, within the document Range.

It is no wonder that people get confused by the use of ranges in Word.

Yes, absolutely, it is very possible (and of course better) to get the same result (the string) with smaller code. However, it is important to understand that Word "thinks" of things this way. IMO, other than really grasping what a paragraph mark is, there is nothing more crucial to using Word VBA than getting a grip on what the heck a range means.

Oddly enough, it is actually very very simple.

faq219-2884

Gerry
My paintings and sculpture
 
If you say so.

_________________
Bob Rashkin
 




I am waaaaaaaay short of "simple," obbly enough, if you don't oddject!!!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
What? That it is simple? OK, I admit it, I was being facetious. In the Word VBA course I teach, I spend a full day just on Range.

A Range is simply everything in the document between two numbers (expressed as Long) - a Start and an End. The numbers represent the number of characters from the start of the document.

Some basic rules:

[ol][li]Document.Range.Start = 0 (not 1).
[/li]
[li]ALL Ranges have those two numbers, Start and End.
[/li]
[li]Start may equal End, in which case the range covers ZERO characters, i.e. Range.Text = "".
[/li]
[li]Corollary: even though ZERO characters are covered by the Range, Range.Character.Count can never = 0. A collapsed Range (Start = End) will still have a Character.Count = 1.
[/li]
[li]Corollary: a single character Range has End = Start + 1.
[/li]
[li]Tables_1: assumpting NO text in cells (cell is "empty"), each cell.range has a Character.Count = 1; End = Start + 1 (e.g. Start:= 6, End:=7). This is the Range of the end-of-cell marker - a paragraph mark.
[/li]
[li]Tables_2: each Row has an end-of-row marker (also considered a paragraph mark, and is also a Range value.
[/li]
[li]InlineShapes (not matter what size they are) have a Range of 1. Insert > Picture increases the Range by 1.
[/li]
[li]HOWEVER, an ActiveX control (Controls toolbar) - even though it is an InlineShape - increases the Range by the field code character count, whether it is toggled visible or not.
E.g. { CONTROL Forms.TextBox.1 \s }
[/li]
[li]ActiveX contents in a TextBox are NOT counted as characters in the Range values. In other words, the characters of { CONTROL Forms.TextBox.1 \s } ARE counted, but the text, say:

yadda yadda yadda, blah blah blah blah blah

INSIDE the ActiveX control are NOT counted/included in the Range values.
[/li]
[li]Formfield contents ARE counted as characters in the Range values. Further, the Range values for the characters FORMTEXT are included.
[/li]
[li]Range.Find resizes the original given range to any .Found.
[/li]
[/ol]

Range values are VERY weird regarding Fields. The values change depending on whether the field codes are visible, or not. I have spend hours pouring over the Start/End values of Range when involving text FormFields. I still do not understand it. Formfield Checkboxes and Dropdowns numbers come out right, but somehow text formfields have 7 extra numbers. Why? Who the heck knows.

Take a blank document, and put in a text formfield, nothing else. It should shows as the default 5 character length.

Select it. using the Selection:

Range.Start = 0 (makes sense, there is nothing else in the doc)
Range.Character.Count = 5 (makes sense, the 5 default characters)
Range.End = 19 HUH???!!!

So, OK, toggle (Alt-F9). It shows as:

{ FORMTEXT } which counts outs as 12 characters. And indeed it comes out correctly.

Range.Start = 0
Range.Character.Count = 12
Range.End = 19

But where oh where are the extra 7 characters between Start and End? I have no bloody idea. It does NOT do this with
{ FORMCHECKBOX }, or { FORMDROPDOWN }.

....they have 1 extra, not 7.

And there ya go. Simple.

faq219-2884

Gerry
My paintings and sculpture
 




I just wrote your name in, Gerry!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top