this vb code removes the numbering, but also for lists (which it probably shouldn't); i…
- User
this vb code removes the numbering, but also for lists (which it probably shouldn't); i can't identify the cause; for each formatting removal it does, display a message with the style of the text where the removal occurs
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 iCleanExit: ' Restore original settings Application.ScreenUpdating = originalScreenUpdating Application.DisplayAlerts = originalDisplayAlerts
If Err.Number = 0 Then MsgBox "Usunięto numerację", vbInformation End If Exit SubErrorHandler: MsgBox "Błąd: " & Err.Description, vbExclamation Resume CleanExit ' Go to cleanup code End Sub
- Assistant
Below is one way to modify your code so that whenever numbering is removed from a paragraph it shows a message box indicating the style of that paragraph. (Be aware that if many paragraphs are affected, you’ll get a lot of pop‐up messages!) Also, note that numbering may be removed from some list paragraphs even if you believe they “shouldn’t” because—even if you expect a list—Word sometimes applies one of your target styles (like “Normal” or “Normalny”) automatically to list items, so your code ends up processing them.
You can try this updated version:
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. ' If a paragraph in a numbered list also has one of these styles, ' it will be processed. targetStyleNames = Array("Normal", "Normalny") ' Using localized name for Polish Word ' Process paragraphs in reverse order. For i = totalParas To 1 Step -1 Set para = doc.Paragraphs(i) ' Get the style name of the current paragraph. On Error Resume Next styleNameToCheck = para.Style.NameLocal If Err.Number <> 0 Then styleNameToCheck = "" Err.Clear End If On Error GoTo ErrorHandler ' 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 of the paragraph. listType = para.Range.ListFormat.ListType ' Check if it's a numbered list. ' wdListNoNumbering = 0 and wdListBullet = 2 are left as is. ' Any other list type (e.g. wdListSimpleNumbering, wdListOutlineNumbering) ' will have numbering removed. If listType <> wdListNoNumbering And listType <> wdListBullet Then para.Range.ListFormat.RemoveNumbers ' Display a message showing which style was processed. MsgBox "Removed numbering from paragraph with style: " & styleNameToCheck, vbInformation End If Exit For ' Found a match with a target style; no further checking needed. 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 End Sub
Explanation
-
Message for Each Removal:
Inside theIfblock that confirms the paragraph’s list type is not “none” or bullet, the code now calls:MsgBox "Removed numbering from paragraph with style: " & styleNameToCheck, vbInformationThis gives you a dialog each time numbering is removed, along with the paragraph’s style.
-
Unintended Removal from Lists:
Even though you intend to keep numbering for “lists,” you may find that some list paragraphs are formatted with one of your target styles (e.g. “Normal” or “Normalny”). Word sometimes uses the Normal style for numbered paragraphs. The conditionIf listType <> wdListNoNumbering And listType <> wdListBullet Thencatches any paragraph that is part of a numbered list (excluding bullet lists), so numbering will be removed.
If your intention is to safely skip all paragraphs that belong to any kind of list, you might adjust the code to check for a list membership instead. For example:
If para.Range.ListFormat.ListString <> "" Then ' Skip paragraphs that are part of a list. Else ' Remove numbering if conditions are met. End IfOr check explicitly for the type of numbering you wish to remove.
-
Further Adjustments:
If you want to avoid the pop-up messages for every reformatting during normal operation, you might consider logging these messages to the Immediate window (usingDebug.Print) or compiling them into a string that you display only after processing completes.
With this code, you should see a message for each removal. If you continue to see removal on list items that you want to keep, consider adjusting your logic so that paragraphs that are in lists (for instance, by checking whether
para.Range.ListFormat.ListString <> "") are skipped even if their style matches one of your target names. -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.