PDA

View Full Version : [SOLVED:] IF loop: object variable or with block variable not set



dagerr
11-30-2018, 03:43 AM
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

gmayor
11-30-2018, 05:15 AM
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 Withas that find is not executed and therefore the code acts on the whole of the document range,

dagerr
12-03-2018, 01:58 AM
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

gmayor
12-03-2018, 11:15 PM
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.

dagerr
12-04-2018, 12:24 PM
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

gmayor
12-04-2018, 09:56 PM
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

dagerr
12-11-2018, 12:55 PM
Of course it is work now. Unfortunately, I did not know that exist syntax: .last