Call/Argument Error with Asc() - Delete "unnecessary" lines in MSWord 2007

StackOverflow https://stackoverflow.com/questions/23411007

  •  13-07-2023
  •  | 
  •  

Pregunta

Final Update: It has been resolved in an answer below. Thanks!


Probelm has been NOT been resolved :-(. The script does not interact well with MSword Fields.


Goal: Delete lines in MSWord 2007 that contain any number of spaces, tabs, and the obvious pilcrow (paragraph mark).

Steps Taken: I googled it and found this forum.

I then found this in a samble of a book on google and tried to modify it.

The modified is below:

Dim oPara As Word.Paragraph
Dim var
Dim SpaceTabCounter As Long
Dim oChar As Word.Characters

For Each oPara In ActiveDocument.Paragraphs
    If Len(oPara.Range) = 1 Then
        oPara.Range.Delete
    Else
        SpaceTabCounter = 0
        Set oChar = oPara.Range.Characters
        For var = 1 To oChar.Count
            Select Case Asc(oChar(var)) '     '   '   '  'ERROR is here
                Case 32, 9
                SpaceTabCounter = SpaceTabCounter + 1
            End Select
        Next
        If SpaceTabCounter + 1 = Len(oPara.Range) Then
             ' paragraph contains ONLY spaces
            oPara.Range.Delete
        End If
    End If
Next

The issue is that I get an error at "Select Case Asc(oChar(var))" half way down the code. "Run-time error '5': Invalid procedure call or argument"

I'm new to VBA and...I can't figure this out. Please send your love!

Thanks


The error is still occuring. Code as it stands now:

Dim oPara As Word.Paragraph
Dim var
Dim SpaceTabCounter As Long
Dim oChar As Word.Characters

For Each oPara In ActiveDocument.Paragraphs
    If Len(oPara.Range) = 1 Then
        oPara.Range.Delete
    Else
        SpaceTabCounter = 0
        Set oChar = oPara.Range.Characters
        For var = 1 To oChar.Count
            Select Case Asc(oChar(var).Text) 'modified this line: added ".Text"
                Case 32, 9
                SpaceTabCounter = SpaceTabCounter + 1
            End Select
        Next
        If SpaceTabCounter + 1 = Len(oPara.Range) Then
             ' paragraph contains ONLY spaces
            oPara.Range.Delete
        End If
    End If
Next
¿Fue útil?

Solución

When your code comes across a content control field, it reads the first character in the paragraph as an empty string. This behavior can be observed by checking the oChar.First.Text field in the local variables window. Asc() will throw an error when passed an empty string. This can be easily reproduced by running this procedure.

Sub throwError5()
    Debug.Print Asc("")
End Sub

You will need to test the value of oChar(var) to ensure it is not an empty string prior to returning its ASCII value.

Option Explicit

Sub deleteEmptyParagraphs()
    Dim oPara As Word.Paragraph
    Dim var
    Dim SpaceTabCounter As Long
    Dim oChar As Word.Characters

    For Each oPara In ActiveDocument.Paragraphs
        If Len(oPara.Range) = 1 Then
            oPara.Range.Delete
        Else
            SpaceTabCounter = 0
            Set oChar = oPara.Range.Characters
            For var = 1 To oChar.Count
                If oChar(var) <> "" Then ' stops Asc from throwing runtime error 5
                    Select Case Asc(oChar(var)) ' no more errrors!
                        Case 32, 9
                            SpaceTabCounter = SpaceTabCounter + 1
                    End Select
                End If
            Next
            If SpaceTabCounter + 1 = Len(oPara.Range) Then
             ' paragraph contains ONLY spaces
                oPara.Range.Delete
            End If
        End If
    Next
End Sub

I don't work with the Word object model often, so I have no idea why the fields' first character is an empty string. Please note that my comment about having to call oChar(index).Text was wrong. Text is the default property of a characters item.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top