Results 1 to 3 of 3

Thread: Autoshapes in MS Word

  1. #1

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    52

    Autoshapes in MS Word

    Hi All,
    I have been trying to write a Word VBA Macro Code which can Search and Replace the text in AutoShapes in a word document.

    I have been able to do the same with Excel, however have been unsuccessful in doing so for the Word documents.

    Please can someone help me in doing this.
    Thanks in advance,
    Lonely

    ***********The Excel Code is *************
    Sub Test1()
    Dim Sh As Shape
    Dim ShRng As ShapeRange
    Dim SubSh As Shape
    On Error Resume Next
    For Each Sh In Worksheets("Sheet1").Shapes
    Set ShRng = Sh.Ungroup
    If Err = 0 Then
    For Each SubSh In ShRng
    SubSh.TextFrame.Characters.Text = Replace(SubSh.TextFrame.Characters.Text, "XXX", "YYY")
    Next SubSh
    ShRng.Regroup
    Else
    Err.Clear
    Sh.TextFrame.Characters.Text = Replace(Sh.TextFrame.Characters.Text, "XXX", "YYY")
    End If
    Next Sh
    End Sub
    *************End Code**************
    I Was Born Lonely, Lived Lonely .... And Will Die Lonely!!

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    In Word the property is called TextRange instead of Characters.

    VB Code:
    1. Sub Test1()
    2.     Dim Sh As Shape
    3.     Dim ShRng As ShapeRange
    4.     Dim SubSh As Shape
    5.    
    6.     On Error Resume Next
    7.    
    8.     For Each Sh In ActiveDocument.Shapes
    9.         Set ShRng = Sh.Ungroup
    10.         If Err = 0 Then
    11.             For Each SubSh In ShRng
    12.                 SubSh.TextFrame.TextRange.Text = Replace(SubSh.TextFrame.TextRange.Text, "XXX", "YYY")
    13.             Next SubSh
    14.             ShRng.Regroup
    15.         Else
    16.             Err.Clear
    17.             Sh.TextFrame.TextRange.Text = Replace(Sh.TextFrame.TextRange.Text, "XXX", "YYY")
    18.         End If
    19.     Next Sh
    20.  
    21. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    May 2003
    Posts
    52
    Hi Workhorse,
    This works wonderfully, expect for just one hitch, how do i define the options like "Match Whole Word", Match case" etc.

    Thanks again,
    Lonely
    I Was Born Lonely, Lived Lonely .... And Will Die Lonely!!

Posting Permissions

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



Click Here to Expand Forum to Full Width