PDA

View Full Version : How I can replace text in footers and footnotes



chuchmaster
02-15-2006, 07:11 AM
I use Selection object to replace text begining with # in document body.
I use MoveStartUntil and MoveRight methods to find and replace the text.

Everithing was OK. But then I decide to replace texts with # in footers It was a problem. I try a lot of ways to do that, but I can't :dunno :(
Please help me to replace text in footers, footnotes, .... :help

Thanks in advance

TonyJollans
02-15-2006, 12:03 PM
Hi chuchmaster,

Welcome to VBAX!

You should try to avoid using the Selection object wherever possible (which is nearly always). To Select footnotes (via code) is clumsy - much better to use Ranges. You can work through the Documents Footnotes Collection if that is what you want.

Or you can use Find and Replace which sounds far better than what you're doing at the moment. Can you post more details of what you're trying to replace and/or the code that's giving you problems?

chuchmaster
02-16-2006, 12:11 AM
Thanks for post !

I have some questions ...
1. Why should one avoid to use Selection ?
2. You say to use find and replace , but there is no such methods to use in VBA

Please if you can , post me some portion of source of how to find codes in document and footnotes begining with # (ex. #423)

Thanks!

TonyJollans
02-16-2006, 04:58 AM
Hi chuchmaster,

1. The Selection is, more or less, a special type of Range; it represents part of the User Interface. When working in code you don't want to be needlessly changing what the user sees and there are many other ways of working which are not available via the UI. That said, there are occasions when only the Selection will do but, in general you should use a (non-Selection) Range in place of the Selection.

2. There is a Find object - usually, incidentally, used with the Selection. Try recording yourself doing a Find and Replace operation via the UI to get some basic code to work with - or try this to get you going
Dim Story As Range

For Each Story In ActiveDocument.StoryRanges
With Story.Find
.ClearFormatting
.Text = "#[0-9]@>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
While .Execute
MsgBox Story.Text
Wend
End With
Next

JohnnyBravo
02-16-2006, 09:19 AM
Tony, out of curiosity, when you say Dim Story As Range, is that like telling VBA to declare the entire document as the range? What's the difference b/w story and document?

TonyJollans
02-16-2006, 09:59 AM
There should be something in help about Stories - or StoryRanges, but a Document is made up of about a dozen Stories (not all necessarily present) - Main Body, Headers and Footers, Footnotes, etc. Roughly they are equivalent to the Views that you see in different Panes in the User Interface.

chuchmaster
02-16-2006, 11:35 PM
Hi all !

Thanks Tony !!!!

You help me a lot, I didn't know that as a find text I can use regular expressions, so that was the problet that I didn't use find method.

I have already wrote my program and it works much more faster than using
MoveStartUntil and other methods ... :thumb

Thanks You a lot. And I didn't know about stories, but now I read about them, and mu program replaces code not only in body and footnotes , but also in other stories !

Thanks again ! :friends:
Best regards, Arthur !

geekgirlau
02-17-2006, 12:18 AM
Hi Arthur, don't forget to mark this thread as "Solved" (use the Thread Tools button at the top of the page).

TonyJollans
02-17-2006, 01:16 AM
My pleasure, Arthur!

Happy to help - and very pleased to see that you've been doing your own learning as well.

Just a quick note about Find. It uses it's own brand of regular expressions - a subset if you like - useful but not as powerful as it might be.

chuchmaster
02-18-2006, 03:23 AM
Hi again !

I found a new problem and I can't understand what is going on !


found = True
Set myStoryRange = ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
Do While found
With myStoryRange.Find
.ClearFormatting
.Text = "[#~]{1}[0-9]{1,}>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True

found = .Execute
If found Then
strCode = Mid(myStoryRange.Text, 2)
' Here I need to find value
' to replace the code I have found
' ....
' Assume that I find it, denote it as VAL1
.Replacement.Text = "VAL1"
.Replacement.Font.Color = wdColorRed

' Replacement text may contain special symbols, that
' may be treated as wildcards
.MatchWildcards = False

' Replace all occurences of the found text with my value
.Text = myStoryRange.Text

.Execute Replace:=wdReplaceAll, Wrap:=wdFindContinue
End If
End With
' Try to find another code and replace with another value
Loop


The probles is that after the first replacement of the first code
in the next step of loop my myStoryRange.StoryType become
"wdFootnoteSeparatorStory" and hence in the next step I cannot find another code to replace.

Why myStoryRange story type changes after replace ?????:bug:

TonyJollans
02-20-2006, 05:00 AM
I can reproduce this and at the moment I don't understand either :)

I will try and find out what is happening.

fumei
02-20-2006, 06:55 PM
This also occurs when trying to loop through and replace text in those textbox Shapes. For some reason Replace only works once if the entire Range (or in this case StoryRange) is used. Why...I have no idea, but it has something to do with...I think...looking at a Collection as ONE object.

But if anyone can find a solution, Tony can.