Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-16-2019, 03:14 PM
macropod's Avatar
macropod macropod is offline Extract Bold text from string Windows 7 64bit Extract Bold text from string Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,387
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

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]
Reply With Quote
  #2  
Old 04-16-2019, 05:22 PM
one4youman one4youman is offline Extract Bold text from string Windows 7 32bit Extract Bold text from string Office 2010
Novice
Extract Bold text from string
 
Join Date: Apr 2019
Posts: 4
one4youman is on a distinguished road
Default

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

Reply With Quote
  #3  
Old 04-16-2019, 05:25 PM
macropod's Avatar
macropod macropod is offline Extract Bold text from string Windows 7 64bit Extract Bold text from string Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,387
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 one4youman View Post
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.
The approach I suggested has nothing to do with changing how anything appears.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 04-17-2019, 07:23 AM
one4youman one4youman is offline Extract Bold text from string Windows 7 32bit Extract Bold text from string Office 2010
Novice
Extract Bold text from string
 
Join Date: Apr 2019
Posts: 4
one4youman is on a distinguished road
Default

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




Reply With Quote
Reply

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 Bold text from string Extract text from a text string Haha88 Excel 8 02-13-2017 05:06 PM
Extract Bold text from string 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:46 AM.


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