+ Reply to Thread
Results 1 to 14 of 14

Thread: Is the debugger lying to me?

  1. #1
    Registered User
    Join Date
    01-26-2010
    Location
    Dunedin, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Is the debugger lying to me?

    I have a class, "OB3Slide", which has a sub "addText(sText as String)".

    Dim x as new OB3Slide
    dim sTemp as string
    sTemp = extractText(oShape)
    x.addText sTemp
    The debugger stops with the entire last line highlighted in yellow, and the error:

    Runtime error '438' Object doesn't support this property or method
    If I then press F8, it immediately steps into the 'addText' sub which works perfectly fine. But it interrupts the program to tell me what appears to be a non-error.

    Has anybody else experienced anything like this?
    Last edited by Moochoopork; 01-05-2012 at 02:48 AM.

  2. #2
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Is the debugger lying to me?

    It might be the code that is in the addText sub, can you post this? The debugger will not highlight errors in custom classes.
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  3. #3
    Registered User
    Join Date
    01-26-2010
    Location
    Dunedin, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Is the debugger lying to me?

    The addText sub just calls this space-saving array addition method:

    Private Sub add(theArray, theValue)
        On Error GoTo err
        If (UBound(theArray) = 0) Then
            theArray(0) = theValue
        Else
            ReDim Preserve theArray(UBound(theArray) + 1)
            theArray(UBound(theArray)) = theValue
        End If
        
        Exit Sub
        
    err:
        ReDim theArray(0)
        theArray(0) = New OB3Slide
        Set theArray(0) = theValue
    End Sub
    which is able to be stepped through without any problems...

  4. #4
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Is the debugger lying to me?

    This doesn't look right:

    err:
        ReDim theArray(0)
        theArray(0) = New OB3Slide
        Set theArray(0) = theValue
    End Sub
    shouldn't it be:
    err:
        ReDim theArray(0)
        Set theArray(0) = New OB3Slide
        Set theArray(0) = theValue
    End Sub
    Though I don't know what you are doing
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  5. #5
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,230

    Re: Is the debugger lying to me?

    I don't know anything about class modules but is it acceptable to have a subroutine which is the same as a VBA reserved word? The Add method is defined in the VBA Help as:

    Add Method (Visual Basic for Applications)

    Adds a member to a Collection object.

    Syntax

    object.Add item, key, before, after

    Regards, TMS

  6. #6
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Is the debugger lying to me?

    It's generally not a good idea, though it raises the question of why you aren't using a collection anyway MoochooPork. Seems like it may be a better object for you as you don't need to redim preserve
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  7. #7
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Is the debugger lying to me?

    I doubt it is related to the issue in hand, but it is not good practice to use Dim ... As New ... in my opinion. Declare in one line and initialise explicitly when you want to create the object.
    Just my 2c.

    PS As regards the class, you can call the routines whatever you like really (subject to naming rules): since you will need to call them as methods of the object in question, there is no confusion for the compiler. In many ways it is good practice to use the sort of syntax that people are familiar with - such as using Add if you have created your own collection class, as an example.
    Good luck.

  8. #8
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Is the debugger lying to me?

    Yep, you should initialise it separately as above you can then check where it is initialise, you can't if you create it at the beginning of the code (because it is created then)
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  9. #9
    Registered User
    Join Date
    01-26-2010
    Location
    Dunedin, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Is the debugger lying to me?

    Wow thanks guys! I'm most experienced with C-family languages and while I spent a lot of time in VB6 back in the day, I've done bugger-all VBA and I find it the most different language I've come across (it's been about 13 years since I touched VB).

    I'm essentially trying to loop through all the Slides in a PPT presentation, then loop through all the Shapes on the Slide, and create an instance of my class using the information derived from the Shapes.

    I would have expected this to work:

    Dim oOB3Slides() as OB3Slide
    Dim oThisSlide as OB3Slide
    
    For each oSlide in ActivePresentation.Slides
        set oThisSlide = new OB3Slide
        For Each oShape in oSlide.Shapes
            ... set oThisSlide's properties ...
        next oShape
        ... add oThisSlide to oOB3Slides ...
    next oSlide
    but VBA doesnt seem to create a NEW OB3Slide each time, it keeps the old one. It doesn't matter if I put the "Dim oSlide as OB3Slide" line before the loop, or "Dim oSlide as new OB3Slide" inside the first loop. It's damn annoying, and goes against everything I've learnt about programming... guess that's what you get for using a non-OO language. I wish MS would give us something better than VB6 to use behind Office.

    Kyle123: Thanks for pointing out the missing "set", but it didnt change a thing However, I'll now go and change my array to a collection - I wasn't aware such a thing existed.

    TMShucks: Scope will determine when VBA will use the Collection.Add() method versus my Module1.add() method.

    Thank you all very much
    Last edited by Moochoopork; 01-05-2012 at 05:59 PM.

  10. #10
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Is the debugger lying to me?

    I cannot see how that code would run or compile since you are trying to use an array as a Collection (arrays do not have methods such as Add).
    Good luck.

  11. #11
    Registered User
    Join Date
    01-26-2010
    Location
    Dunedin, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Is the debugger lying to me?

    OnErrorGoto0: I'm not calling MyArray.add(value), I'm calling add(MyArray, value). "add" is a method I've written myself.

  12. #12
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Is the debugger lying to me?

    In the code you just posted
        oOB3Slides.add(oThisSlide)
    but oOB3Slides is an array.
    Good luck.

  13. #13
    Registered User
    Join Date
    01-26-2010
    Location
    Dunedin, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Is the debugger lying to me?

    *doh* lol my bad.. line should have read:
    add oOB3Slides, oThisSlide
    Nice catch

  14. #14
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Is the debugger lying to me?

    From what it looks like you're doing, you want something like:

    Sub kyle()
    
    Dim oThisSlide As Slide
    Dim oShape As PowerPoint.Shape
    Dim oOB3Slides As Collection
    Dim oCustom As oOB3Slide
    Dim x As Integer
    
    Set oOB3Slides = New Collection
    
    For Each oThisSlide In ActivePresentation.Slides
        For Each oShape In oThisSlide.Shapes
            Set oCustom = New oOB3Slide
                With oShape
                    oCustom.Something = .TextFrame.TextRange.Text
                    'assign other stuff to the class
                End With
            oOB3Slides.Add oCustom
        Next oShape
    Next oThisSlide
    
    For x = 1 To oOB3Slides.Count
        Debug.Print oOB3Slides(x).Something
    Next x
    
    End Sub
    The collection will then hold that instance of the class, leaving you to create a new instance in each iteration
    Last edited by Kyle123; 01-06-2012 at 04:24 AM.
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0