Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Shortening a repetitive silly macro

    Hi,

    I needed to remove chapter numbers (Heading 1, Heading 2 ... Heading 7). Therefore, I recorded a macro for manually removing the chapter number of Heading 1.

    This was the result:

    Code:
    Sub RemChapNumbers()
    '
    '
        ActiveDocument.Styles("Heading 1").LinkToListTemplate ListTemplate:= _
            Nothing
        With ActiveDocument.Styles("Heading 1")
            .AutomaticallyUpdate = False
            .BaseStyle = "Normal"
            .NextParagraphStyle = "Normal"
        End With
    End Sub
    So if I would like the same procedure applied to the rest of the headings, I would have to use:

    Code:
    Sub MacroNew()
    '
    ' MacroNew Macro
    '
    '
        ActiveDocument.Styles("Heading 1").LinkToListTemplate ListTemplate:= _
            Nothing
        With ActiveDocument.Styles("Heading 1")
            .AutomaticallyUpdate = False
            .BaseStyle = "Normal"
            .NextParagraphStyle = "Normal"
        End With
        ActiveDocument.Styles("Heading 2").LinkToListTemplate ListTemplate:= _
            Nothing
        With ActiveDocument.Styles("Heading 2")
            .AutomaticallyUpdate = False
            .BaseStyle = "Normal"
            .NextParagraphStyle = "Normal"
        End With
        ActiveDocument.Styles("Heading 3").LinkToListTemplate ListTemplate:= _
            Nothing
        With ActiveDocument.Styles("Heading 3")
            .AutomaticallyUpdate = False
            .BaseStyle = "Normal"
            .NextParagraphStyle = "Normal"
        End With
    'And so on
    End Sub
    Is there a way to shorten this macro?

    Thanks a lot!

    Daniel

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Daniel,

    This should do the trick.

    Code:
    Sub RemChapNumbers()
    
        Dim iStyleNo as Integer
        Dim zStyle    as String
    
        For iSytleNo = 1 to 9   
    
           zStyle = "Heading " & Format(iStyleNo,"#")  
           ActiveDocument.Styles(zStyle).LinkToListTemplate ListTemplate:= Nothing     
    
          With ActiveDocument.Styles(zStyle)        
                .AutomaticallyUpdate = False         
                .BaseStyle = "Normal"        
                .NextParagraphStyle = "Normal"    
          End With 
    
       Next iStyleNo
    
    End Sub
    Last edited by RetiredGeek; 2012-11-28 at 10:44.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. The Following User Says Thank You to RetiredGeek For This Useful Post:

    bcn (2012-11-28)

  4. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    Or just
    Code:
    Sub RemChapNumbers()   
    Dim iStyleNo               As Integer
       For iStyleNo = 1 To 9
          With ActiveDocument.Styles("Heading " & iStyleNo)
             .LinkToListTemplate ListTemplate:=Nothing
             .AutomaticallyUpdate = False
             .BaseStyle = "Normal"
             .NextParagraphStyle = "Normal"
          End With
       Next iStyleNo
    End Sub
    Regards,
    Rory

    Microsoft MVP - Excel

  5. The Following User Says Thank You to rory For This Useful Post:

    bcn (2012-11-28)

  6. #4
    New Lounger
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you very much to both of you, RG and Rory. Very helpful!!!

    Daniel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •