MS Office Forum / Word / Programming / March 2005
Create Array of Bookmarks
|
|
Thread rating:  |
Bill Foley - 24 Mar 2005 18:51 GMT Hey Gang, Word VBA-Wanna-Be here:
I am creating a large document that I can use to create smaller documents by bookmarking text. Basically, I want to have four-levels of text that I bookmark by selecting the paragraph(s), clicking a toolbar button that opens a UserForm, providing a OptionButton group of the four choices, where you select the choice, then click OK. That will assign a new bookmark name to the selected text. For example, a_1, a_2. Selecting other text and choosing another level will generate bookmarks named b_1, b_2, etc.
Once I have the entire document (actually a template) bookmarked, I save it. I have an AutoNew macro that opens up another UserForm providing the user with four options of documents to create. If the user chooses "Level 1", I want all of the "non-applicable" bookmarks deleted. Assuming Level 1 documents have bookmarks named "a_1", a_2", etc., I would want to get rid of all the "b, c, and d" bookmarks.
My questions are two fold:
1. How do I use VBA to name bookmarks for level "a" to make them "a_1", "a_2", etc.? I know how to assign a name itself, but not to add 1 to a starting point.
2. How would I generate the array, search for all bookmarks "not" like the chosen one, then delete them?
Thanks in advance!
 Signature Bill Foley, Microsoft MVP (PowerPoint) Microsoft Office Specialist Master Instructor - XP www.pttinc.com Check out PPT FAQs at: http://www.rdpslides.com/pptfaq/ Check out Word FAQs at: http://word.mvps.org/FAQs/General/index.htm
Jean-Guy Marcil - 24 Mar 2005 22:22 GMT Bill Foley was telling us: Bill Foley nous racontait que :
> Hey Gang, Word VBA-Wanna-Be here: > [quoted text clipped - 20 lines] > "a_1", "a_2", etc.? I know how to assign a name itself, but not to > add 1 to a starting point. I do not understand what you mean here. What starting point? Does "1" mean 1 as in a digit, or "one" as another one?
> 2. How would I generate the array, search for all bookmarks "not" > like the chosen one, then delete them? I don't think you need an array. Wouldn't something like this work:
'_______________________________________ Dim MyBook As BookMark
For Each MyBook in ActiveDocument.Bookmarks If InStr(1, MyBook.Name, "a_") = 0 Then MyBook.Range.Delete End If Next '_______________________________________
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
Bill Foley - 25 Mar 2005 04:02 GMT Jean,
Sorry if I was confusing. To clarify, I want to increment a bookmark number as I create new ones. The only reason I was using an array is that is what I had way back when I first started this using WordBasic. The array part seemed to be working, but I couldn't figure out the incrementing part.
The original code created the UserForm via code, assigned a number and name to the bookmark based on which option button was selected. I am trying to get rid of all Wordbasic stuff and have been trying to convert it into VBA. I am going to get rid of the coded UserForm and focus on create it myself. I am also going to use a Select case to determine which option button was selected. The part I can't figure out is how to increment each new bookmark.
I have included the original code below:
Public Sub MAIN() Dim mytype$ Dim numBookmarks On Error GoTo byebye
WordBasic.BeginDialog 204, 116, "Identify Section" WordBasic.GroupBox 23, 6, 142, 85, "Mark As" WordBasic.OptionGroup "OptionGroup1" WordBasic.OptionButton 35, 18, 100, 16, "PEO Initial", "OptionButton1" WordBasic.OptionButton 35, 36, 123, 16, "PEO Requal", "OptionButton2" WordBasic.OptionButton 35, 54, 105, 16, "LO Initial", "OptionButton3" WordBasic.OptionButton 35, 72, 105, 16, "LO Requal", "OptionButton4" WordBasic.CancelButton 6, 92, 88, 21 WordBasic.OKButton 112, 92, 88, 21 WordBasic.EndDialog
Dim dlg As Object: Set dlg = WordBasic.CurValues.UserDialog WordBasic.Dialog.UserDialog dlg
If dlg.OptionGroup1 = 0 Then mytype$ = "poi_": WordBasic.FormatFont Color:=6
If dlg.OptionGroup1 = 1 Then mytype$ = "por_": WordBasic.FormatFont Color:=2
If dlg.OptionGroup1 = 2 Then mytype$ = "loi_": WordBasic.FormatFont Color:=5
If dlg.OptionGroup1 = 3 Then mytype$ = "lor_": WordBasic.FormatFont Color:=3
numBookmarks = WordBasic.CountBookmarks()
WordBasic.EditBookmark Name:=mytype$ + WordBasic.[LTrim$](Str(numBookmarks + 1)), SortBy:=0, Add:=1
Exit Sub
byebye:
Exit Sub
End Sub
=================
Thanks for your help!
 Signature Bill Foley, Microsoft MVP (PowerPoint) Microsoft Office Specialist Master Instructor www.pttinc.com Check out PPT FAQs at: http://www.rdpslides.com/pptfaq/ "Success, something you measure when you are through succeeding."
> Bill Foley was telling us: > Bill Foley nous racontait que : [quoted text clipped - 42 lines] > Next > '_______________________________________ Andrew - 25 Mar 2005 02:37 GMT To append a number to a bookmark all you have to do is append an incrementing variable.
Dim sBmName As String Dim i As Integer
i = 0
Do i = i + 1 ' increments i
' Determine bookmark name here and assign it to sBmName...
sBmName = sBmName & "_" & i ' append number With ActiveDocument.Bookmarks .Add Range:=Selection.Range, Name:=sBmName End With Loop
Naturally you'll need to provide a way to exit the loop. But presumably you already have that bit.
Good Luck
> Hey Gang, Word VBA-Wanna-Be here: > [quoted text clipped - 23 lines] > > Thanks in advance! Bill Foley - 28 Mar 2005 16:32 GMT Thanks Andrew, but I'm not looping through a bunch of existing bookmarks and adding numbers to them. What I am trying to do is to add a new bookmark name that is one more then a previously named bookmark.
For example, I select a range of text, click a toolbar button, opening a userform, selecting an optionbutton, then clicking the OK button. What I am trying to do with that code is create a bookmark named (for example) "poi_1". Later on in the document I select more text, repeat the steps above hoping to get a bookmark named "poi_2". Later on I select text, choose a different optionbutton, and want it named "loi_1".
I tried doing a bookmark count and incrementing that way but it looked at all bookmarks and added a number way too high (I actually have four varieties of bookmarks in this document).
I was hoping I could accomplish this using the below code below, but what happened the second time I selected text was it deleted the previous bookmark and named this one with that same name. Not having done this since Wordbasic, I am trying to find the same syntax with VBA.
The code I am working on is:
==============
Dim i As Integer i = 1
If OptionButton1.Value = True Then ' what do I need here to check the highest numbered "poi" bookmark????? With ActiveDocument.Bookmarks .Add Range:=Selection.Range, Name:="poi_" & i End With
==============
The next block of text I select and name needs to know that "poi_1" exists and call it "poi_2". I'm sure it is something simple, but VBA is not a strong point of mine.
TIA for any assistance you can provide!
 Signature Bill Foley, Microsoft MVP (PowerPoint) Microsoft Office Specialist Master Instructor - XP www.pttinc.com Check out PPT FAQs at: http://www.rdpslides.com/pptfaq/ Check out Word FAQs at: http://word.mvps.org/FAQs/General/index.htm
> To append a number to a bookmark all you have to do is append an incrementing > variable. [quoted text clipped - 47 lines] > > > > Thanks in advance! Chuck - 29 Mar 2005 18:27 GMT You can count the instances of the root of the bookmark you want to increment. For instance if you want to add a "poi" bookmark then "poi" is the root ("poi"+1, "poi"+2) etc. So you can just loop through all the bookmarks in the document, count the ones that start with "poi", increment your counter and then add your bookmark with the updated counter.
i = 0 strRoot = "poi"
For Each Bookmark In ActiveDocument.Bookmarks If Left(Bookmark.Name, 3) = strRoot Then i = i + 1 End If Next Bookmark
With ActiveDocument.Bookmarks .Add Range:=Selection.Range, Name:=strRoot & "_" & i + 1 End With
Of course you'd need to write code to assign the value of strRoot.
> Thanks Andrew, but I'm not looping through a bunch of existing bookmarks and > adding numbers to them. What I am trying to do is to add a new bookmark [quoted text clipped - 98 lines] > > > > > > Thanks in advance! Bill Foley - 29 Mar 2005 19:03 GMT Thanks, Chuck. I'll give it a go.
 Signature Bill Foley, Microsoft MVP (PowerPoint) Microsoft Office Specialist Master Instructor - XP www.pttinc.com Check out PPT FAQs at: http://www.rdpslides.com/pptfaq/ Check out Word FAQs at: http://word.mvps.org/FAQs/General/index.htm
> You can count the instances of the root of the bookmark you want to > increment. For instance if you want to add a "poi" bookmark then "poi" is [quoted text clipped - 119 lines] > > > > > > > > Thanks in advance! Bill Foley - 29 Mar 2005 19:33 GMT Works a treat! THANKS!
 Signature Bill Foley, Microsoft MVP (PowerPoint) Microsoft Office Specialist Master Instructor - XP www.pttinc.com Check out PPT FAQs at: http://www.rdpslides.com/pptfaq/ Check out Word FAQs at: http://word.mvps.org/FAQs/General/index.htm
> You can count the instances of the root of the bookmark you want to > increment. For instance if you want to add a "poi" bookmark then "poi" is [quoted text clipped - 119 lines] > > > > > > > > Thanks in advance!
|
|
|