#1
|
|||
|
|||
How to parse bold sections of a paragraph?
I am writing a piece of code which parses the paragraphs of a document and uploads relevant bits to a server.
Consider a document in which three paragraphs exist, with Normal style. The first para contains no additional formatting. The second para is entirely bolded. One word only within the third para is bolded. Now, consider the following macro: Code:
Public Sub paratest() Dim i As Integer For i = 1 To ActiveDocument.Paragraphs.Count With ActiveDocument.Paragraphs.Item(i) Debug.Print .Range.Text Debug.Print "Bold: ".Range.Bold End With Next End Sub Quote:
How can I programmatically discover which parts (text runs?) within each para have had additional formatting applied? |
#2
|
|||
|
|||
You're on the right track. First find which paragraphs are wholly bold (or italic, or whatever), which ones are partially, and which ones are not. Then for the partial ones, use a similar kind of logic to discover which words are wholly, partially, or not bolded etc. Then for those partial ones, go down to the character leve. In every case, 0 means no, -1 means yes, and 9999999 means partial. You'll need to examine properties as shown on the debug.print statements below (fully expressed for clarity, but ugly):
Code:
Dim para As Integer Dim word As Integer Dim char As Integer para = 3 word = 6 char = 5 Debug.Print ActiveDocument.Paragraphs.Item(para).Range.Bold Debug.Print ActiveDocument.Paragraphs.Item(para).Range.Words.Item(word).Bold Debug.Print ActiveDocument.Paragraphs.Item(para).Range.Words.Item(word).Characters(char).Bold |
#3
|
||||
|
||||
For a different way of approaching the problem, try something along the lines of:
Code:
Sub Demo() Application.ScreenUpdating = False Dim StrOut As String With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .Font.Bold = True .MatchWildcards = False .Execute End With Do While .Find.Found StrOut = StrOut & vbCr & .Text .Collapse wdCollapseEnd .Find.Execute Loop End With While InStr(StrOut, vbCr & vbCr) > 0 StrOut = Replace(StrOut, vbCr & vbCr, vbCr) Wend Application.ScreenUpdating = True MsgBox "The document contains the following bold text:" & StrOut End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Not Bold text but it comes up bold | Pluviophile | Word | 7 | 10-22-2013 10:29 AM |
Format Bold in one line makes all lines bold | Nitte | Word | 2 | 02-07-2013 12:34 AM |
Set paragraph text to bold. | Sorcerer13 | Word | 1 | 09-04-2012 11:08 AM |
Export Outlook emails to Access & parse certain text elements | smahale | Outlook | 0 | 01-18-2012 09:30 PM |
parse, store and email a .tmp file from wordpad? | scadaman29325 | Office | 0 | 09-28-2010 04:56 PM |