#1
|
|||
|
|||
Create bookmark from comma separated text
I have a list of factors in a paragraph separated by a comma. I would like to select the factors and then run a macro to create bookmarks in sequence using an inputbox to ask for the beginning text of the bookmark.
Such as example: In the paragraph, X1, X2, X3, X4, X5, X6. After selecting those six factors, the result would by six bookmarks beginning with the title entered in the inputbox. CON_1 CON_2 CON_3 CON_4 CON_5 CON_6 |
#2
|
|||
|
|||
Something like this:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim varFactors Dim lngIndex As Long Dim strFind As String Dim oRng As Range varFactors = Split(Selection.Text, ",") For lngIndex = 0 To UBound(varFactors) strFind = InputBox("Enter the string to find.") Set oRng = ActiveDocument.Range With oRng.Find .Text = strFind .MatchCase = True If .Execute Then 'If you need to expand the range of the defined text do it here before applying the BM oRng.Bookmarks.Add Trim(varFactors(lngIndex)), oRng End If End With Next lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Hi Greg and thank you for the help,
Maybe this isn't quite it so I've attached an example. For the first string, I've created the bookmarks manually and they are CONS1 thru CONS6. The second string of factors could be WC1 thru WC8 if that's what the user enters in the inputbox. Essentially the string is everything in the selection separated by the comma. Later in the document I will use these bookmarks to create a cross reference. |
#4
|
|||
|
|||
Jeffery,
I'm sorry I can't help more but I just don't understand what you are trying to do. |
#5
|
|||
|
|||
Does anybody else have an idea? Basically instead of manually creating a bunch of bookmarks, I would like VBA to create bookmarks from the selected text which is separated by a comma. The name can simple be in sequence which would be proceeded by the word entered into an inputbox by the user.
|
#6
|
|||
|
|||
Almost anyone could help if you would provide a clear explanation of what you want.
Post a document showing some text, what you want selected and what you want the result to be after the code runs. |
#7
|
|||
|
|||
Sorry about that, but sure thought the attachment in post #3 had the clear explanation and example.
The user is going to highlight X1, X2, X3, X4, X5, X6 which is in a paragraph. When the code runs, the user will be prompted via an inputbox to add a descriptor for the bookmarks. In the sample, the user highlights X1, X2, X3, X4, X5, X6 and then upon running the macro they enter CONS in the inputbox. After the code runs there are now 6 bookmark titled, CONS1, CONS2, CONS3, etc which is in the sample. I'm not sure what part of the explanation I am missing, but hopefully this completes it. Thank you for your help. |
#8
|
|||
|
|||
Based on what you have shown and described:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim varFactors Dim lngIndex As Long Dim strFind As String Dim oRng As Range Dim strDesc As String varFactors = Split(Selection.Text, ",") strDesc = InputBox("Enter a decriptor") For lngIndex = 0 To UBound(varFactors) Set oRng = ActiveDocument.Range With oRng.Find .Text = Trim(varFactors(lngIndex)) .MatchCase = True While .Execute If oRng.InRange(Selection.Range) Then 'If you need to expand the range of the defined text do it here before applying the BM oRng.Bookmarks.Add strDesc & lngIndex + 1, oRng End If oRng.Collapse wdCollapseEnd Wend End With Next lbl_Exit: Exit Sub End Sub |
#9
|
|||
|
|||
This is absolutely perfect Greg. Thank you for your time.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
using bookmark feature in Word to create index | Indexer | Word | 6 | 02-19-2017 12:28 PM |
Create bookmark and use it in macrobutton | fusion001 | Word VBA | 3 | 05-28-2015 02:00 AM |
Nest IF statements with a text comma inbetween each IF statement | AClaborn | Excel | 2 | 01-20-2015 12:07 PM |
Word 2003 - IncludeText Does Not Include Bookmark Text if in a Form Text Control | skarden | Word | 1 | 12-12-2011 10:39 PM |
adding text on a separated column line | ahmet1985 | Word | 0 | 03-27-2010 07:10 AM |