• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3285
  • Last Modified:

Word 2010 Changing a style in VBA

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
mcurran1
Asked:
mcurran1
  • 3
  • 2
1 Solution
 
GrahamSkanRetiredCommented:
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
 
mcurran1Author Commented:
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
 
mcurran1Author Commented:
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
 
GrahamSkanRetiredCommented:
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
 
mcurran1Author Commented:
My solution does EXACTLY what I wanted it to do.  Other comments came close, but not EXACTLY what I was looking for.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now