Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-01-2017, 11:22 AM
gsandy gsandy is offline Select Text after Bookmark Windows 7 64bit Select Text after Bookmark Office 2013
Novice
Select Text after Bookmark
 
Join Date: Mar 2017
Location: New Zealand
Posts: 10
gsandy is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 05-01-2017, 04:07 PM
macropod's Avatar
macropod macropod is offline Select Text after Bookmark Windows 7 64bit Select Text after Bookmark Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

What is the code you're currently using and what are you trying to achieve?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-01-2017, 05:24 PM
gsandy gsandy is offline Select Text after Bookmark Windows 7 64bit Select Text after Bookmark Office 2013
Novice
Select Text after Bookmark
 
Join Date: Mar 2017
Location: New Zealand
Posts: 10
gsandy is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 05-01-2017, 05:29 PM
macropod's Avatar
macropod macropod is offline Select Text after Bookmark Windows 7 64bit Select Text after Bookmark Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 05-01-2017, 06:35 PM
gsandy gsandy is offline Select Text after Bookmark Windows 7 64bit Select Text after Bookmark Office 2013
Novice
Select Text after Bookmark
 
Join Date: Mar 2017
Location: New Zealand
Posts: 10
gsandy is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 05-09-2017, 06:35 PM
gsandy gsandy is offline Select Text after Bookmark Windows 7 64bit Select Text after Bookmark Office 2013
Novice
Select Text after Bookmark
 
Join Date: Mar 2017
Location: New Zealand
Posts: 10
gsandy is on a distinguished road
Default

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
Called with:
Code:
  Private Sub CommandButton1_Click()
   
      Call UpdateBookmark("bmContact", "bmCompany", "bmAddress", "bmRe", txtContact, txtCompany, txtAddress)
   
  End Sub
Reply With Quote
  #7  
Old 05-09-2017, 08:17 PM
gmayor's Avatar
gmayor gmayor is offline Select Text after Bookmark Windows 10 Select Text after Bookmark Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
and call it with
Code:
FillBM "bmContact", StrTextContact
FillBM "bmCompany", StrTxtCompany
etc
If the named bookmarks or the strings don't exist the function does nothing, otherwise the values are written into the named bookmarks, and are replaced if you run the calling macro again with different values.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #8  
Old 05-10-2017, 04:52 AM
macropod's Avatar
macropod macropod is offline Select Text after Bookmark Windows 7 64bit Select Text after Bookmark Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gsandy View Post
I have got the code working with 4 bookmarks.
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]
Reply With Quote
  #9  
Old 05-10-2017, 11:43 AM
gsandy gsandy is offline Select Text after Bookmark Windows 7 64bit Select Text after Bookmark Office 2013
Novice
Select Text after Bookmark
 
Join Date: Mar 2017
Location: New Zealand
Posts: 10
gsandy is on a distinguished road
Default

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.
Reply With Quote
Reply



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
Select Text after Bookmark Word 2003 - IncludeText Does Not Include Bookmark Text if in a Form Text Control skarden Word 1 12-12-2011 10:39 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:03 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft