MS Word: Replace bookmark Text in a Userform instead of Adding using VBA



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















VBA Newbie here.



I've just created a userform that allows my team to create a cover letter.



The button below appears in the body of the document in an "Instruction" section. When they click on it, it shows my form which they need to fill out.



Private Sub EditFormButt_Click()
CoverLetterForm.Show
End Sub


The Form currently has pre-filled placeholder text. I want my team to be able to replace the text. Right now, what happens is, when they click my "OK" Button (code to follow), the placeholder text remains, and their new text is added.



For example:




To: Mr. Paul Daniels Ms. Sarah Jones

21 New Order Street

London

England


Dear Mr. Paul Daniels Ms. Sarah Jones




Ms. Sarah Jones is the Placeholder Text, Mr. Paul Daniels is the updated text.



I need any new entries to replace whatever is in the Placeholder text, but not delete anything that hasn't been updated, as sometimes, they may only want to change the name of recipient but not the person's address. This is also useful if they want to correct any mistakes they may have made in spelling without having to start a totally new document.



Here is my OKButt Code:



Private Sub OKButt_Click()
Dim bmRecName As Range
Set bmRecName = ActiveDocument.Bookmarks("bmRecName").Range
bmRecName.Text = Me.RecName.Value
Dim bmRecAddress As Range
Set bmRecAddress = ActiveDocument.Bookmarks("bmRecAddress").Range
bmRecAddress.Text = Me.RecAddress.Value
CoverLetterForm.Hide
End Sub


Thanks for your help :)










share|improve this question

















  • 1





    It's not quite clear how things are set up in the document... Where are the bookmarks in relation to the "placeholder text"? I'm guessing they're next to the text, not surrounding the text? Try selecting placeholder text and adding the bookmark to the selection so that the bookmark contains the selection. Use a different bookmark for each item (i.e. not the entire address in one bookmark).

    – Cindy Meister
    Nov 15 '18 at 12:46











  • Hello Cindy, the placeholder text is pre-filled in the UserForm. I have found a way by adding the following to each Range ActiveDocument.Bookmarks.Add "bmRecName", BMRecName

    – Anniesoptera
    Nov 15 '18 at 12:57


















0















VBA Newbie here.



I've just created a userform that allows my team to create a cover letter.



The button below appears in the body of the document in an "Instruction" section. When they click on it, it shows my form which they need to fill out.



Private Sub EditFormButt_Click()
CoverLetterForm.Show
End Sub


The Form currently has pre-filled placeholder text. I want my team to be able to replace the text. Right now, what happens is, when they click my "OK" Button (code to follow), the placeholder text remains, and their new text is added.



For example:




To: Mr. Paul Daniels Ms. Sarah Jones

21 New Order Street

London

England


Dear Mr. Paul Daniels Ms. Sarah Jones




Ms. Sarah Jones is the Placeholder Text, Mr. Paul Daniels is the updated text.



I need any new entries to replace whatever is in the Placeholder text, but not delete anything that hasn't been updated, as sometimes, they may only want to change the name of recipient but not the person's address. This is also useful if they want to correct any mistakes they may have made in spelling without having to start a totally new document.



Here is my OKButt Code:



Private Sub OKButt_Click()
Dim bmRecName As Range
Set bmRecName = ActiveDocument.Bookmarks("bmRecName").Range
bmRecName.Text = Me.RecName.Value
Dim bmRecAddress As Range
Set bmRecAddress = ActiveDocument.Bookmarks("bmRecAddress").Range
bmRecAddress.Text = Me.RecAddress.Value
CoverLetterForm.Hide
End Sub


Thanks for your help :)










share|improve this question

















  • 1





    It's not quite clear how things are set up in the document... Where are the bookmarks in relation to the "placeholder text"? I'm guessing they're next to the text, not surrounding the text? Try selecting placeholder text and adding the bookmark to the selection so that the bookmark contains the selection. Use a different bookmark for each item (i.e. not the entire address in one bookmark).

    – Cindy Meister
    Nov 15 '18 at 12:46











  • Hello Cindy, the placeholder text is pre-filled in the UserForm. I have found a way by adding the following to each Range ActiveDocument.Bookmarks.Add "bmRecName", BMRecName

    – Anniesoptera
    Nov 15 '18 at 12:57














0












0








0








VBA Newbie here.



I've just created a userform that allows my team to create a cover letter.



The button below appears in the body of the document in an "Instruction" section. When they click on it, it shows my form which they need to fill out.



Private Sub EditFormButt_Click()
CoverLetterForm.Show
End Sub


The Form currently has pre-filled placeholder text. I want my team to be able to replace the text. Right now, what happens is, when they click my "OK" Button (code to follow), the placeholder text remains, and their new text is added.



For example:




To: Mr. Paul Daniels Ms. Sarah Jones

21 New Order Street

London

England


Dear Mr. Paul Daniels Ms. Sarah Jones




Ms. Sarah Jones is the Placeholder Text, Mr. Paul Daniels is the updated text.



I need any new entries to replace whatever is in the Placeholder text, but not delete anything that hasn't been updated, as sometimes, they may only want to change the name of recipient but not the person's address. This is also useful if they want to correct any mistakes they may have made in spelling without having to start a totally new document.



Here is my OKButt Code:



Private Sub OKButt_Click()
Dim bmRecName As Range
Set bmRecName = ActiveDocument.Bookmarks("bmRecName").Range
bmRecName.Text = Me.RecName.Value
Dim bmRecAddress As Range
Set bmRecAddress = ActiveDocument.Bookmarks("bmRecAddress").Range
bmRecAddress.Text = Me.RecAddress.Value
CoverLetterForm.Hide
End Sub


Thanks for your help :)










share|improve this question














VBA Newbie here.



I've just created a userform that allows my team to create a cover letter.



The button below appears in the body of the document in an "Instruction" section. When they click on it, it shows my form which they need to fill out.



Private Sub EditFormButt_Click()
CoverLetterForm.Show
End Sub


The Form currently has pre-filled placeholder text. I want my team to be able to replace the text. Right now, what happens is, when they click my "OK" Button (code to follow), the placeholder text remains, and their new text is added.



For example:




To: Mr. Paul Daniels Ms. Sarah Jones

21 New Order Street

London

England


Dear Mr. Paul Daniels Ms. Sarah Jones




Ms. Sarah Jones is the Placeholder Text, Mr. Paul Daniels is the updated text.



I need any new entries to replace whatever is in the Placeholder text, but not delete anything that hasn't been updated, as sometimes, they may only want to change the name of recipient but not the person's address. This is also useful if they want to correct any mistakes they may have made in spelling without having to start a totally new document.



Here is my OKButt Code:



Private Sub OKButt_Click()
Dim bmRecName As Range
Set bmRecName = ActiveDocument.Bookmarks("bmRecName").Range
bmRecName.Text = Me.RecName.Value
Dim bmRecAddress As Range
Set bmRecAddress = ActiveDocument.Bookmarks("bmRecAddress").Range
bmRecAddress.Text = Me.RecAddress.Value
CoverLetterForm.Hide
End Sub


Thanks for your help :)







vba ms-word userform






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 12:41









AnniesopteraAnniesoptera

31




31







  • 1





    It's not quite clear how things are set up in the document... Where are the bookmarks in relation to the "placeholder text"? I'm guessing they're next to the text, not surrounding the text? Try selecting placeholder text and adding the bookmark to the selection so that the bookmark contains the selection. Use a different bookmark for each item (i.e. not the entire address in one bookmark).

    – Cindy Meister
    Nov 15 '18 at 12:46











  • Hello Cindy, the placeholder text is pre-filled in the UserForm. I have found a way by adding the following to each Range ActiveDocument.Bookmarks.Add "bmRecName", BMRecName

    – Anniesoptera
    Nov 15 '18 at 12:57













  • 1





    It's not quite clear how things are set up in the document... Where are the bookmarks in relation to the "placeholder text"? I'm guessing they're next to the text, not surrounding the text? Try selecting placeholder text and adding the bookmark to the selection so that the bookmark contains the selection. Use a different bookmark for each item (i.e. not the entire address in one bookmark).

    – Cindy Meister
    Nov 15 '18 at 12:46











  • Hello Cindy, the placeholder text is pre-filled in the UserForm. I have found a way by adding the following to each Range ActiveDocument.Bookmarks.Add "bmRecName", BMRecName

    – Anniesoptera
    Nov 15 '18 at 12:57








1




1





It's not quite clear how things are set up in the document... Where are the bookmarks in relation to the "placeholder text"? I'm guessing they're next to the text, not surrounding the text? Try selecting placeholder text and adding the bookmark to the selection so that the bookmark contains the selection. Use a different bookmark for each item (i.e. not the entire address in one bookmark).

– Cindy Meister
Nov 15 '18 at 12:46





It's not quite clear how things are set up in the document... Where are the bookmarks in relation to the "placeholder text"? I'm guessing they're next to the text, not surrounding the text? Try selecting placeholder text and adding the bookmark to the selection so that the bookmark contains the selection. Use a different bookmark for each item (i.e. not the entire address in one bookmark).

– Cindy Meister
Nov 15 '18 at 12:46













Hello Cindy, the placeholder text is pre-filled in the UserForm. I have found a way by adding the following to each Range ActiveDocument.Bookmarks.Add "bmRecName", BMRecName

– Anniesoptera
Nov 15 '18 at 12:57






Hello Cindy, the placeholder text is pre-filled in the UserForm. I have found a way by adding the following to each Range ActiveDocument.Bookmarks.Add "bmRecName", BMRecName

– Anniesoptera
Nov 15 '18 at 12:57













2 Answers
2






active

oldest

votes


















0














Simpler:



Private Sub OKButt_Click()
Dim bmRng As Range
With ActiveDocument
Set bmRng = .Bookmarks("bmRecName").Range
bmRng.Text = Me.RecName.Value
.Bookmarks.Add "bmRecName", bmRng
Set bmRng = .Bookmarks("bmRecAddress").Range
bmRng.Text = Me.RecAddress.Value
.Bookmarks.Add "bmRecAddress", bmRng
CoverLetterForm.Hide
End With
End Sub





share|improve this answer






























    0














    Ok, I think I've found the answer, but if anyone has a more elegant solution, I'm all ears!



    Private Sub OKButt_Click()
    Dim bmRecName As Range
    Set bmRecName = ActiveDocument.Bookmarks("bmRecName").Range
    bmRecName.Text = Me.RecName.Value
    ActiveDocument.Bookmarks.Add "bmRecName", bmRecName
    Dim bmRecAddress As Range
    Set bmRecAddress = ActiveDocument.Bookmarks("bmRecAddress").Range
    bmRecAddress.Text = Me.RecAddress.Value
    ActiveDocument.Bookmarks.Add "bmRecAddress", bmRecAddress
    CoverLetterForm.Hide
    End Sub


    Basically adding the following code:



    ActiveDocument.Bookmarks.Add "bmRecAddress", bmRecAddress





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319726%2fms-word-replace-bookmark-text-in-a-userform-instead-of-adding-using-vba%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Simpler:



      Private Sub OKButt_Click()
      Dim bmRng As Range
      With ActiveDocument
      Set bmRng = .Bookmarks("bmRecName").Range
      bmRng.Text = Me.RecName.Value
      .Bookmarks.Add "bmRecName", bmRng
      Set bmRng = .Bookmarks("bmRecAddress").Range
      bmRng.Text = Me.RecAddress.Value
      .Bookmarks.Add "bmRecAddress", bmRng
      CoverLetterForm.Hide
      End With
      End Sub





      share|improve this answer



























        0














        Simpler:



        Private Sub OKButt_Click()
        Dim bmRng As Range
        With ActiveDocument
        Set bmRng = .Bookmarks("bmRecName").Range
        bmRng.Text = Me.RecName.Value
        .Bookmarks.Add "bmRecName", bmRng
        Set bmRng = .Bookmarks("bmRecAddress").Range
        bmRng.Text = Me.RecAddress.Value
        .Bookmarks.Add "bmRecAddress", bmRng
        CoverLetterForm.Hide
        End With
        End Sub





        share|improve this answer

























          0












          0








          0







          Simpler:



          Private Sub OKButt_Click()
          Dim bmRng As Range
          With ActiveDocument
          Set bmRng = .Bookmarks("bmRecName").Range
          bmRng.Text = Me.RecName.Value
          .Bookmarks.Add "bmRecName", bmRng
          Set bmRng = .Bookmarks("bmRecAddress").Range
          bmRng.Text = Me.RecAddress.Value
          .Bookmarks.Add "bmRecAddress", bmRng
          CoverLetterForm.Hide
          End With
          End Sub





          share|improve this answer













          Simpler:



          Private Sub OKButt_Click()
          Dim bmRng As Range
          With ActiveDocument
          Set bmRng = .Bookmarks("bmRecName").Range
          bmRng.Text = Me.RecName.Value
          .Bookmarks.Add "bmRecName", bmRng
          Set bmRng = .Bookmarks("bmRecAddress").Range
          bmRng.Text = Me.RecAddress.Value
          .Bookmarks.Add "bmRecAddress", bmRng
          CoverLetterForm.Hide
          End With
          End Sub






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 22:43









          macropodmacropod

          3,0942311




          3,0942311























              0














              Ok, I think I've found the answer, but if anyone has a more elegant solution, I'm all ears!



              Private Sub OKButt_Click()
              Dim bmRecName As Range
              Set bmRecName = ActiveDocument.Bookmarks("bmRecName").Range
              bmRecName.Text = Me.RecName.Value
              ActiveDocument.Bookmarks.Add "bmRecName", bmRecName
              Dim bmRecAddress As Range
              Set bmRecAddress = ActiveDocument.Bookmarks("bmRecAddress").Range
              bmRecAddress.Text = Me.RecAddress.Value
              ActiveDocument.Bookmarks.Add "bmRecAddress", bmRecAddress
              CoverLetterForm.Hide
              End Sub


              Basically adding the following code:



              ActiveDocument.Bookmarks.Add "bmRecAddress", bmRecAddress





              share|improve this answer



























                0














                Ok, I think I've found the answer, but if anyone has a more elegant solution, I'm all ears!



                Private Sub OKButt_Click()
                Dim bmRecName As Range
                Set bmRecName = ActiveDocument.Bookmarks("bmRecName").Range
                bmRecName.Text = Me.RecName.Value
                ActiveDocument.Bookmarks.Add "bmRecName", bmRecName
                Dim bmRecAddress As Range
                Set bmRecAddress = ActiveDocument.Bookmarks("bmRecAddress").Range
                bmRecAddress.Text = Me.RecAddress.Value
                ActiveDocument.Bookmarks.Add "bmRecAddress", bmRecAddress
                CoverLetterForm.Hide
                End Sub


                Basically adding the following code:



                ActiveDocument.Bookmarks.Add "bmRecAddress", bmRecAddress





                share|improve this answer

























                  0












                  0








                  0







                  Ok, I think I've found the answer, but if anyone has a more elegant solution, I'm all ears!



                  Private Sub OKButt_Click()
                  Dim bmRecName As Range
                  Set bmRecName = ActiveDocument.Bookmarks("bmRecName").Range
                  bmRecName.Text = Me.RecName.Value
                  ActiveDocument.Bookmarks.Add "bmRecName", bmRecName
                  Dim bmRecAddress As Range
                  Set bmRecAddress = ActiveDocument.Bookmarks("bmRecAddress").Range
                  bmRecAddress.Text = Me.RecAddress.Value
                  ActiveDocument.Bookmarks.Add "bmRecAddress", bmRecAddress
                  CoverLetterForm.Hide
                  End Sub


                  Basically adding the following code:



                  ActiveDocument.Bookmarks.Add "bmRecAddress", bmRecAddress





                  share|improve this answer













                  Ok, I think I've found the answer, but if anyone has a more elegant solution, I'm all ears!



                  Private Sub OKButt_Click()
                  Dim bmRecName As Range
                  Set bmRecName = ActiveDocument.Bookmarks("bmRecName").Range
                  bmRecName.Text = Me.RecName.Value
                  ActiveDocument.Bookmarks.Add "bmRecName", bmRecName
                  Dim bmRecAddress As Range
                  Set bmRecAddress = ActiveDocument.Bookmarks("bmRecAddress").Range
                  bmRecAddress.Text = Me.RecAddress.Value
                  ActiveDocument.Bookmarks.Add "bmRecAddress", bmRecAddress
                  CoverLetterForm.Hide
                  End Sub


                  Basically adding the following code:



                  ActiveDocument.Bookmarks.Add "bmRecAddress", bmRecAddress






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 13:00









                  AnniesopteraAnniesoptera

                  31




                  31



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319726%2fms-word-replace-bookmark-text-in-a-userform-instead-of-adding-using-vba%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo