#1
|
|||
|
|||
Select Text after Bookmark
I wish to select one line of text after a bookmark (bmContact) leaving the bookmark in place. Can anyone advise the vba code to do this? Thanks sandy |
#2
|
||||
|
||||
What is the code you're currently using and what are you trying to achieve?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
I have used a userform to insert text at bookmarks. Once the text has been inserted the bookmarks have been replaced. So I have bookmarks with one line of text after them (1 to 4 words).
The document has been saved and re-opened and the bookmarks with text after them are in place. I wish to select the text after the bookmark and edit text using vba (another userform), leaving the bookmark in place. I have seen code that deletes a bookmark and text but not what I want to do. |
#4
|
||||
|
||||
Inserting text at a bookmark doesn't delete or replace it. You still haven't posted your code, though. To see how to insert text at a bookmark so that it can be updated, have a look at: https://www.msofficeforums.com/word-...html#post67058
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Thanks Paul. I though that the bookmark was always deleted when text was inserted, that's why used vba to replace it! I will revise my existing code and see how it goes.
I'm off for a weeks tramp in the South Island so will look at it next week. Cheers Sandy. |
#6
|
|||
|
|||
I have got the code working with 4 bookmarks. The 4th bookmark (bmRe) is working except that I want to add the contents of a userform textbox (txtProject). This has been commented out because the code won’t run if left in. The error says that that the variable is not defined. If I add txtProject to the Call code brackets then I get a compile error. Any help would be appreciated. (I am unsure if I should put this on a new thread?).
Code:
Sub UpdateBookmark(bmContact As String, bmCompany As String, bmAddress As String, bmRe As String, StrTxtContact As String, StrTxtCompany As String, StrTxtAddress As String) Dim BkMkContact As Range Dim BkMkCompany As Range Dim BkMkAddress As Range Dim BkMkRe As Range With ActiveDocument 'Contact If .Bookmarks.Exists(bmContact) Then Set BkMkContact = .Bookmarks(bmContact).Range BkMkContact.Text = StrTxtContact .Bookmarks.Add bmContact, BkMkContact End If 'Company If .Bookmarks.Exists(bmCompany) Then Set BkMkCompany = .Bookmarks(bmCompany).Range BkMkCompany.Text = StrTxtCompany .Bookmarks.Add bmCompany, BkMkCompany End If 'Address If .Bookmarks.Exists(bmAddress) Then Set BkMkAddress = .Bookmarks(bmAddress).Range BkMkAddress.Text = StrTxtAddress .Bookmarks.Add bmAddress, BkMkAddress End If 'Re If .Bookmarks.Exists(bmRe) Then Set BkMkRe = .Bookmarks(bmRe).Range BkMkRe.Text = "Re: " '& txtProject .Bookmarks.Add bmRe, BkMkRe End If .Fields.Update End With Set BkMkContact = Nothing Set BkMkCompany = Nothing Set BkMkAddress = Nothing Set BkMkRe = Nothing End Sub Code:
Private Sub CommandButton1_Click() Call UpdateBookmark("bmContact", "bmCompany", "bmAddress", "bmRe", txtContact, txtCompany, txtAddress) End Sub |
#7
|
||||
|
||||
You are getting your range names and bookmark names mixed up and not achieving the intended aim. Use the following to fill the bookmarks
Code:
Public Sub FillBM(strBMName As String, strValue As String) 'Graham Mayor - http://www.gmayor.com Dim oRng As Range With ActiveDocument On Error GoTo lbl_Exit Set oRng = .Bookmarks(strBMName).Range oRng.Text = strValue oRng.Bookmarks.Add strBMName End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub Code:
FillBM "bmContact", StrTextContact FillBM "bmCompany", StrTxtCompany etc
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#8
|
||||
|
||||
That's the strangest way I've ever seen anyone try to implement the code in the link I posted. Surely it was clear from that post that the correct way to update each bookmark was by making repeated use of:
Call UpdateBookmark("bookmark_name", "bookmark_text") varying the bookmark name & content for each bookmark???
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
I have now got to grips with how the code works and its now working for me.
Thanks for your code Graham. Fair comment Paul, I thought my code was a bit "long winded"! Hopefully this old dog can learn some new tricks. Cheers Sandy. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to add a bookmark to text within a header | jroger05 | Word VBA | 3 | 07-14-2016 02:28 PM |
VBA Search Table for Text/Select Text/Insert Hyperlink | sldrellich | Word VBA | 3 | 03-24-2015 01:09 PM |
Microsoft Word macro to find text, select all text between brackets, and delete | helal1990 | Word VBA | 4 | 02-05-2015 03:52 PM |
Repeating Bookmark Text | BECKTHOMO | Word | 1 | 03-27-2012 08:34 PM |
Word 2003 - IncludeText Does Not Include Bookmark Text if in a Form Text Control | skarden | Word | 1 | 12-12-2011 10:39 PM |