#1
|
|||
|
|||
Extract Bold text from string
I am attempting to find a way to extract the first continuous bold words from a string that is created by the text contained within a bookmark.
For Example if String X = “Thequick brown fox jumps over the lazy dog” I would like to output “The quick brown fox” to String Y but not include “The lazy”, or for example if String X = “1. The quick brown fox jumps over the lazy dog” I would like to output “1. Thequick” to String Y but not include the remainder of the original string. The string text will always begin with the bold text I am looking to extract but will often contain other bolded words that I do not need. Thank you in advance, John |
#2
|
||||
|
||||
Since you apparently have multiple bolded strings, what differentiates the ones you're interested in from the others?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
I am working on trying to create a template letter that uses bookmarks to generate a UserForm and then selections on the UserForm are then used to update the template.
The letter contains language associated with multiple different contracts (Contract 1, Contract 2, Contract 3, etc.) which each contain 15 to 25 different contractual terms. Within the UserForm you would select the Contract from a dropdown Combo Box and based on this selection the 15 to 25 different contractual terms would load as a selectable CheckBox. After selecting the CheckBoxes that you want to cite in the letter, you would click an OK button which would clear any terms that are not selected. The language and selections are different for each contract, and contain very specific language / format that need to be followed. The letter template would initially contain all of the contracts and based on the UserForm the contracts that are not selected are deleted followed by the terms that are not selected and achieved by creating bookmarks for each of the terms that will also be contained within a bookmark for the contract. The initial template text would be as follows: <Begin Bookmark Contract1> Introduction text / message about the contract. <Begin Bookmark Contract1Term1> Term 1. Explanation of the term. <End Bookmark Contract1Term1> <Begin Bookmark Contract1Term2> Term 2. Explanation of the term. <End Bookmark Contract1Term2> <Begin Bookmark Contract1Term3> Term 3. Explanation of the term.: · Explanation of the term. · Explanation of the term. · Explanation of the term. <End Bookmark Contract1Term3> <End Bookmark Contract1> <Begin Bookmark Contract2> Introduction text / message about the contract. <Begin Bookmark Contract2Term1> 1. Name of term1 a. Part A Explanation of the term: (1) Explanation of the term; (2) Explanation of the term; b. Part B Explanation of the term. <End Bookmark Contract2Term1> <Begin Bookmark Contract2Term2> 2. Name of term2 a. Part A Explanation of the term: (1) Explanation of the term; (2) Explanation of the term; b. Part B Explanation of the term. <End Bookmark Contract2Term2> <End Bookmark Contract2> UserForm / Script: On the UserForm I have 25 CheckBox’s (Labeled 1 through 25) listed and I would like the caption to be updated to reflect a portion of the term. I have been able to script this to obtain the text to the left of the first period, however on some of the contracts the term starts with a number then a period followed by the term text. Ideally I would like to extract only the first portion of the string that appears in Bold without extracting any other portions of the string that may also appear in Bold. I would then like to pass the remainder of the string to the ControlTipText field so the text can be seen during a mouse over of the text box. I also want to pass the string source (Name of the bookmark) to the Checkbox Tag so I can use it to later reference the Bookmark name to determine which book marks to keep, and which to delete. If no bookmark is attached to the checkbox during the loop, I will then hide the checkbox from the Userform. Any suggestions on how to change the below code to extract only the first portion of the bold text from each bookmark in the below script to pass it to the caption? Code:
Private Sub CommandButton8_Click() Dim bmk As Bookmark Dim i As Long Dim P As String Dim CovOutput As String Dim msg As String i = 1 For Each bmk In ActiveDocument.Bookmarks("Contract1").Range.Bookmarks CovOutput = bmk.Name P = i Controls("CheckBox" & P).Tag = bmk.Name 'Set Tag equal to the bookmark name - Extract Tag to determine which to remove on false statement. Controls("CheckBox" & P).Caption = Left(ActiveDocument.Bookmarks(CovOutput).Range.Text, InStr(ActiveDocument.Bookmarks(CovOutput).Range.Text, ".")) 'Text Prior to 1st Period Controls("CheckBox" & P).ControlTipText = Mid(ActiveDocument.Bookmarks(CovOutput).Range.Text, (InStr(ActiveDocument.Bookmarks(CovOutput).Range.Text, ".") + 1)) 'Remaining text from the string msg = msg & bmk.Name & vbCr 'Adds bookmark names to Msgbox to use during debug. i = (i + 1) On Error GoTo ErrorStop Next bmk ErrorStop: 'Uncomment for Debug 'MsgBox msg End Sub The above works to extract the text based on the first period, but will not work for all of the contracts as each is slightly different. Instead I would like to extract the first few words that appear in bold within the bookmark and then pass the text to the caption. IE: <Begin Bookmark Contract1Term1> Term 1. Explanation of the term. <End Bookmark Contract1Term1> would pass text "Term 1." to the caption. OR <Begin Bookmark Contract2Term1> 1. Name of term1 a. Part A Explanation of the term: (1) Explanation of the term; (2) Explanation of the term; b. Part B Explanation of the term. <End Bookmark Contract2Term1> would pass text "1. Name of term1" to the caption. Thanks, John Last edited by one4youman; 04-16-2019 at 09:41 AM. |
#4
|
||||
|
||||
If you used Word's Heading Styles for your Term text (including with Style separators if you need run-in text of the same line), you wouldn't need to worry about which bold text to find or bookmarks. Instead, you could simply reference the headings and their associated ranges.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Unfortunately I do not think I can do it that way due to the specific way the contracts are written as I cannot change how they currently appear within the template for compliance reasons.
What I could however do is highlight the portion of the term and then somehow use Selection.Find to extract the highlighted text then pass to the caption. Once the form has been completed and all Bookmarks are update based on the UserForm, I would execute another sub to clear any yellow highlighted text from the document. I have been trying to figure out how to get the below to do this but cannot seem to figure out a good solution. Code:
Dim bmk As Bookmark Dim i As Long Dim P As String Dim CovOutput As String Dim msg As String i = 1 For Each bmk In ActiveDocument.Bookmarks("Contract1").Range.Bookmarks '************************* 'Find the highlighted text '************************* If BkMkCount = 1 Then With Selection.Bookmarks(1) BkMkName = .Name .Select With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .MatchWildcards = False .Forward = True .Wrap = wdFindContinue .Highlight = True Do .Execute Loop Until Selection.Range.HighlightColorIndex = wdYellow _ Or Not .Found HighlightedText = Selection.Range '.Select End With MsgBox msg MsgBox BkMkName End With End If '************************* CovOutput = bmk.Name P = i Controls("CheckBox" & P).Tag = bmk.Name 'Set Tag equal to the bookmark name - Extract Tag to determine which to remove on false statement. Controls("CheckBox" & P).Caption = HighlightedText 'Highlighted text located within the bookmark. Controls("CheckBox" & P).ControlTipText = Mid(ActiveDocument.Bookmarks(CovOutput).Range.Text, (InStr(ActiveDocument.Bookmarks(CovOutput).Range.Text, ".") + 1)) 'Remaining text from the string msg = msg & bmk.Name & vbCr 'Adds bookmark names to Msgbox to use during debug. i = (i + 1) On Error GoTo ErrorStop Next bmk ErrorStop: 'Uncomment for Debug 'MsgBox msg End Sub Thanks, John |
#6
|
||||
|
||||
The approach I suggested has nothing to do with changing how anything appears.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Any suggestions on how the code should look to pull the Heading styles?
Ideally, I would select the contract from a drop down combo box (Named cboContractForm) which corresponds to the bookmark that surrounds contract 1, contract 2, etc... click a button named cmdLoad, which then loops through the bookmarks contained / nested within the contract bookmark and loads the heading text from each bookmark to define the caption. It would be similar to the following, but I am struggling to figure out how to setup the code to achieve this. Code:
Private Sub UserForm_Initialize() With cboContractForm .AddItem "Contract 1" .AddItem "Contract 2" End With End Sub Based on the selected contract, update the check boxes for each term: Code:
Private Sub cmdLoad_Click() Dim Contract as String If cboContractForm = "Contract 1" Then Contract = “Contract1” ‘Name of Bookmark Range If cboContractForm = "Contract 2" Then Contract = “Contract2” ‘Name of Bookmark Range Dim bmk As Bookmark Dim i As Long Dim P As String Dim CovOutput As String Dim msg As String i = 1 For Each bmk In ActiveDocument.Bookmarks(Contract).Range.Bookmarks 'Bookmark for entire contract - Set based on cboContractForm combobox. CovOutput = bmk.Name P = i Controls("CheckBox" & P).Tag = bmk.Name 'Set Tag equal to the bookmark that corresponds with the term being loaded into the Userform. If the text box is false I will use the tag name to determine which bookmarks to clear from the form. Controls("CheckBox" & P).Caption = Word 's Heading Styles for your Term text? 'Heading Text from within the found / looped bookmark. Controls("CheckBox" & P).ControlTipText = bmk 'Mouse over to show the entire term msg = msg & bmk.Name & vbCr i = (i + 1) On Error GoTo ErrorStop Next bmk ErrorStop: 'Uncomment for Debug 'MsgBox msg End Sub After selecting the applicable text boxes, click OK to update the letter. Code:
Private Sub cmdOK_Click() On Error Resume Next With ActiveDocument If cboContractForm = "Contract 1" Then .Bookmarks("Contract1").Range.Text = "" If cboContractForm = "Contract 2" Then .Bookmarks("Contract2").Range.Text = "" Else 'Remove terms that do not apply If CheckBox1 = False Then .Bookmarks("CheckBox1.Tag.Value").Range.Text = "" If CheckBox2 = False Then .Bookmarks("CheckBox2.Tag.Value").Range.Text = "" If CheckBox3 = False Then .Bookmarks("CheckBox3.Tag.Value").Range.Text = "" If CheckBox4 = False Then .Bookmarks("CheckBox4.Tag.Value").Range.Text = "" 'etc for each check box End If End With End Sub I have never used header styles to pass values / extract text and I am not quite sure how to obtain values or reference ranges. The above codes do not work, but help to explain my thought process. Any updates would be greatly appreciated. Thank you, John |
#8
|
||||
|
||||
The code to get all headings of a particular Style might look like:
Code:
Private Sub UserForm_Initialize() With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .Style = wdStyleHeading1 .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found cboContractForm.AddItem Split(.Range.Text, vbCr)(0) .Collapse wdCollapseEnd .Find.Execute Loop End With End Sub Having chosen the contract, you might then use code like the following to get its items: Code:
Sub Get_Items() Dim Rng As Range With ActiveDocument With .Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = cboContractForm.Value .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .Style = wdStyleHeading1 .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Set Rng = .Duplicate Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel") With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .Style = wdStyleHeading2 .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found If .InRange(Rng) Then 'Code to populate checkbox captions goes here Else Exit Do End If .Collapse wdCollapseEnd .Find.Execute Loop End With End With End Sub Code:
Sub Delete_Contracts() Application.ScreenUpdating = False Dim Rng As Range With ActiveDocument With .Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .Style = wdStyleHeading1 .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found If Split(.Text, vbCr) <> cboContractForm.Value Then Set Rng = .Duplicate Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel") Rng.Text = vbNullString End If .Collapse wdCollapseEnd .Find.Execute Loop End With End With Application.ScreenUpdating = True End Sub Then, when you've selected the items, you might use code like the following to delete any unwanted items: Code:
Sub Delete_Items() Application.ScreenUpdating = False Dim Ctrl As Control, StrItems As String, Rng As Range For Each Ctrl In Me.Controls With Ctrl If .Name Like "ContractItem#*" Then If .Value = 0 Then If .Caption = "" Then StrItems = StrItems & "|" & .Caption & "|" End If End If End If End With Next MsgBox StrItems Exit Sub With ActiveDocument With .Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .Style = wdStyleHeading2 .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found If InStr(StrItems, "|" & Split(.Text, vbCr) & "|") > 0 Then Set Rng = .Duplicate Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel") Rng.Text = vbNullString End If .Collapse wdCollapseEnd .Find.Execute Loop End With End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
||||
|
||||
Cross-posted at: https://www.mrexcel.com/forum/genera...xt-string.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
bold, extract, string |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Formula to Extract text from a text string | Haha88 | Excel | 2 | 11-14-2017 01:32 AM |
Extract text from a text string | Haha88 | Excel | 8 | 02-13-2017 05:06 PM |
Extract a string from a paragraph | kirkm | Word VBA | 7 | 09-11-2016 06:13 PM |
Extract numbers from a text string | aleale97 | Excel | 4 | 02-10-2011 10:33 AM |
Extract from String using Wildcard | whousedmy | Word | 0 | 05-21-2009 01:35 AM |