Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2005

Tip: Looking for answers? Try searching our database.

Style formatting through VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anne P. - 04 Apr 2005 23:28 GMT
Hi,

I have been doing customizations for companies (mostly law firms) from Word 6 through 2000.  When setting up their styles, I use the following method:



First, I insert dummy text by typing =and(10,5) to insert 10 paragraphs, with 5 sentences each.  Then, in each paragraph, I apply the required formatting and save the style name.  



When I am done, I have a bunch of paragraphs with the text:  "The quick brown fox jumps over .", and it is not clear what style is applied without looking at the stylename name drop-down list.



So, I created the following macro which loops through all paragraphs in the document.  It turns on the Bold attribute, inserts the name of the style at the beginning of the paragraph and then turns off the Bold attribute.  

Sub StyleName()

Dim opara As Paragraph

For Each opara In ActiveDocument.Paragraphs

   If opara.Style <> "Normal" Then

       Selection.Font.Bold = wdToggle

   opara.Range.InsertBefore Text:=Format(opara.Style.NameLocal & ". ")

   Selection.Font.Bold = wdToggle

End If

Next opara

End Sub



This method has worked very good for Word 97 and Word 2000.  However, it does not work for Word XP or Word 2003.  I have split the window and watched how it works as I step through the macro (pressing F8).  I see it turn bold on, then it goes to the beginning of the paragraph (before the bold on command which effectively turns bold off), inserts the name of the style, then does the Bold toggle, but since it was just turned off before it inserted the text, it is now turning Bold on after the text has been inserted and the text is not bold.  



Any suggestions on how I can make this work in Word 2003?



Thanks,

Anne


Jay Freedman - 05 Apr 2005 02:32 GMT
Hi Anne,

Before answering directly, let me point out a feature you may not have
noticed. Put the document in Normal view or Outline view, then go to
Tools > Options > View and set the value of the "Style area width" box
to something larger than zero -- say, 1". The name of the style of
each paragraph will appear to the left, without changing the content
of the document at all.

If you still want to mangle your documents, notice that in your macro
the Selection (which is the only part that you make bold or nonbold)
has nothing at all to do with opara.Range (which is where the style
name is being inserted). I can't believe this code ever worked in any
version of Word -- it must have been altered in the move from one
version to another.

This version will work, with the added plus of not changing the user's
selection:

Sub StyleName()
   Dim opara As Paragraph
   Dim oRange As Range
   For Each opara In ActiveDocument.Paragraphs
       If opara.Style <> "Normal" Then
           Set oRange = opara.Range
           With oRange
               .Collapse wdCollapseStart
               .Text = opara.Style.NameLocal & ". "
               .Bold = True
           End With
       End If
   Next opara
End Sub

Finally, both of your posts that I've answered have been cross-posted
to all the Word VBA newsgroups. While cross-posting is preferable to
separate multiple postings, it isn't necessary at all. The regular
readers here look at all the newsgroups.

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org

>Hi,
>
[quoted text clipped - 33 lines]
>
>Anne
Anne P. - 05 Apr 2005 13:29 GMT
Thanks Jay.

Actually, I am very aware of the Style Area Width feature.  However, that
does not help in this situation.  I use this method to produce a printout of
all styles being created for the client so that they may see how the style
looks and what it's name is.  They can approve or not approve the style, or
make suggestions on changes to be made.

Anne
> Hi Anne,
>
[quoted text clipped - 93 lines]
>>
>>Anne
Charles Kenyon - 05 Apr 2005 03:02 GMT
Adding to Jay's comments... In working with Bold / underline, etc., usually you do not want to be toggling it (which is what you get with recorded macros). You want to be setting this to True or False. Word does not work with codes the way WP does.
Signature

Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.

 Hi,

 I have been doing customizations for companies (mostly law firms) from Word 6 through 2000.  When setting up their styles, I use the following method:

 

 First, I insert dummy text by typing =and(10,5) to insert 10 paragraphs, with 5 sentences each.  Then, in each paragraph, I apply the required formatting and save the style name.  

 

 When I am done, I have a bunch of paragraphs with the text:  "The quick brown fox jumps over .", and it is not clear what style is applied without looking at the stylename name drop-down list.

 

 So, I created the following macro which loops through all paragraphs in the document.  It turns on the Bold attribute, inserts the name of the style at the beginning of the paragraph and then turns off the Bold attribute.  

 Sub StyleName()

 Dim opara As Paragraph

 For Each opara In ActiveDocument.Paragraphs

     If opara.Style <> "Normal" Then

         Selection.Font.Bold = wdToggle

     opara.Range.InsertBefore Text:=Format(opara.Style.NameLocal & ". ")

     Selection.Font.Bold = wdToggle

 End If

 Next opara

 End Sub

 

 This method has worked very good for Word 97 and Word 2000.  However, it does not work for Word XP or Word 2003.  I have split the window and watched how it works as I step through the macro (pressing F8).  I see it turn bold on, then it goes to the beginning of the paragraph (before the bold on command which effectively turns bold off), inserts the name of the style, then does the Bold toggle, but since it was just turned off before it inserted the text, it is now turning Bold on after the text has been inserted and the text is not bold.  

 

 Any suggestions on how I can make this work in Word 2003?

 

 Thanks,

 Anne

 
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.