this vb code correctly removes numbering but also removes bullet points - i want to keep only the first functionality
Sub a_usunNumeracjeOproczNaglowkow()
Dim doc As Document
Dim para As Paragraph
Dim i As Long
Dim totalParas As Long
Dim styleNameToCheck As String
Dim targetStyle As Variant
Dim targetStyleNames As Variant
code
Set doc = ActiveDocument
' --- Maximum performance settings ---
Dim originalScreenUpdating As Boolean
Dim originalDisplayAlerts As WdAlertLevel
originalScreenUpdating = Application.ScreenUpdating
originalDisplayAlerts = Application.DisplayAlerts
Application.ScreenUpdating = False
Application.DisplayAlerts = wdAlertsNone
'--------------------------------------
On Error GoTo ErrorHandler
totalParas = doc.Paragraphs.Count
' Define the style names from which numbering should be removed
' Add any other style names if necessary (e.g., "List Paragraph")
targetStyleNames = Array("Normal", "Normalny") ' Using localized name for Polish Word
' Process paragraphs in reverse order. This is generally safer if
' operations might delete or merge paragraphs, though RemoveNumbers usually doesn't.
For i = totalParas To 1 Step -1
Set para = doc.Paragraphs(i)
' Get the style name of the current paragraph
' Using .NameLocal to get the displayed style name (e.g., "Normalny")
On Error Resume Next ' In case .Style or .NameLocal is problematic for a specific paragraph
styleNameToCheck = para.Style.NameLocal
If Err.Number <> 0 Then
styleNameToCheck = "" ' Reset if there was an error getting the style name
Err.Clear
End If
On Error GoTo ErrorHandler ' Restore normal error handling for the rest of the loop
' Check if the paragraph's style is one of the target styles
For Each targetStyle In targetStyleNames
If LCase(styleNameToCheck) = LCase(CStr(targetStyle)) Then
' If the style matches, and it actually has list formatting, remove the numbering.
If para.Range.ListFormat.ListType <> wdListNoNumbering Then
para.Range.ListFormat.RemoveNumbers
End If
Exit For ' Style matched, no need to check other target styles for this paragraph
End If
Next targetStyle
Next i
If Err.Number = 0 Then
MsgBox "Usunięto numerację", vbInformation
End If
Exit Sub
ErrorHandler:
MsgBox "Błąd: " & Err.Description, vbExclamation
Resume CleanExit ' Go to cleanup code
End Sub
Assistant
The issue is that RemoveNumbers removes all list formatting, including both numbering and bullets. To keep bullet points while removing only numbering, you need to check the list type and only remove numbered lists. Here's the modified code:
vb
Sub a_usunNumeracjeOproczNaglowkow()
Dim doc As Document
Dim para As Paragraph
Dim i As Long
Dim totalParas As Long
Dim styleNameToCheck As String
Dim targetStyle As Variant
Dim targetStyleNames As Variant
Dim listType As WdListType
Set doc = ActiveDocument
' --- Maximum performance settings ---
Dim originalScreenUpdating As Boolean
Dim originalDisplayAlerts As WdAlertLevel
originalScreenUpdating = Application.ScreenUpdating
originalDisplayAlerts = Application.DisplayAlerts
Application.ScreenUpdating = False
Application.DisplayAlerts = wdAlertsNone
'--------------------------------------
On Error GoTo ErrorHandler
totalParas = doc.Paragraphs.Count
' Define the style names from which numbering should be removed
' Add any other style names if necessary (e.g., "List Paragraph")
targetStyleNames = Array("Normal", "Normalny") ' Using localized name for Polish Word
' Process paragraphs in reverse order. This is generally safer if
' operations might delete or merge paragraphs, though RemoveNumbers usually doesn't.
For i = totalParas To 1 Step -1
Set para = doc.Paragraphs(i)
' Get the style name of the current paragraph
' Using .NameLocal to get the displayed style name (e.g., "Normalny")
On Error Resume Next ' In case .Style or .NameLocal is problematic for a specific paragraph
styleNameToCheck = para.Style.NameLocal
If Err.Number <> 0 Then
styleNameToCheck = "" ' Reset if there was an error getting the style name
Err.Clear
End If
On Error GoTo ErrorHandler ' Restore normal error handling for the rest of the loop
' Check if the paragraph's style is one of the target styles
For Each targetStyle In targetStyleNames
If LCase(styleNameToCheck) = LCase(CStr(targetStyle)) Then
' Get the list type
listType = para.Range.ListFormat.ListType
' Check if it's a numbered list (not a bullet list or no list)
' wdListBullet = 2, so we want to remove anything that's not 0 (no list) or 2 (bullet)
If listType <> wdListNoNumbering And listType <> wdListBullet Then
para.Range.ListFormat.RemoveNumbers
End If
Exit For ' Style matched, no need to check other target styles for this paragraph
End If
Next targetStyle
Next i
CleanExit:
' Restore original settings
Application.ScreenUpdating = originalScreenUpdating
Application.DisplayAlerts = originalDisplayAlerts
If Err.Number = 0 Then
MsgBox "Usunięto numerację", vbInformation
End If
Exit Sub
ErrorHandler:
MsgBox "Błąd: " & Err.Description, vbExclamation
Resume CleanExit ' Go to cleanup code
End Sub
The key change is in this section:
vb
' Get the list type
listType = para.Range.ListFormat.ListType
' Check if it's a numbered list (not a bullet list or no list)
' wdListBullet = 2, so we want to remove anything that's not 0 (no list) or 2 (bullet)
If listType <> wdListNoNumbering And listType <> wdListBullet Then
para.Range.ListFormat.RemoveNumbers
End If
This modification checks the list type before removing it. The code will now only remove numbered lists (like wdListSimpleNumbering, wdListOutlineNumbering, etc.) while preserving bullet lists (wdListBullet).