Results 1 to 6 of 6

Thread: [RESOLVED] object variable not set

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Resolved [RESOLVED] object variable not set

    I'm creating an array P & want to dump it's elements into an Excel worksheet :

    VB Code:
    1. Set oXLApp = New Excel.Application
    2. Set oXLBook = oXLApp.Workbooks.Add
    3. Set oXLSheet = oXLBook.Worksheets(1)
    4.  
    5. While param < Val(Text3.Text)
    6. param = Val(Text2.Text) + Val(Text4.Text) * m
    7. ...
    8. oXLSheet.Cells(m + 1, 1).Value = param
    9. ...
    10. m = m+ 1
    11. Wend
    12. ...
    13. End Sub

    I get a 'object variable or with block variable not set' error on statement :
    oXLSheet.Cells(m + 1, 1).Value = param
    Why?
    I thought I created the oXLSheet object in the statement before the While loop : Set oXLSheet = oXLBook.Worksheets(1)
    Does this not carry over into a loop ?!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: object variable not set

    The "Set" line is right, and it would be fine in the loop.

    I cannot see any issues with what you posted, can you show us more code?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: object variable not set

    Yes - here it is. Vals is a function I wrote. I can share that too if needed; I checked it out earlier & it seems to output the PP array correctly :

    VB Code:
    1. Public m, param
    2. Dim oXLApp As Excel.Application
    3. Dim oXLBook As Excel.Workbook
    4. Dim oXLSheet As Excel.Worksheet
    5. Dim oXLChart_Rec, oXLChart_ID, oXLChart_all As Excel.Chart
    6.  
    7.  
    8.  
    9. Private Sub button_Click()
    10. Dim kout, return1 As Integer
    11. Dim PP() As Single
    12.  
    13. Set oXLApp = New Excel.Application
    14. Set oXLBook = oXLApp.Workbooks.Add
    15. Set oXLSheet = oXLBook.Worksheets(1)
    16.  
    17. param = 0
    18. m = 0
    19. While param < Val(Text3.Text)
    20.    param = Val(Text2.Text) + Val(Text4.Text) * m
    21.    RichTextBox1.Find (List1.Text)
    22.    RichTextBox1.UpTo ("=")
    23.    RichTextBox1.UpTo ("1234567890.")
    24.    RichTextBox1.Span ("1234567890.")
    25.    RichTextBox1.SelRTF = param
    26.    RichTextBox1.SaveFile "C:\test.txt", rtfText
    27.    k = 0
    28.    Call DLL_app(kout)
    29.    If Str(k) = 0 Then
    30.    Text1.Text = "app ran OK"
    31.    Else
    32.    Text1.Text = "app problem!"
    33.    End If
    34.    RichTextBox2.LoadFile ("C:\starter.txt")
    35.    Name "C:\starter.txt" As "C:\" & Text5.Text & "_" & Str(param) & ".txt"
    36.    PP = Vals("C:\" & Text5.Text & "_" & Str(param) & ".out", Val(Text6.Text))
    37.  
    38.    oXLSheet.Cells(m + 1, 1).Value = param     '<---error flags this statement
    39.    oXLSheet.Cells(m + 1, 2).Value = PP(1)
    40.    oXLSheet.Cells(m + 1, 3).Value = PP(2)
    41.    oXLSheet.Cells(m + 1, 4).Value = PP(3)
    42.  
    43.    m = m + 1
    44.  
    45. Wend
    46.  
    47. oXLApp.Visible = True
    48.  
    49. Set oXLSheet = Nothing
    50. oXLBook.SaveAs "C:\" & List1.Text & ".xls"
    51. oXLBook.Close SaveChanges:=False
    52. Set oXLBook = Nothing
    53. oXLApp.Quit
    54. Set oXLApp = Nothing
    55.  
    56. return1 = MsgBox("processing finished", vbOKOnly, "alert")
    57. End Sub

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: object variable not set

    Your code is not ideal (see comments below), but for what you have shown all of the Excel code is fine.

    Do either Vals or DLL_app work with your Excel objects at all? (either way, it would probably help if we could see the code)


    On to the comments:

    The first thing I saw was that your variables are not declared properly. It's an easy mistake, but one that can cause problems. Lets take this line as an example:
    VB Code:
    1. Dim kout, return1 As Integer
    return1 is an Integer, but kout is not - it is a Variant instead. This makes your code slower, use more memory, and more prone to errors. Here's how it should be:
    VB Code:
    1. Dim kout as Integer, return1 As Integer

    Your variables m and param do not have a data type at all - but they should (I would assume Integer), and your variable k has not even been defined (all variables should be defined, and have a data type).


    When you call functions (such a MsgBox) and dont care about the return value, just ignore it - there is no need to create/use a variable. Instead of this:
    VB Code:
    1. return1 = MsgBox("processing finished", vbOKOnly, "alert")
    use this:
    VB Code:
    1. MsgBox "processing finished", vbOKOnly, "alert"

    This line: If Str(k) = 0 Then
    ..has two problems. First of all, k is a number, so converting it to a string (with a space in front of the value) to compare it with a number (0) is pointless. The second issue is that k will always be 0, as that is what you have just set it to.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    27

    Re: object variable not set

    The DLL does works fine. Thank you for the comments; I've cleaned up the code a bit.

    I think I have a guess as to what is wrong. It 's the
    VB Code:
    1. PP = Vals("C:\" & Text5.Text & "_" & Str(param) & ".out", Val(Text6.Text))

    that's causing the problem.


    Here is the Vals function :
    VB Code:
    1. Function Vals(File As String, Prob As Integer) As Variant
    2.  
    3.   Set oXLApp = New Excel.Application
    4.   oXLApp.Visible = False
    5.  
    6.   oXLApp.Workbooks.OpenText FileName:=File, _
    7.         Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    8.         xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False,      Semicolon:=False, _
    9.         Comma:=False, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
    10.         Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1)), _
    11.         TrailingMinusNumbers:=True
    12.  
    13.    Set oXLBook = oXLApp.Workbooks(1)
    14.    Set oXLSheet = oXLBook.Worksheets(1)
    15.    .....
    16.    .....
    17.    Set oXLSheet = Nothing            
    18.    oXLBook.Close SaveChanges:=True    
    19.    Set oXLBook = Nothing
    20.    oXLApp.Quit                    
    21.    Set oXLApp = Nothing
    22.  
    23. End Function


    After Vals returns, the Excel app has already quit & so it doesn't recognize oXLSheet anymore. What I've done that works is to :
    not quit oXLApp inside the Val function. I've also renamed oXLSheet & OXLBook to oXLSheetA & oXLBookA inside the button click subroutine.
    This keeps excel open so that the oXLSheetA object is not lost.
    I guess that's as good as solution as any ?

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: object variable not set

    Ah... that would definitely cause it!

    Your solution is certianly valid, however I would recommend something a little different. Rather than have different variable names, you can use the same ones - but with a tiny (and yet huge) change - instead of having the variables declared at the top of the form, have them declared in each sub where they are used (in both button_Click and Vals).

    As it was previously, you were using the same variables - and overwriting them. Even with normal variables this is a bad thing, but with Object variables (Excel.Anything, ADODB.Anything, etc) this can be a nightmare, as you are losing your connection to the object (Excel), but it is still open using memory/CPU/... (check your Task Manager, or just wait until you shut your computer down - Excel will probably ask if you want to save!)

    If you declare the variables just where they are used, you dont get any issues with them being overwritten by other code - which is easy to do accidentally as you add more parts to the application. Where variables are declared determines their Scope (where they can be used), and you should always try to keep the Scope as small as possible.

Posting Permissions

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



Click Here to Expand Forum to Full Width