Consulting

Results 1 to 7 of 7

Thread: IF loop: object variable or with block variable not set

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location

    IF loop: object variable or with block variable not set

    Hi, the following code work properly but after end it shows error in IF loop: object variable or with block variable not set. I don't know what is not set. Help me please.

    Sub add_sign_xml()
    
    
    Dim bPar As Range
    Dim oPar As Paragraph
      Set bPar = ActiveDocument.Range
     
      With bPar.Find
        With .Font
        .Size = 8
        .Underline = wdUnderlineSingle
        End With
        
        For Each oPar In ActiveDocument.Range.Paragraphs
        If oPar.Range.Characters.First = "<" _  
        And Not InStrRev(oPar.Range.Text, "-") > 0 _
        And Not InStr(oPar.Next.Range.Text, "<tr>") > 0 Then
                oPar.Range.InsertAfter "add"
        End If
      Next
      
      End With
    lblb_Exit:
      Set bRng = Nothing
    End Sub
    Karol

  2. #2
    There is no bRng declared (or used) in your code and the particular error relates to
    oPar.Next.Range.Text
    for when processing the last paragraph in the document, there is no next paragraph to process. Glancing at the code I am not convinced that it does work 'properly' especially with regard to
      With bPar.Find
        With .Font
        .Size = 8
        .Underline = wdUnderlineSingle
        End With
    as that find is not executed and therefore the code acts on the whole of the document range,
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Even if I delete unnecessary code and add number of paragraph, it still working the same: It is doing what I want to do with document but still with error: object variable or with block variable not set

    Sub add_sign_xml()
    
    
    Dim oPar As Paragraph
    
    
        For Each oPar In ActiveDocument.Range.Paragraphs
        If oPar.Range.Characters.First = "<" _  
        And Not InStrRev(oPar.Range.Text, "-") > 0 _
        And Not InStr(oPar.Next(1).Range.Text, "<tr>") > 0 Then 'I've added no. of paragraph
                oPar.Range.InsertAfter "add"
        End If
      
    Next
    
    End Sub
    Karol

  4. #4
    As I said earlier, you will get an error if you try to process a next paragraph that doesn't exist. If oPar is the last paragraph then there is no oPar.Next(1) hence the error. You need to error trap that.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Still the same after adding line: And Not Len(oPar.Next.Range) = 1

    Sub add_sign_xml()
    
    
    Dim oPar As Paragraph
    
    
        For Each oPar In ActiveDocument.Range.Paragraphs
        If oPar.Range.Characters.First = "<" _
        And Not InStrRev(oPar.Range.Text, "-") > 0 _
        And Not InStr(oPar.Next.Range.Text, "<tr>") > 0 _
        And Not Len(oPar.Next.Range) = 1 Then 
                   
                    oPar.Range.InsertAfter "add"
         
        End If
      
    Next
    
    
    End Sub
    Karol

  6. #6
    You are still interrogating a paragraph that doesn't exist, this time to establish what it's length is. There is no next paragraph after the last paragraph.
    Sub add_sign_xml()
    Dim oPar As Paragraph
        For Each oPar In ActiveDocument.Range.Paragraphs
            If Not oPar.Range = ActiveDocument.Range.Paragraphs.Last.Range Then
                If oPar.Range.Characters.First = "<" _
                   And Not InStrRev(oPar.Range.Text, "-") > 0 _
                   And Not InStr(oPar.Next(1).Range.Text, "<tr>") > 0 Then
                    oPar.Range.InsertAfter "add"
                End If
            End If
        Next
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Of course it is work now. Unfortunately, I did not know that exist syntax: .last
    Karol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •