Results 1 to 6 of 6
  1. #1
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post

    Text Style Within a Table - Style Variable

    Hi Folks,

    I have some text within a table - I am trying to work out how to isolate the style range from it.


    The text has a style applied to it - lets say for example the text in one of the table cells is the string "Hello" and has a style of "Heading 1" applied to it.

    To make a Variable - I have done this :

    Set Column1TextStyle = oTable.Cell(i, 1).Range.Style
    Column1TextStyle.End = Column1TextStyle - 1

    Next

    Get that style variable to apply to a string in the document that begins with [ST]

    Code:
     With myRange.Find
                        .ClearFormatting
      
                        .Replacement.ClearFormatting
                        .MatchWildcards = True           
                        .Text = " [ST] "
                   
                        .Replacement.Text = Column1TextStyle.Style

    I know there are easier ways of applying styles to text - but it is set up this way, and I want to simply isolate the style from the table cell.
    Put it in a variable and then use it as a replacement for text.

    Does this make sense or am I barking up the wrong tree.

    Within the table - this style is it a text,paragraph or range style? I just cant figure it out

    Code:
    Set  Column1TextStyle = oTable.Cell(i, 1).Range.Style
                 Column1TextStyle.End = Column1TextStyle - 1
    if any body does know do let me know thank you so much

    pb

  2. #2
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post
    Hi,

    I think its actually FormattedText

    It should be


    Code:
    Set  Column1TextStyle = oTable.Cell(i, 1).Range.FormattedText
                 Column1TextStyle.End = Column1TextStyle - 1
    Im now trying to find the string as before

    Code:
    With myRange.Find
                        .ClearFormatting
      
                        .Replacement.ClearFormatting
                        .MatchWildcards = True           
                        .Text = " [ST] "      ' Apply the Formatted Style found in above table cell
                         
    
                        .myRange.FormattedText = Column1TextStyle.FormattedText

    I would like to apply the Formatted Text properties to the String [ST]

    At the moment - I am getting a straight replace of the contents. I don't actually want the contents replaced

    I just want to apply the formatting style properties to my [ST] string. So ST is getting replaced with the string "Hello" that is in column 1

    I will work on this and see what I come up with

    I am nearly there just can't work out how not to replace the [ST] - but simply apply the formatting to the string

    pb

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    It's not exactly clear what you're trying to do. Assuming you want to apply a Style that exists in a table cell to some content found elsewhere in the document and represented by the wildcard expression " [ST] ", try:
    Code:
    With myRange.Find
      .ClearFormatting
      .MatchWildcards = True
      .Format = True
      .Forward = True
      .Wrap = wdFindContinue
      .Text = " [ST] "
      With .Replacement
        .ClearFormatting
        .Style = ActiveDocument.Tables(1).Cell(2, 1).Range.Style
        .Text = "^&"
      End With
      .Execute Replace:=wdReplaceAll
    End With
    In the above code, .Cell(2, 1) refers to the cell in row 2, column 1 of the table.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. #4
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post
    Hello Paul,

    thank you for this great input - I could not figure out .Text = "^&" is used.

    You are correct in what I am trying to do - find the formatted text or style in a table cell and apply it elsewhere

    I ran the code below - no replacement happened yet


    Code:
    Sub ReplaceFormatting()
    
    
    
     Dim myRange As Range
     
     Set myRange = ActiveDocument.Range
     
    
    With myRange.Find
      .ClearFormatting
      .MatchWildcards = True
      .Format = True
      .Forward = True
      .Wrap = wdFindContinue
      .Text = " [ST] "
      With .Replacement
        .ClearFormatting
        .Style = ActiveDocument.Tables(1).Cell(2, 1).Range.Style
        .Text = "^&"
      End With
      .Execute Replace:=wdReplaceAll
    End With
    
    End Sub

    What could it be? The tiny details some times drive me up the wall - esp when I can't spot them, growing pains

    Thank you

    pb

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by pb89 View Post
    I could not figure out .Text = "^&" is used.
    Might I suggest that, before rushing in to code something, you use the macro recorder to simulate what you're trying to do (as far as possible) and see what code it produces. A review of the 'Special' parameters in the Find/Replace dialogue wouldn't go astray, either. With that strategy, the macro recorder would generate code like:
    Code:
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
      Selection.Find.ClearFormatting
      Selection.Find.Replacement.ClearFormatting
      Selection.Find.Replacement.Style = ActiveDocument.Styles("Emphasis")
      With Selection.Find
        .Text = " [ST] "
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
      End With
      Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    You'll see that the replacement style is designated as ActiveDocument.Styles("Emphasis"), but that's only because the macro recorder can't choose a Style the way you want to, so I had to substitute something else that would generate the basic code. Of course, being as dumb as a box of rocks, the macro recorder's code isn't all that flash, but it usually at least points you in the right direction.

    The reason you're not getting any replacements might be because the Style you're trying to replace with doesn't encompass the whole cell. Is it a paragraph Style or a character Style? If it's a paragraph Style that includes the first paragraph in the cell, try:
    .Style = ActiveDocument.Tables(1).Cell(2, 1).Range.Paragraphs(1).Range.ParagraphStyle
    If it's a character Style that includes the first character in the cell, try:
    .Style = ActiveDocument.Tables(1).Cell(2, 1).Range.Characters(1).CharacterStyle
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. The Following User Says Thank You to macropod For This Useful Post:

    pb89 (2016-02-05)

  7. #6
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post

    Smile

    Hi Paul,

    thanks for the extended explanation


    It did the trick. For the past 2 days - I've been back and forth the object VBA Microsoft Reference - I could not for the life of me fix this.

    Code:
    Selection.Find.Replacement.Style = ActiveDocument.Tables(1).Cell(2, 1).Range.Paragraphs(1).Range.ParagraphStyle
    Silly of me not to think of table cells being able to have individual paragraphs - which doh were revealed when I turned on the formatting - so a table cell can have

    Paragraphs 1,2,3,....onto infinity



    Dud's that did not work :

    Code:
    ActiveDocument.Tables(1).Cell(2, 1).Range.FormattedText
    ActiveDocument.Tables(1).Cell(2, 1).Range
    ActiveDocument.Tables(1).Cell(2, 1).Range.Style
    ActiveDocument.Tables(1).Cell(2, 1).Range.Style =( "Heading 1")
    ActiveDocument.Tables(1).Cell(2, 1).Range.ParagraphStyle
    ActiveDocument.Tables(1).Cell(2, 1).Range.Fontcolor
    
    ....
    ....
    ...
     etc

    Well - I am happy to learn a new skill for my VBA tool box.

    Thanks ever so much

    Cheers Paul for helping.

    Do have a great weekend !

    pb

    Solved

Posting Permissions

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