PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
***Resolved*** How to Replace Text Between Bookmarks in Word-VBForums
Results 1 to 14 of 14

Thread: ***Resolved*** How to Replace Text Between Bookmarks in Word

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Edgerton, WI
    Posts
    381

    ***Resolved*** How to Replace Text Between Bookmarks in Word

    Does anyone know how to replace text between two bookmarks in Word 2000? What I need to do is replace whatever text is between the bookmarks with a predefined phrase.

    The actual text between the bookmarks to be replaced will be different everytime this routine is run so I can't use find and replace.

    Thanks for your input.
    Last edited by Rev. Michael L. Burns; Nov 8th, 2002 at 11:49 AM.

  2. #2
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396
    What you could do is, make that phrase that you need to change a BookMark and use a String in your routine with the changing text.
    Can't Remember Birthdays or Important Dates- Never Miss any Important Date(s)

  3. #3
    Lively Member
    Join Date
    Jul 2002
    Posts
    78
    Is the text between the bookmarks a paragraph? multiple paragraphs? Is it consistent?

    The reason I'm asking is that if it is the entire paragraph that will be replaced, simply place the bookmark at the beginning of the paragraph -- then set your code to GoTo the bookmark, select the entire paragraph, then replace the entire paragraph with whatever you are replacing it with.

    Now, the complication of course arises if whatever is between the two bookmarks is not consistent... if you could be more specific about what is between the bookmarks there may be an easier way!

    Thanks,
    Mary

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Edgerton, WI
    Posts
    381
    The text between the bookmarks is ever changing and often times several paragraphs in length. Basically, it is the body of the last letter written.

    Presently I have vb highlight the text between the bookmarks when the document is first opened and the user just starts typing to replace it and this works fine but the user doesn't really need to see the content of the last letter written. They also may not understand that this is what they are supposed to do.

    Instead, when the document opens, I want it to immediatly replace the body of the letter (that which is between the bookmarks, ie. the body of the last letter) with the phrase "Please type your letter here...."

    Basically, this document is used to set up a letter that will later be used in a mailmerge elsewhere in my program. The user opens this document from within vb and types only the body of the letter and then saves it and then closes Word. From another location in the program, if we need to send a letter to everyone in Albion, WI (presupposing that we have already written the body content as above) we do a query for such and then click a button that generates the letter to them all.

    Everything works fine and as expected with the exception of the automatic replacement of text which serves as a prompt as well as replacing text that the user may not need to see (for instance I may use the system to send out a confidential letter to the members of our church board which I may not want the next person using the system to be able to see, thus my reasoning for the replacement idea).

  5. #5
    Lively Member
    Join Date
    Jul 2002
    Posts
    78
    Create a letter template that has everything except the body and simply insert a text form field where the body will go if they are typing it when the document gets opened. You can edit a text form field so that the user sees "Click here and type the body of your letter" (or whatever else you want to say).

    Have your program open the template, then user clicks on the field and starts typing -- the field is removed and replaced with whatever they type.

    This suggestion assumes that the letter is saved as a different name and you aren't reusing the same letter file every time (that's how I read your process so hopefully I understand it correctly! If not I'm sure we can come up with another approach - Word provides many options, some easier than others, some not!).

    Thanks,
    Mary

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Edgerton, WI
    Posts
    381
    Thanks Mary,

    Unfortunately, because of design long ago, this always uses the same letter and is always saved as the under the same name. That is the reason I want to replace the text.

    Right now it will highlight the old body text upon opening and the user can just start typing and all works ok. However, it allows them to initially see the text of the previous letter which may contain sensitive info not for public view. This is the reason I want to replace the initially highlighted old text with a message prompting them to start typing.

    Not the best design but kinda locked in here since I must use the same form doc over and over and save under the same name.

    Hope this clears the waters a bit.

  7. #7
    Frenzied Member mxnmx's Avatar
    Join Date
    Dec 2001
    Location
    I'm back...now!!!
    Posts
    1,396
    This should work for ya:



    What you could do is, make that phrase that you need to change a BookMark and use a String in your routine with the changing text.

    Can't Remember Birthdays or Important Dates- Never Miss any Important Date(s)

  8. #8
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    Originally posted by Rev. Michael L. Burns (shortened down by HaxSoft for the sake of brevity.)
    ...I have vb highlight the text between the bookmarks when the document is first opened...when the document opens, I want it to immediatly replace the body of the letter...with the phrase "Please type your letter here...."...Everything works fine...with the exception of the automatic replacement of text...
    I would very much like to get answers to the following questions, because I think this can be solved quite easily:

    1) Which version of Word do you use?
    2) What is the code that highlights the text you want to replace?
    3) How do you control Word; VBA or VB?
    4) If VB, which version do you use.

    While answers to the above questions would help me understand the problem better, I think I do have a solution and will not hesitate to bring it. If you have the text to be replaced highlighted already when you open the document, then you could use:

    VB Code:
    1. Selection.TypeText "Please type your letter here."

    That should be it.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Edgerton, WI
    Posts
    381
    HaxSoft,

    That did the trick. For everyone who offered their assistance I am most greatful. For thos who are interesteded I have listed below my code as it currently stands which does what I need.

    VB Code:
    1. Set wrd = GetObject(, "Word.Application")
    2.             If Err.Number Then
    3.                 'Word isn't started so we have to load it.
    4.                 Set wrd = CreateObject("Word.Application")
    5.             End If
    6.             With wrd
    7.                 .Documents.Open App.Path + "\Mailmerge Letterhead"
    8.                 .Visible = True
    9.             End With
    10.             'Highlight Text To Be Replaced
    11.             ActiveDocument.Range( _
    12.                 ActiveDocument.Bookmarks("StartHighLightedText").Range.Start, _
    13.                 ActiveDocument.Bookmarks("StopHighlightedText").Range.Start).Select
    14.             'Replace the Highlighted Text
    15.             Selection.TypeText "Please type the body of your letter here.............."
    16.             'Find and Highlight the newly added text
    17.             Selection.Find.ClearFormatting
    18.             With Selection.Find
    19.                 .Text = "Please type the body of your letter here.............."
    20.                 .Replacement.Text = ""
    21.                 .Forward = True
    22.                 .Wrap = wdFindContinue
    23.                 .Format = False
    24.                 .MatchCase = False
    25.                 .MatchWholeWord = False
    26.                 .MatchWildcards = False
    27.                 .MatchSoundsLike = False
    28.                 .MatchAllWordForms = False
    29.             End With
    30.             Selection.Find.Execute

    HaxSoft, although it is not needed now, you asked several questions so I thought I'd add the answers here as well. I am using vb6 sp5, Word 2000, Access2000 and I am controling Word with vb. The code is listed above.

    I am still have a problem that may or may not be related though and hopefully someone has an idea.

    When I execute this code it works beautifully, doing everything I wanted and expected. But after closing Word and trying to execute this code again the code will not work. It will not highlight or replace the text. I have to close the vb application and reopen it before it will work again.

    Any ideas

  10. #10
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    Originally posted by Rev. Michael L. Burns
    HaxSoft,

    That did the trick. For everyone who offered their assistance I am most greatful...so I thought I'd add the answers here as well. I am using vb6 sp5, Word 2000, Access2000 and I am controling Word with vb. The code is listed above.

    I am still have a problem that may or may not be related though and hopefully someone has an idea.

    When I execute this code it works beautifully, doing everything I wanted and expected. But after closing Word and trying to execute this code again the code will not work. It will not highlight or replace the text. I have to close the vb application and reopen it before it will work again.

    Any ideas
    Thank you for providing the answers, and I am only glad to be of help to you. This problem of yours rather intrigued me.

    I haven't had time to look closely at the code, but I have a suspicion that one of the bookmarks are being replaced (and thus deleted) in the whole process.

    It's weekend now, so I hope to find some time to look into this. My interest in this problem stems mainly from a project that I am working on, involving Word and bookmarks, and everything. Maybe we can help each other out here.

    In any case, I am just glad it semi-worked.

  11. #11
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    HUNCH!!!

    I haven't tried this, as the machine I am using for the web doesn't have Office installed, BUT my hacker-instinct tells me that

    VB Code:
    1. 'Highlight Text To Be Replaced
    2. ActiveDocument.Range( _
    3.     ActiveDocument.Bookmarks("StartHighLightedText").Range.[b]Start[/b], _
    4.     ActiveDocument.Bookmarks("StopHighlightedText").Range.Start).Select

    should be this:

    VB Code:
    1. 'Highlight Text To Be Replaced
    2. ActiveDocument.Range( _
    3.     ActiveDocument.Bookmarks("StartHighLightedText").Range.[b]End[/b], _
    4.     ActiveDocument.Bookmarks("StopHighlightedText").Range.Start).Select

    Just a thought. I have no way of testing it at this point, but I THINK that's the problem.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Edgerton, WI
    Posts
    381
    HaxSoft,

    Thanks for pointing out my typo but that didn't solve the problem. Still works the first time through and then have to close my application and reopen before it will work again. Thanks anyway.

    Mike

  13. #13
    New Member
    Join Date
    Apr 2013
    Posts
    1

    Re: ***Resolved*** How to Replace Text Between Bookmarks in Word

    Hi all,

    this Thread was very useful but I have a problem:
    the text I replace becomes part of last bookmark "StopHighlightedText", so when I run the macro again instead of replacing the text between bookmarks is adding before the text I previously replaced.
    Can someone help?

    Josť

  14. #14
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,346

    Re: ***Resolved*** How to Replace Text Between Bookmarks in Word

    Quote Originally Posted by jmrrsantos View Post
    Hi all,

    this Thread was very useful but I have a problem:
    the text I replace becomes part of last bookmark "StopHighlightedText", so when I run the macro again instead of replacing the text between bookmarks is adding before the text I previously replaced.
    Can someone help?

    Josť
    Since this thread is several years old and has been "Resolved" please start your own thread to receive a response faster.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width