Results 1 to 15 of 15
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Exclude tables from formatting replacement (2000 sp4)

    Shutting the horse after the door has bolted, but...

    I'd like to change the style of all the normal text in a document from normal to body text. This is not appropriate for text inside tables, however, and there are plenty of tables. Is there either any way to exclude tables from a find and replace operation (which I doubt), or, alternatively, a VBA method to find out if text is in a table?

    Thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exclude tables from formatting replacement (2000 sp4)

    You could create a clone of the Normal style, name it Table Normal and apply it to all tables.

    In VBA, you could use this:

    Sub ExcludeTables()
    Application.ScreenUpdating = False
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Wrap = wdFindStop
    .Replacement.Text = ""
    .Format = True
    .Style = wdStyleNormal
    Do While .Execute
    If Not Selection.Information(wdWithInTable) Then
    Selection.Style = wdStyleBodyText
    End If
    Selection.Collapse Direction:=wdCollapseEnd
    Loop
    End With
    Application.ScreenUpdating = True
    End Sub

  3. #3
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Exclude tables from formatting replacement (20

    Thank you very much Hans.

    I thought I'd become reasonably familiar with VBA, through working in Access and Excel, and yet your sub reads like a new language! It would have taken hours for me to come close to this (if ever).

    Much appreciated.

    I'm interested in the ability to run code within a .Find - is it possible to change the replacement during an operation? One situation I came across recently in which this would have been useful was the need to increment all table numbers and references by a specific offset.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exclude tables from formatting replacement (20

    One possibility is to change .Replacement.Text within the loop. Another one is to set Selection.Text to a (variable) string specified within the loop.

  5. #5
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Exclude tables from formatting replacement (20

    What a useful thing to know.

    Thanks

    BTW, when you said " You could create a clone of the Normal style, name it Table Normal and apply it to all tables." I take it that would still need to be a manual (or VBA) process: there's no way to limit a selection to tables, is there?

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exclude tables from formatting replacement (20

    > I take it that would still need to be a manual (or VBA) process

    Yes, that's correct.

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Exclude tables from formatting replacement (20

    In VBA it is relatively easy to limit the selection to tables. Depending on what you are trying to achieve:
    1. you could change one line of the code that Hans supplied to only act on text within a table or
    2. you could run code that searches for every table in the document and then acts on that.

    Code that demonstrates the second option include <post:=50,287>post 50,287</post:>, <post:=383,686>post 383,686</post:> and <post:=344,770>post 344,770</post:>
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #8
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Exclude tables from formatting replacement (20

    Dear Hans,

    I'm in the process of trying to apply my new found knowledge, but it's clearly more complicated than I thought.

    This is my current code:

    Sub IncrementTableNum()
    Dim lTableNum As Long
    Application.ScreenUpdating = False
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "(Table?)([1-9][1-9])"
    .MatchWildcards = True
    .Wrap = wdFindStop
    Do While .Execute
    lTableNum = CLng(Right(Selection, 2))
    If lTableNum > 20 Then
    .Replacement.Text = "1" & Str(lTableNum + 1)
    End If
    Selection.Collapse Direction:=wdCollapseEnd
    Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    But the table numbers aren't changing, although stepping through the sub they seem to be being found. I tried commenting out the Application.ScreenUpdating lines in case I wasn't seeing the changes because of them, but it doesn't seem to make a difference.

    What am I doing wrong?

    BTW using Selection was the only way I could find of extracting the actual found text in order to generate the incremented string. Don't know if there's a better way.

    Thanks

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Exclude tables from formatting replacement (20

    Mark, you are setting the replacement text but you are not running a replace afterwards. It might be easier, given that the table name is selected, to use Selection.TypeText to overwrite the old information with the new. This would avoid the difficulty of using the Find function in two different ways.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exclude tables from formatting replacement (20

    Setting Replacement.Text will not do anything, since the loop only uses Execute to find the next instance of the Find What text. If you want to change the text, you must use

    Selection.Text = ...

    instead. You cannot use 1 here, you must create the string explicitly.

  11. #11
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Exclude tables from formatting replacement (20

    Thanks Jefferson and Hans,

    Going back to your original code Hans, I begin to see my mistake. So, one more question: are the actual contents of the found text (eg 1 or 2) exposed at all? In this case I don't actually need them, since 1 will always be Table followed by a space, although sometimes the space will be non-breaking, hence the wildcard, but I can envisage other situation where I would want to access the actual string found, rather than the criterion, in order to construct the replacement string. Using Selection works only up to a point (eg if the string is always of a predictable length).

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exclude tables from formatting replacement (20

    The expressions 1 etc. are only available in .Replacement.Text, not outside it. For complex manipulations of the found text, you'll need to parse Selection.Text.

  13. #13
    Lounger
    Join Date
    Jul 2003
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exclude tables from formatting replacement (2000 sp4)

    Good afternoon,
    Can someone help me edit the code in the post, <post#=557,539>post 557,539</post#>, so that it does a Search for Normal font and Replaces with Table Text font in all the tables of a document.
    as is mentioned further down the thread in <post#=557861>post 557861</post#>?
    Thanks in advance for your help. I will check back tomorrow.

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exclude tables from formatting replacement (2000 sp4)

    Do you mean the Normal and Table Text styles? They are not fonts. If so, try this:

    Sub ReplaceTableStyle()
    Dim tbl As Table
    For Each tbl In ActiveDocument.Tables
    With tbl.Range.Find
    .Text = ""
    .Replacement.Text = ""
    .ClearFormatting
    .Replacement.ClearFormatting
    .Style = "Normal"
    .Replacement.Style = "Table Text"
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
    End With
    Next tbl
    Set tbl = Nothing
    End Sub

  15. #15
    Lounger
    Join Date
    Jul 2003
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exclude tables from formatting replacement (2000 sp4)

    Yes, I did mean styles. Thank you. This works great.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •