?
Solved

Apply user defined paragraph style to certain paragraphs

Posted on 2012-09-16
6
Medium Priority
?
954 Views
Last Modified: 2012-09-20
Dear experts:

Below macro applies a user-defined paragraph style to all paragraphs in a
'document that contain the string "Table followed by a white space followed by a field".
It works just fine. But ...

Sub ApplyStyleToParagraphs()
    Dim prg As Paragraph

    For Each prg In ActiveDocument.Paragraphs
        If prg.Range.Find.Execute(findText:="Table^w^d", _
            Wrap:=wdFindStop, Forward:=True, MatchWildcards:=False) = True Then
            prg.Style = ActiveDocument.Styles("User-Defined-Style")
        End If
    Next
End Sub

Open in new window


But I would like to get only those paragraphs worked on where this string "Table^w^d" is at the very beginning of the paragraph. Paragraphs where this string is in between (not at the very beginning) are to be left untouched.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
  • 3
  • 3
6 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 38404631
As long as it does not appear as the first line in the document then try:

findText:="^pTable^w^d"

Chris
0
 

Author Comment

by:AndreasHermle
ID: 38404702
Hi Chris,

thank you very much for your swift help.

although it appears logic/reasonable to me, nothing happens with this revised code.

Regards, Andreas
0
 

Author Comment

by:AndreasHermle
ID: 38404838
Hi Chris,

The search term ("^pTable^w^d"), when entered into the search dialog field (on the condition that the field codes are shown) finds the desired locations, but does not work when integrated into the above macro. I guess, because two paragraphs get selected ?!

Regards, Andreas
0
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 38405466
Okay

Have connected to a PC and see that ... don't understand but I do see it.

Best so far from me is a kludge:

Sub ApplyStyleToParagraphs()
    Dim prg As Paragraph

    For Each prg In ActiveDocument.Paragraphs
        If prg.Range.Find.Execute(findText:="Table^w", _
            Wrap:=wdFindStop, Forward:=True, MatchWildcards:=False) = True And Trim(prg.Range.Words(1)) = "Table" Then
            prg.Style = ActiveDocument.Styles("User-Defined-Style")
        End If
    Next
End Sub

Open in new window


Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 38405475
I do see it ... silly me, you are iterating on paragraphs so of course your code wont see the para marker!

Chris
0
 

Author Closing Comment

by:AndreasHermle
ID: 38419755
Hi Chris,

excellent. Works just great. Exactly as requested. Thank you very much for your superb help. Regards, Andreas
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question