Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-08-2015, 02:31 PM
mtrborges mtrborges is offline Help - Run-time error 91 - VBA for inserting and formatting text depending on style Mac OS X Help - Run-time error 91 - VBA for inserting and formatting text depending on style Office for Mac 2011
Novice
Help - Run-time error 91 - VBA for inserting and formatting text depending on style
 
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
  #2  
Old 02-08-2015, 07:53 PM
Guessed's Avatar
Guessed Guessed is offline Help - Run-time error 91 - VBA for inserting and formatting text depending on style Windows 7 32bit Help - Run-time error 91 - VBA for inserting and formatting text depending on style Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The problem arises when you get to the last paragraph and then assign notag as the range for the next paragraph (since there isn't one).

An alternative coding that deals with this is as follows. The coding is quite clumsy because it loops onto paragraphs the code just inserted but it should work for you.

Code:
Sub AutomaticNumbering()
  Dim manum As Field
  Dim rgpara As Paragraph
  Dim myRange As Range
  Dim notag As Range
  Dim i As Single
  
  For Each rgpara In ActiveDocument.Paragraphs
    Set myRange = rgpara.Range
    If myRange.Style = "Text" Then
      Set notag = myRange.Next(Unit:=wdParagraph, Count:=1)
      If notag Is Nothing Then
        myRange.InsertParagraphAfter
        Set notag = myRange.Paragraphs(2).Range
        notag.Style = "MNS"
        notag.Collapse Direction:=wdCollapseStart
        ActiveDocument.Fields.Add Range:=notag, Type:=wdFieldAutoNumLegal, _
              Text:="\Arabic *\e", PreserveFormatting:=True

      ElseIf notag.Style <> "MNS" Then
        myRange.InsertParagraphAfter
        Set notag = myRange.Paragraphs(2).Range
        notag.Style = "MNS"
        notag.Collapse Direction:=wdCollapseStart
        ActiveDocument.Fields.Add Range:=notag, Type:=wdFieldAutoNumLegal, _
              Text:="\Arabic *\e", PreserveFormatting:=True
      End If
    End If
  Next

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

End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 02-08-2015, 11:35 PM
macropod's Avatar
macropod macropod is offline Help - Run-time error 91 - VBA for inserting and formatting text depending on style Windows 7 64bit Help - Run-time error 91 - VBA for inserting and formatting text depending on style Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Cross-posted at: http://windowssecrets.com/forums/sho...nding-on-style
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
error 91, range object, word vba macro

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help - Run-time error 91 - VBA for inserting and formatting text depending on style How to globally change the formatting of a bullet style to another style? ravl13 Word 5 03-10-2013 05:04 PM
Macro to export email to text file on send depending on category Joe Patrick Outlook 0 10-19-2012 06:20 PM
Help - Run-time error 91 - VBA for inserting and formatting text depending on style Error:Power point can't readthe outline from time-world.ppt.No text converter is inst aligahk06 PowerPoint 1 10-21-2011 03:48 AM
Help - Run-time error 91 - VBA for inserting and formatting text depending on style Strange formatting text color error in form fields.. rscafidi Word 2 10-16-2011 03:25 PM
Inserting 'Text from File' without loosing formatting OverAchiever13 Word 2 05-28-2010 12:24 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft