Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-18-2016, 08:09 PM
kirkm kirkm is offline Getting bold Pargraph Windows XP Getting bold Pargraph Office 2003
Advanced Beginner
Getting bold Pargraph
 
Join Date: Aug 2013
Posts: 40
kirkm is on a distinguished road
Default 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
Don't know if I need MyParaNum there may be another way but it's a count to return to that point in the doc (as needed).


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.
Reply With Quote
  #2  
Old 09-19-2016, 12:25 AM
macropod's Avatar
macropod macropod is offline Getting bold Pargraph Windows 7 64bit Getting bold Pargraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #3  
Old 09-19-2016, 12:31 AM
gmayor's Avatar
gmayor gmayor is offline Getting bold Pargraph Windows 10 Getting bold Pargraph Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It is not exactly clear what
Quote:
I want to get each next paragraph after any paragraph that is bolded.
means but if you want to get the next non-bold paragraph after a bold paragraph then:
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
Reply With Quote
  #4  
Old 09-19-2016, 12:42 AM
macropod's Avatar
macropod macropod is offline Getting bold Pargraph Windows 7 64bit Getting bold Pargraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #5  
Old 09-19-2016, 01:45 AM
kirkm kirkm is offline Getting bold Pargraph Windows XP Getting bold Pargraph Office 2003
Advanced Beginner
Getting bold Pargraph
 
Join Date: Aug 2013
Posts: 40
kirkm is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 09-19-2016, 05:09 AM
macropod's Avatar
macropod macropod is offline Getting bold Pargraph Windows 7 64bit Getting bold Pargraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #7  
Old 09-19-2016, 03:23 PM
kirkm kirkm is offline Getting bold Pargraph Windows XP Getting bold Pargraph Office 2003
Advanced Beginner
Getting bold Pargraph
 
Join Date: Aug 2013
Posts: 40
kirkm is on a distinguished road
Default

That just ends immediately. Does something need to setup .Paragraphs.Last.Range.End ?
Reply With Quote
  #8  
Old 09-19-2016, 03:38 PM
macropod's Avatar
macropod macropod is offline Getting bold Pargraph Windows 7 64bit Getting bold Pargraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Did you select the range you want to process?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 09-19-2016, 03:41 PM
kirkm kirkm is offline Getting bold Pargraph Windows XP Getting bold Pargraph Office 2003
Advanced Beginner
Getting bold Pargraph
 
Join Date: Aug 2013
Posts: 40
kirkm is on a distinguished road
Default

ah, sorry - no. You mean like Control-A ?
Reply With Quote
  #10  
Old 09-19-2016, 03:55 PM
macropod's Avatar
macropod macropod is offline Getting bold Pargraph Windows 7 64bit Getting bold Pargraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #11  
Old 09-19-2016, 04:42 PM
kirkm kirkm is offline Getting bold Pargraph Windows XP Getting bold Pargraph Office 2003
Advanced Beginner
Getting bold Pargraph
 
Join Date: Aug 2013
Posts: 40
kirkm is on a distinguished road
Default

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 ?
Reply With Quote
  #12  
Old 09-19-2016, 05:07 PM
macropod's Avatar
macropod macropod is offline Getting bold Pargraph Windows 7 64bit Getting bold Pargraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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 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]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting bold Pargraph Adding bold colons at end of bold row headings bertietheblue Word Tables 2 07-26-2015 07:26 AM
Getting bold Pargraph 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
Getting bold Pargraph 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:20 AM.


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