Microsoft Office Forums How to parse bold sections of a paragraph?

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2014, 06:26 AM
XmisterIS XmisterIS is offline How to parse bold sections of a paragraph? Windows 7 64bit How to parse bold sections of a paragraph? Office 2007
Novice
How to parse bold sections of a paragraph?
 
Join Date: Sep 2013
Posts: 12
XmisterIS is on a distinguished road
Default 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
This gives an output like:

Quote:
This is the first paragraph
Bold: 0

This is the second paragraph
Bold: -1

This is the third paragraph
Bold: 9999999
In fact, in the 3rd para, the word "third" is bolded, all the rest are not.



How can I programmatically discover which parts (text runs?) within each para have had additional formatting applied?
Reply With Quote
  #2  
Old 03-31-2014, 02:22 PM
Larry Sulky Larry Sulky is offline How to parse bold sections of a paragraph? Windows 7 64bit How to parse bold sections of a paragraph? Office 2010 64bit
Novice
 
Join Date: Mar 2014
Posts: 14
Larry Sulky is on a distinguished road
Default

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
Obviously, you wouldn't just set specific values for the indexes para, word, and char; you would nested-loop through them (like your original sample shows for paragraphs).
Reply With Quote
  #3  
Old 03-31-2014, 02:54 PM
macropod's Avatar
macropod macropod is offline How to parse bold sections of a paragraph? Windows 7 32bit How to parse bold sections of a paragraph? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,533
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to parse bold sections of a paragraph? 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
How to parse bold sections of a paragraph? 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


All times are GMT -7. The time now is 06:07 AM.


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