> Alan B. Densky was telling us:
> Alan B. Densky nous racontait que :
>
>> Hi,
>>
>> I've got things working, sort of. But I have a problem. I tried the
>> advice in the bad find karma, but it had no effect.
>>
>> My macro works correctly and does what I want until it gets to the
>> label [Works Okay To Here]. The insertion point is at the top of the
>> section of the document where I want it. But when it executs the code
>> from that point, instead of selecting the range as defined in the
>> macro, it skips that range and goes on down the document and selects
>> the next range that matches the criteria. I've messed with the
>> .Forward=True in that part of the macro to see what would happen, but
>> it has no effect on it at all.
>> Any ideas?
>>
>> Alan
>>
>> 'Find the string that starts the links block of text and delete the
>> text on that line
>> Selection.Find.ClearFormatting
>> Selection.Find.Replacement.ClearFormatting
>> With Selection.Find
>> .Text = "LINKS SECTION======"
>> .Replacement.Text = ""
>> .Forward = True
>> .Wrap = wdFindAsk
>> .Format = False
>> .MatchCase = False
>> .MatchWholeWord = False
>> .MatchWildcards = False
>> .MatchSoundsLike = False
>> .MatchAllWordForms = False
>> End With
>> Selection.Find.Execute
>> With Selection
>> If .Find.Forward = True Then
>> .Collapse Direction:=wdCollapseStart
>> Else
>> .Collapse Direction:=wdCollapseEnd
>> End If
>> .Find.Execute Replace:=wdReplaceOne
>> If .Find.Forward = True Then
>> .Collapse Direction:=wdCollapseEnd
>> Else
>> .Collapse Direction:=wdCollapseStart
>> End If
>> .Find.Execute
>> End With
>>
>> 'Delete the block of text that doesn't have any links in it
>> Dim oRg As Range
>> Set oRg = ActiveDocument.Range
>> With oRg.Find
>> .ClearFormatting
>> .Replacement.ClearFormatting
>> .Text = "(NO LINKS SECTION======)(*)(END NO LINKS
>> SECTION======)" .Replacement.Text = ""
>> .Forward = True
>> .Format = False
>> .MatchWildcards = True
>> .Wrap = wdFindContinue
>> .Execute Replace:=Word.WdReplace.wdReplaceOne
>> End With
>>
>> 'Go back up and find the beginning of the article
>>
>> Selection.Find.ClearFormatting
>> With Selection.Find
>> .Text = "Article With Back links"
>> .Replacement.Text = ""
>> .Forward = False
>> .Wrap = wdFindAsk
>> .Format = False
>> .MatchCase = False
>> .MatchWholeWord = False
>> .MatchWildcards = False
>> .MatchSoundsLike = False
>> .MatchAllWordForms = False
>> End With
>> Selection.Find.Execute
>> Selection.MoveDown Unit:=wdLine, Count:=1
>>
>> [Works Okay To Here]
>>
>> 'Copy the selected range to the clipboard
>> With oRg.Find
>> .ClearFormatting
>> .Replacement.ClearFormatting
>> .Text = "(Article With Back links)(*)(END NO LINKS
>> SECTION======2)" .Forward = True
>> .MatchWildcards = True
>> If .Execute Then
>> oRg.Select ' for testing, remove later
>> ' oRg.Copy
>> ' oRg.Text = ""
>> End If
>> End With
>>
>
> It is always a bit messy to use alternatively Range and Selection objects.
> You should only use one of them, unless you have no other choice. The
> Range.Find object allows you to manipulate the document content without
> touching at the user selection point and will not leave any settings
> behind in the Find/Replace dialog box when the user next uses it, but it
> can a bit a bit ore obscure and difficult to get used to. If you need
> both, you have to be extra careful and make sure you know what is going on
> in the document.
> One trick you can use is to set the Word document window to half the
> screen (Horizontally) and the VBA code window to the other half. Then,
> place the cursor inside the code and hit F8 to debug step by step. Every
> time you hit F8, one line of code gets executed and you can see the
> changes, if any, in the document above.
>
> In your particular case, what I can see is this:
>
> You start with this:
> Selection.Find.ClearFormatting
> Selection.Find.Replacement.ClearFormatting
> With Selection.Find
> ...
> which works form the user current selection and will change that selection
> if the text is found. The found text becomes the current selection.
> Let's say the text has been found and the selection was consequently
> changed.
> Next, you use a bunch of If statements to change the current selection.
> This is followed by:
> Dim oRg As Range
> Set oRg = ActiveDocument.Range
> With oRg.Find
> ...
> This works on the whole document and will act on the first instance fund
> in the document, regardless of the current selection.
> And then you do:
> Selection.Find.ClearFormatting
> With Selection.Find
> .Text = "Article With Back links"
> which will again move the selection if the text is found. Let's say it
> was.
> You finish with:
> With oRg.Find
> .ClearFormatting
> .Replacement.ClearFormatting
> Here oRg refers to the range that was found before, and deleted! So, now
> it refers to nothing, so unsurprisingly, nothing is found. How did you
> expect to copy something that was deleted to the clipboard? I am a bit
> confused by your procedure. Maybe you need to add
> Set oRg = ActiveDocument.Range
> before this last oRg.Find to reset the range to the whole document.
> Remember that a Range.Find.Execute will not change the selection in the
> document but will change the Range object if the text is found.
>
> --
>
> Salut!
> _______________________________________
> Jean-Guy Marcil - Word MVP
> jmarcilREMOVE[ at ]CAPSsympatico.caTHISTOO
> Word MVP site:
http://www.word.mvps.org>