Solved

Word 2010 Changing a style in VBA

Posted on 2013-12-06
5
2,078 Views
Last Modified: 2013-12-15
I have some code I am trying to develop for changing styles on selected text in a word document.  I can get the text to change to the selected style visually, but when you select the "Heading 1" text, it still shows that is has a normal style in the ribbon.  It doesn't change to "Heading 1" as it would if I manually change it via the ribbon.

So you understand the document:
A person has to place the cursor after what will be set as "Heading 1".  The code then selects from the cursor to the end of the paragraph and changes the font.  It then selects everything to the left of the cursor and it is supposed to set the style to "Heading 1" and then indent the paragraph.

Here is my code so far...

Sub SelectCurrentParagraph()

    With Selection
        .Range.Start = Curpos
        .MoveEnd Unit:=wdParagraph
        .Font.Name = "Times New Roman"
        .Font.Size = "12"
        .MoveLeft
   
        .Range.Start = Curpos
        .HomeKey Unit:=wdLine, Extend:=wdExtend
        .Style = "Heading 1"  ' <-This is the problem area
        .HomeKey
        .TypeText Text:=vbTab
    End With
0
Comment
Question by:mcurran1
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
Not quite sure what you are trying to do. Does this help?
    Dim para As Paragraph
    Set para = Selection.Paragraphs.First
    With para.Range
        .Font.Name = "Times New Roman"
        .Font.Size = "12"
        .Style = "Heading 1"  ' <-This is the problem area
     End With

Open in new window

0
 

Author Comment

by:mcurran1
Comment Utility
I will give you a sample paragraph of what it might look like to start with...  They will place the cursor after the "." after the word Meeting and before the "W" in Written on the first line. The macro will then select everything to the right and the to the end of the paragraph and change the font.  It then moves the cursor to the left to unselect that text and gives a definite cursor position and the macro selects everything to the left of the cursor to the beginning of the line and needs to change the style to "Heading 1" so that it then becomes an entry in the table of contents.  Does that make sense?  However, when my macro runs it doesn't do that.  Yours selects the whole paragraph and changes style correctly, but I just want the selected words "Section 5.      Notice of Meeting." to be in the style "Heading 1".

Sample Paragraph:
Section 5.      Notice of Meeting.  Written notice stating the place, day and our of the meeting and, in case of a special meeting, the purpose or purposes for which the meeting is called, shall be delivered not less than ten (10) nor more than fifty (50) days before the date of the meeting, either personally or by mail, by or at the direction of the President, the Secretary, or the office or persons calling the meeting, to each Shareholder of record entitled to vote at such meeting.  If mailed, such notice shall be deemed to be delivered when deposited in the United States mail, postage prepaid, addressed to the Shareholder at his or her address as it appears on the stock transfer book of the corporation.  Each Shareholder shall furnish the Secretary with an address at which notices may be delivered to him or her, and in default thereof, notice may be addressed to him or her at the office of the corporation.  However, the right to notice may be waived as provided in these Bylaws.
0
 

Accepted Solution

by:
mcurran1 earned 0 total points
Comment Utility
Nevermind, I figured it out.  I was the Style Separator piece that I wasn't understanding or even knew about.

Sub SelectCurrentParagraph()

    With Selection
               
        .InsertParagraph
               
        .MoveLeft
        .Range.Start = Curpos
        .HomeKey Unit:=wdLine, Extend:=wdExtend
        .Style = ActiveDocument.Styles("Heading 1")
        .ParagraphFormat.Alignment = wdAlignParagraphJustify
        .MoveLeft
        .TypeText Text:=vbTab
        .EndKey
        .InsertStyleSeparator
       
        .Range.Start = Curpos
        .MoveEnd Unit:=wdParagraph
        .Font.Name = "Times New Roman"
        .Font.Size = "12"
        .MoveRight
           
    End With
   
End Sub
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
I'd have done it like this:
Sub SplitPara()
    Dim para As Paragraph
    Dim RngParaOrig As Range
    Dim sen As Range
    
    Set para = Selection.Paragraphs.First
    Set RngParaOrig = para.Range
    Set sen = RngParaOrig.Sentences(2)
    sen.Collapse wdCollapseEnd
    sen.Text = vbCr
    RngParaOrig.Paragraphs.First.Style = "Heading 1"
    RngParaOrig.Paragraphs.Last.Style = "Normal"
End Sub

Open in new window

0
 

Author Closing Comment

by:mcurran1
Comment Utility
My solution does EXACTLY what I wanted it to do.  Other comments came close, but not EXACTLY what I was looking for.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now