View Single Post
 
Old 02-08-2015, 02:31 PM
mtrborges mtrborges is offline Mac OS X Office for Mac 2011
Novice
 
Join Date: Feb 2015
Posts: 1
mtrborges is on a distinguished road
Default Help - Run-time error 91 - VBA for inserting and formatting text depending on style

Hello!

I've been having some trouble with the following code:

Code:
Sub AutomaticNumbering()
Dim manum As Field
Dim rgpara As Paragraph
Dim myRange As Range
Dim notag As Range

For Each rgpara In ActiveDocument.Paragraphs

If rgpara.Range.Style = "Text" Then
Set myRange = rgpara.Range
Set notag = myRange.Next(Unit:=wdParagraph, Count:=1)


       If notag.Style = "MNS" Then
       
        Else

        rgpara.Range.InsertParagraphAfter
            myRange.Collapse Direction:=wdCollapseEnd
            myRange.Style = "MNS"
            Set manum = ActiveDocument.Fields.Add(Range:=myRange, Type:=wdFieldAutoNumLegal, Text:="\Arabic *\e", PreserveFormatting:=True)
        End If
        
End If

Set notag = Nothing
Set myRange = Nothing
Set manum = Nothing
Set rgpara = Nothing

Next rgpara



End Sub
The function works fine, but in the end I get "Run-time error 91 Object Variable or With block variable not set".

The Debug indicates the problem might be related to the following line:

Code:
 If notag.Style = "MNS" Then
Does anyone have an idea to help?
Reply With Quote