#1
|
|||
|
|||
Getting bold Pargraph
Hi, I'm new to Word VBA but playing around with this
Code:
Sub ShowPara() Stop Dim p As Paragraph, pp As String, MyParaNum As Integer Dim pp2 As String For Each p In ActiveDocument.Paragraphs MyParaNumber = MyParaNumber + 1 pp = p.Range.Text If Not p.Range.End Then pp2 = p.Range.Next.Paragraphs(1).Range.Text End If Next End Sub I want to get each next paragraph after any paragraph that is bolded. Is that possible ? After finding I want to do some checks on it, then maybe replace it. I think it's p.Range.Text = pp (after pp has been modified). Thanks. |
#2
|
||||
|
||||
There is nothing in your code that tests whether a paragraph is bold. Do note that you can use Find/Replace for finding bold content and that testing whether a 'paragraph' is bold presupposes everything, including the paragraph break, is bold (or not). This can be done with or without VBA.
Unless you say what you're trying to achieve, we can't give more specific advice; the solution may or may not require a loop to implement.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
It is not exactly clear what
Quote:
Code:
Sub ShowPara() Dim oPara As Paragraph, sText As String, oRng As Range For Each oPara In ActiveDocument.Paragraphs If oPara.Range.Bold = True Then If Not oPara.Range.End = ActiveDocument.Range.End Then Set oRng = oPara.Range.Next.Paragraphs(1).Range If oRng.Bold = False Then sText = oRng.Text oRng.Select MsgBox sText End If End If End If Next oPara lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
||||
|
||||
And if either the bold paragraph or the non-bold paragraph is differentiated by a Style, using Find/Replace for finding the Style(s) might be rather more efficient.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Thank you Paul and Graham (your code works for me).
But when I do stuff with the paragraph, I have to Stop the code and when restarted it goes through those paragraphs a second time. Can I start it where I want e.g paragraph number 300 ? I could write that into the code each time; it doesn't need to be automatic. I am working through the document fixing OCR errors manually. |
#6
|
||||
|
||||
You could just tweak Graham's code a bit, then select the range you want to work with before running the macro:
Code:
Sub ShowPara() Dim oPara As Paragraph, sText As String, oRng As Range With Selection For Each oPara In .Paragraphs If oPara.Range.Bold = True Then If Not oPara.Range.End = .Paragraphs.Last.Range.End Then Set oRng = oPara.Range.Next.Paragraphs(1).Range If oRng.Bold = False Then sText = oRng.Text oRng.Select MsgBox sText End If End If End If Next oPara End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
That just ends immediately. Does something need to setup .Paragraphs.Last.Range.End ?
|
#8
|
||||
|
||||
Did you select the range you want to process?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
ah, sorry - no. You mean like Control-A ?
|
#10
|
||||
|
||||
Ctrl-A will select the entire document. Instead of doing that, which is no better than using Graham's original code, you might just select a particular range with the mouse, or position the cursor where you want to start from and use Ctrl-Shift-End to select from there to the end of the document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Thanks Paul. Understood. I'm now trying some more advanced methods (using a Form) and seeing what I can make work.
In the line Set oRng = oPara.Range.Next.Paragraphs(1).Range What does the (1) mean ? |
#12
|
||||
|
||||
If a range object spans multiple paragraphs, you can change the 1 to 2, 3, etc. to specify which paragraph in the range to work with. With the way oRng is set in the code you're using, you can only use 1. You could also use:
Set oRng = oPara.Range.Next.Paragraphs.First.Range or: Set oRng = oPara.Range.Next.Paragraphs.Last.Range
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Adding bold colons at end of bold row headings | bertietheblue | Word Tables | 2 | 07-26-2015 07:26 AM |
Text in #1 is made bold, rest of the document is edited, text in #1 is now not bold | footer-assistance | Word | 1 | 06-29-2015 03:49 AM |
Making Bold Using 'IF' | bigukfan | Mail Merge | 3 | 03-10-2014 02:11 PM |
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 |