Search-replace using format-- not working

  • Thread starter christophercbrewster via OfficeKB.com
  • Start date
C

christophercbrewster via OfficeKB.com

I do search-and-replace in VBA only using recorded code (with my changes),
though there might be a nicer non-recorded way to do it. I need code for a
search-replace that deletes all text using a certain font. But when I record
it, the recorded code doesn't preserve the font information. (Only the ".
Format = True" part is there.) Would appreciate advice.

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200911/1
 
J

Jay Freedman

I do search-and-replace in VBA only using recorded code (with my changes),
though there might be a nicer non-recorded way to do it. I need code for a
search-replace that deletes all text using a certain font. But when I record
it, the recorded code doesn't preserve the font information. (Only the ".
Format = True" part is there.) Would appreciate advice.

The macro recorder has a bug, in that it fails to record the
formatting that you're searching for or replacing with. See the
section "Fixing broken Replace macros" in
http://word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm
 
C

christophercbrewster via OfficeKB.com

Thanks-- that worked (I think I've seen it before).

I have a similar question. I recorded the dialog to add top and bottom
padding to all the cells in a table, but this also fails. It looks
straightforward but it has no effect. Is this a similar problem of the
recorder omitting something?

Sub Macro2()
Selection.Tables(1).Select
With Selection.Cells(1)
.TopPadding = InchesToPoints(0.03)
.BottomPadding = InchesToPoints(0.03)
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
.FitText = False
End With
End Sub


Jay said:
The macro recorder has a bug, in that it fails to record the
formatting that you're searching for or replacing with. See the
section "Fixing broken Replace macros" in
http://word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200911/1
 
J

Jay Freedman

That macro actually does have an effect, but only on the top left cell
of the table. Since the unchanged cells in the same row and column
hold the shape of the table the same, it just looks like nothing
happens.

In this case I can't quite blame the recorder. The problem is that the
recorder doesn't have the ability to declare object variables or to
write loops. You have to know some details about the Word object model
to be able to design the proper modification, using a For Each loop:

Sub Macro2()
Dim oCell As Cell
Selection.Tables(1).Select
For Each oCell In Selection.Cells
With oCell
.TopPadding = InchesToPoints(0.03)
.BottomPadding = InchesToPoints(0.03)
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
.FitText = False
End With
Next
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
C

christophercbrewster via OfficeKB.com

Of course-- it's right there in my code "With Selection.Cells(1)". Thanks for
the explanation.

Jay said:
That macro actually does have an effect, but only on the top left cell
of the table. Since the unchanged cells in the same row and column
hold the shape of the table the same, it just looks like nothing
happens.

In this case I can't quite blame the recorder. The problem is that the
recorder doesn't have the ability to declare object variables or to
write loops. You have to know some details about the Word object model
to be able to design the proper modification, using a For Each loop:

Sub Macro2()
Dim oCell As Cell
Selection.Tables(1).Select
For Each oCell In Selection.Cells
With oCell
.TopPadding = InchesToPoints(0.03)
.BottomPadding = InchesToPoints(0.03)
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
.FitText = False
End With
Next
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Thanks-- that worked (I think I've seen it before).
[quoted text clipped - 30 lines]

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200911/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top