[VBA] Error 424: Object Required

Can anyone please let me know why my code is not working?  I keep receiving Error 424:  Object Required.  I'm pretty new to VBA and coding so it might be something very simple.  This is what I would like my code to do:

  • There are text elements in a level.
  • The text content are numbers (1, 2, 3, 4, etc.).
  • I want to be able to select one text element, parse its content as a number and attach this number to a defined general URL.
  • The result should be attached to the selected element as URL link.

This is my code:

Sub Init()
  Dim oText As TextElement
  Set oText = ElementEnumerator.Current.AsTextElement
  Dim content As String
  content = oText.Text
  CreateLink content
End Sub
Sub CreateLink(ByVal content As String)
  Dim strURL As String
  strURL = "http://www.google.com" & "/" & content
  Debug.Print "My URL=" & strURL
  CadInputQueue.SendKeyin "ELEMENT CREATE LINK URL " & strURL
End Sub

  • You should see the line which causes the error and tell it us...

    But this case seems pretty simple: Neither You've declared the variable ElementEnumerator nor did You assign any value to it - at least not in the code we can see here.

    Robert

  • Thank you for the quick tip Robert.

    Here is what I think you are saying is the solution (with this change I am now getting a different error though):

    Sub Init()
    Dim ee As ElementEnumerator
    Dim oText As TextElement
    Set oText = ee.Current.AsTextElement 'The error is now occurring here (Run-Time error '91': Object variable or With block variable not set

    Dim content As String
    content = oText.Text
    CreateLink content
    End Sub
    Sub CreateLink(ByVal content As String)
    Dim strURL As String
    strURL = "http://www.google.com" & "/" & content
    Debug.Print "My URL=" & strURL
    CadInputQueue.SendKeyin "ELEMENT CREATE LINK URL " & strURL
    End Sub
  • Unknown said:
    Dim ee As ElementEnumerator
    Dim oText As TextElement
    Set oText = ee.Current.AsTextElement

    You've declared a variable named ee.  It's a reference to a class of type ElementEnumerator.  Right now, you haven't initialised that object: it's an empty chunk of memory.  That explains the error message.

    An ElementEnumerator is used to enumerate elements.  The question is "What elements do you want to enumerate?"  Often, you want to scan a DGN model to examine its graphic elements.  The Scan method of ModelReference does exactly that and returns a valid ElementEnumerator.  Look that up in VBA help, and write something like this:

    Set ee = ActiveModelReference.Scan
    Do While ee.MoveNext
      Dim oElement As Element
      Set oElement = ee.Current
      If oElement.IsTextElement Then
        Dim oTextElement As TextElement
        Set oTextElement = oElement.AsTextElement
        '  Now you can use the properties and methods of TextElement
        Debug.Print "Text=" & oTextElement.Text
      EndIf
    Loop

     
    Regards, Jon Summers
    LA Solutions

  • Hi Jeff,

    Unknown said:
    I'm pretty new to VBA and coding

    The problem is not you are new to VBA, because we were (and for sure will be again) new to some topic ;-)

    But what is the problem is it seems you didn't pass any VBA learning, so you don't have basic knowledge how VBA works and how to create VBA apps, and you are trying to use MicroStation VBA, which adds another magnitude of complexity. In other worlds, you want to fly before you know how to walk.

    I asked you already in another thread and you have not answered it: Did you read Learning MicroStation VBA book? This is mandatory reading for anybody who wish to start with MicroStation VBA. It starts step by step from a ground, so it requires and expects only basic knowledge of VBA. It can make your coding effort much easier and will provide you answers for many your questions and problems.

    With regards,

      Jan

  • Jon,
    First, thanks for all the help. I've looked up every word of code you have helped me with so far in the MicroStationVBA.chm and I've really developed a good understanding of what is happening when I run this VBA. But there is one thing that I cannot get around, I cannot select a text element, run the macro, and have that text element attach to the general URL in the code. Instead, only the most recently created text element will attach itself to the general URL in the code. Example: I use the Place Text tool to create a text element that displays the following text, TEST, then I select a different text element and run the VBA and a get a link attached to that text element that reads www.generalURL.com/TEST. I've searched the VBA Help index high and low looking for a way to parse out only the text of the element that I currently have highlighted. I keep delving deeper and deeper into selection set tutorials, but I can't seem to find anything. Here is the code as it is:

    Sub Init()
    Dim ee As ElementEnumerator
    Set ee = ActiveModelReference.Scan
    Do While ee.MoveNext
    Dim oElement As Element
    Set oElement = ee.Current
    If oElement.IsTextElement Then
    Dim oTextElement As TextElement
    Set oTextElement = oElement.AsTextElement
    Debug.Print "Text=" & oTextElement.Text
    End If
    Loop
    Dim content As String
    content = oTextElement.Text
    CreateLink content
    End Sub
    Sub CreateLink(ByVal content As String)
    Dim strURL As String
    strURL = "http://www.google.com" & "/" & content
    Debug.Print "My URL=" & strURL
    CadInputQueue.SendKeyin "ELEMENT CREATE LINK URL " & strURL
    End Sub


    Do you see any reason why this is happening?
  • Nevermind, I got it! Instead of .Scan I am now using .GetSelectedElements .

    Thanks for all your help!