Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-25-2011, 09:06 AM
b0x4it b0x4it is offline Putting a bookmark on a REF field in the selection Windows 7 32bit Putting a bookmark on a REF field in the selection Office 2010 32bit
Advanced Beginner
Putting a bookmark on a REF field in the selection
 
Join Date: May 2011
Posts: 95
b0x4it is on a distinguished road
Default Putting a bookmark on a REF field in the selection

I am trying to find a REF field in the selected text and adding a bookmark to it using this code:



Code:
Sub AddBookmark()
    Dim oStoryRng As Range
    Dim oFld As Field
    For Each oFld In Selection.Fields
        If oFld.Type = wdFieldRef Then
            ActiveDocument.Bookmarks.Add Range:=oFld, Name:="test"
            oFld.Update
        End If
    Next oFld
End Sub
But when I select the text that includes the field and then run this code, nothing happens and no bookmark is added. Would you please let me know what is wrong with this code?
Reply With Quote
  #2  
Old 05-25-2011, 11:52 PM
macropod's Avatar
macropod macropod is offline Putting a bookmark on a REF field in the selection Windows 7 32bit Putting a bookmark on a REF field in the selection Office 2007
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Hi b0x4it,

Why would you want to bookmark a cross-reference field? surely you could simply insert another cross-reference wherever else you need one (eg via Insert\Cross-reference or by copying & pasting the existing one)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 05-26-2011 at 02:00 AM.
Reply With Quote
  #3  
Old 05-25-2011, 11:58 PM
b0x4it b0x4it is offline Putting a bookmark on a REF field in the selection Windows 7 32bit Putting a bookmark on a REF field in the selection Office 2010 32bit
Advanced Beginner
Putting a bookmark on a REF field in the selection
 
Join Date: May 2011
Posts: 95
b0x4it is on a distinguished road
Default

That's right. But I am trying to learn how I can put a bookmark on a field that is in the current line, table or paragraph. This is just a sample, I want to learn how I can do that using VBA. In fact it would be SEQ instead of REF, but doesn't matter and I can simply change the working code to put bookmark on whatever field I want. any idea?
Reply With Quote
  #4  
Old 05-26-2011, 12:51 AM
macropod's Avatar
macropod macropod is offline Putting a bookmark on a REF field in the selection Windows 7 32bit Putting a bookmark on a REF field in the selection Office 2007
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Try:
Code:
Sub AddBookmark()
Dim oFld As Field, Rng As Range
For Each oFld In Selection.Fields
  If oFld.Type = wdFieldRef Then
    Set Rng = oFld.Result
    Rng.End = Rng.End + 1
    ActiveDocument.Bookmarks.Add Range:=Rng, Name:="test"
    Exit For
  End If
Next oFld
Set Rng = Nothing
End Sub
Note the 'Exit For'. This serves two purposes, it:
1. avoids needlessly checking any subsequent fields; and
2. prevents any attempt to re-use the bookmark - only one instance of a given bookmark can exist.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-26-2011, 01:14 AM
b0x4it b0x4it is offline Putting a bookmark on a REF field in the selection Windows 7 32bit Putting a bookmark on a REF field in the selection Office 2010 32bit
Advanced Beginner
Putting a bookmark on a REF field in the selection
 
Join Date: May 2011
Posts: 95
b0x4it is on a distinguished road
Default

Hi Paul,

It works perfectly. I think the problem with my code was "Set Rng = oFld.Result"

Thank you.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Putting a bookmark on a REF field in the selection Highlight and put bookmark on the closest field to the cursor b0x4it Word VBA 11 05-19-2011 11:15 PM
Putting a bookmark on a REF field in the selection Question about putting editable inscriptions over a picture in RTF 62mkv Drawing and Graphics 4 03-20-2011 10:32 PM
Putting a bookmark on a REF field in the selection Form Field - Drop down selection causing auto text chesspupil Word VBA 7 05-09-2010 05:43 AM
Putting periods/dots around the letter A ph3iron Word 0 03-27-2010 06:11 AM
Read Receipt option on even after putting it off ran_sushmi Outlook 0 04-17-2009 06:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:57 AM.


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