PDA

View Full Version : [SOLVED:] Help with Search&Replace all content - even in tables!



janaboo13
11-13-2014, 09:23 PM
Hi All!
It's been a while since I've had to write Word macros, but now I need help! I need to search for any lowercase letter, space, number and replace the space with a non-breaking space. I used something similar (see code below) and it works on all content, except the content in tables. I've tried using ActiveDocument.Content.Find, but to no avail. I'm stumped...how do I make sure that content is included in the S&R?:banghead:

Any help would be appreciated!

Here's the code:


Sub FixLCNumNonBreakingSpaces()
'
' This macro replaces spaces between lowercase letters and numbers with non-breaking spaces
'
'
With Selection.Find
.ClearFormatting
.Text = "([a-z]) ([0-9])"
.Replacement.ClearFormatting
.Replacement.Text = "\1^s\2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

gmaxey
11-13-2014, 09:44 PM
I don't know what kind of tables you are talking about or where they may be, but here you code works fine with tables inserted in the main text storyrange.

macropod
11-13-2014, 11:04 PM
Agreed. There is nothing about the code that would prevent it working with tables, per se. Of course, if you've stuck the tables into textboxes, you'd have to select each textbox in turn before running the macro. Either that or re-code it to process both the body of the document and all shape objects as well.

janaboo13
11-15-2014, 10:41 AM
Agreed. There is nothing about the code that would prevent it working with tables, per se. Of course, if you've stuck the tables into textboxes, you'd have to select each textbox in turn before running the macro. Either that or re-code it to process both the body of the document and all shape objects as well.

So, I did some more digging and have discovered that the problem arises when the number is a result of an inserted cross-reference. These documents are published from an Author-it database into Word. The cross-reference is produced using an object template that inserts the text "on page" followed by the appropriate page number. I haven't tried it, but I'm wondering if I can insert a non-breaking space (^s) after the word "page". Not sure that the publishing process will work...hmmmmm.

So I'm thinking that the macro doesn't see the actual number, just a field, right? Is there another way to do the search that would pick these up instead of a number?

Thanks so much! Jan

gmaxey
11-15-2014, 11:54 AM
Crude, but this might work:


Sub FixLCNumNonBreakingSpaces()
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If IsNumeric(oFld.Result) Then
If oFld.Code.Words.First.Previous.Previous = "e" Then
oFld.Code.Words.First.Previous = Chr(160)
Stop
End If
End If
Next
End Sub

macropod
11-15-2014, 09:43 PM
You could, of course, use a wildcard Find/Replace, where:
Find = (<[Oo]n page)^32
Replace = \1^s

janaboo13
11-17-2014, 08:47 PM
Thanks, Greg. I tried running this and I got a Run time error #5891 (That property is not available for that object). Debug highlighted "If IsNumeric(oFld.Result) Then"

I won't even try to understand what that means. Any other ideas?

Thanks so much! Jan


Crude, but this might work:


Sub FixLCNumNonBreakingSpaces()
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If IsNumeric(oFld.Result) Then
If oFld.Code.Words.First.Previous.Previous = "e" Then
oFld.Code.Words.First.Previous = Chr(160)
Stop
End If
End If
Next
End Sub

janaboo13
11-17-2014, 08:55 PM
Hi Macropod! I have one question...what does the "<" mean in the find expression? I get that the expression you put in parens ("[Oo]n page)^32) is looking for the word "on" either upper or lower case and a space, right? I'm sure I'm missing something, sorry.
Jan

macropod
11-17-2014, 09:02 PM
The < means the [Oo] must be at the start of a word. That ensures you get 'On page' or 'on page' but not 'bon page', for example.

gmaxey
11-17-2014, 09:14 PM
You can try adding "VBA" before the method:


Sub FixLCNumNonBreakingSpaces()
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If VBA.IsNumeric(oFld.Result) Then
If oFld.Code.Words.First.Previous.Previous = "e" Then
oFld.Code.Words.First.Previous = Chr(160)
Stop
End If
End If
Next
End Sub