Ask a questionAsk a question
 

AnswerWord 2003 replace string after insert

  • Thursday, October 29, 2009 7:47 AMVSempoux Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello all, 

    I would like to accomplish the following:

    - i open my word document 
    - on page 1 and two i have a number of dropdownboxes 
    - on the last page i have a table with 2 columns 
    (col1 = code, col2 = description) 
    example : 
    col1 _________col2 
    ------------------- 
    <TR1>________Mr 
    <TR2>________Smith 
    <TR3>________John 
    ....... 

    when in one of my dropdownboxes i select the following text: 

    "Dear <TR1> <TR2>, we thank you for your trust in our company." 

    it is automatically inserted in a bookmark just under the dropdown. 

    What i would like next in that automated step is that a script checks the 
    content of that text and if it finds a value (like <TR1>) that is 
    contained in col1 of my table, automatically replaces it with the value that 
    is in col2. 

    So the script should run through all the <> values it finds in the selected 
    text from the dropdownbox and compare it with the values in col1 of my table. When it finds the correspondant value, it should be replaced with the value in col2. 

    thanks already for your help. 

    Vincent

Answers

  • Sunday, November 01, 2009 4:32 PMJeff - www.SRSoft.usMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vincent,

    try this

    Sub CreateBookmarkWithTextFromTable()
        Dim thankyouline As String: thankyouline = "Dear <TR1> <TR2>, we thank you for your trust in our company"
        'Dim thankyouline As String: thankyouline = Selection.Paragraphs(1).Range.Text
     
        With ActiveDocument.Bookmarks
           .Add Range:=Selection.Range, Name:="thankyou"
           .DefaultSorting = wdSortByName
           .ShowHidden = False
        End With
        Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
        'Selection.MoveDown Unit:=wdLine, Count:=1
        Dim sal As String: sal = Selection.Tables(1).Rows(1).Cells(2).Range.Text
        Dim lastname As String: lastname = Selection.Tables(1).Rows(2).Cells(2).Range.Text
        thankyouline = Replace(thankyouline, "<TR1>", sal)
        thankyouline = Replace(thankyouline, "<TR2>", lastname)
        thankyouline = Replace(thankyouline, vbCr, "")
        Selection.GoTo What:=wdGoToBookmark, Name:="thankyou"
        Selection.Bookmarks("thankyou").Range.Text = thankyouline
    End Sub

    this just creates a new bookmark wherever your cursor is already, gets the first table in your document and gets the text values from rows 1 and 2 in column 2, then builds the thankyouline replacing the line feed characters at the ends of the text values from the table and finally gets the bookmark and sets the text as the thankyouline.

    you can also build the thankyouline string piece by piece but the repalace is fine.  it might be easier for you to follow that way.  to build the string would be something like this:
    thankyouline = "Dear " + sal + " " + lasname + ", we thank you for your trust in our company"

    you can use that instead of the two first replace lines.  you still need to remove the vbcr characters from the string though.  you can comment out that line to see what happens without it.
    FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial

All Replies

  • Sunday, November 01, 2009 4:32 PMJeff - www.SRSoft.usMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vincent,

    try this

    Sub CreateBookmarkWithTextFromTable()
        Dim thankyouline As String: thankyouline = "Dear <TR1> <TR2>, we thank you for your trust in our company"
        'Dim thankyouline As String: thankyouline = Selection.Paragraphs(1).Range.Text
     
        With ActiveDocument.Bookmarks
           .Add Range:=Selection.Range, Name:="thankyou"
           .DefaultSorting = wdSortByName
           .ShowHidden = False
        End With
        Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
        'Selection.MoveDown Unit:=wdLine, Count:=1
        Dim sal As String: sal = Selection.Tables(1).Rows(1).Cells(2).Range.Text
        Dim lastname As String: lastname = Selection.Tables(1).Rows(2).Cells(2).Range.Text
        thankyouline = Replace(thankyouline, "<TR1>", sal)
        thankyouline = Replace(thankyouline, "<TR2>", lastname)
        thankyouline = Replace(thankyouline, vbCr, "")
        Selection.GoTo What:=wdGoToBookmark, Name:="thankyou"
        Selection.Bookmarks("thankyou").Range.Text = thankyouline
    End Sub

    this just creates a new bookmark wherever your cursor is already, gets the first table in your document and gets the text values from rows 1 and 2 in column 2, then builds the thankyouline replacing the line feed characters at the ends of the text values from the table and finally gets the bookmark and sets the text as the thankyouline.

    you can also build the thankyouline string piece by piece but the repalace is fine.  it might be easier for you to follow that way.  to build the string would be something like this:
    thankyouline = "Dear " + sal + " " + lasname + ", we thank you for your trust in our company"

    you can use that instead of the two first replace lines.  you still need to remove the vbcr characters from the string though.  you can comment out that line to see what happens without it.
    FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial