Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word 2010 Macro to Remove Line Numbering if Numbered

Posted on 2013-06-30
4
Medium Priority
?
692 Views
Last Modified: 2013-07-02
I have a word document where some lines are numbered and some are not.  I want a macro that will move through the document paragraph by paragraph and, ONLY IF THAT LINE IS NUMBERED, set the numbering to "none" and adjust the left margin as follows:

        Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
        With Selection.ParagraphFormat
            .LeftIndent = InchesToPoints(0.19)
            .SpaceBeforeAuto = False
            .SpaceAfterAuto = False
        End With

I've attached an example word file.
WordExport.doc
0
Comment
Question by:rmbrown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 39289163
Try this:
Public Sub RemoveNumbersFromNumberedParagraphs()
    For Each oPar In ActiveDocument.Paragraphs
        If oPar.Range.ListFormat.ListType = wdListOutlineNumbering Then
            oPar.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
            With oPar.Range.ParagraphFormat
                .LeftIndent = InchesToPoints(0.19)
                .SpaceBeforeAuto = False
                .SpaceAfterAuto = False
            End With
        End If
    Next
End Sub

Open in new window

WordExport.doc
0
 

Author Comment

by:rmbrown
ID: 39290790
Definitely answered my question so I'll accept immediately... but I have a follow on if you have time.

Regarding the indention, what I really want to do is to indent the text just enough to leave the first letter of the text in it's original location (so it looks like the number was deleted but the text didn't shift left).  What the code I sent you actually does is to move it all to the left.  When the word document is in outline form, this doesn't give me exactly what I need.

My first thought was to change the line
.LeftIndent = InchesToPoints(0.19)
to
.LeftIndent = .LeftIndent + InchesToPoints(0.19) but this results in an error since .LeftIndent surprised me by being Boolean.  Thoughts?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39293115
Maybe the problem was that the debugger can't support .LeftIndent, you would have to include the whole expression as specified in the With statement.

But also by the time you get there the listformat has been removed and the indent changed already.

Try this:
Public Sub RemoveNumbersFromNumberedParagraphs()
    For Each oPar In ActiveDocument.Paragraphs
        If oPar.Range.ListFormat.ListType = wdListOutlineNumbering Then
            Dim intLeftIndent As Integer
            intLeftIndent = oPar.Range.ParagraphFormat.LeftIndent
            oPar.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
            With oPar.Range.ParagraphFormat
                .LeftIndent = intLeftIndent ' + InchesToPoints(0.19)
                .SpaceBeforeAuto = False
                .SpaceAfterAuto = False
            End With
        End If
    Next
End Sub

Open in new window

0
 

Author Comment

by:rmbrown
ID: 39293675
Thanks!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Suggested Courses

722 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