Error 424: object required when calling external library - vba

I'm writing a VBA macro, and have imported mscorlib.dll in order to refer to System.Math.
Sub draw16mmButtonHole()
Dim test As Double
test = System.Math.ASin(0) 'this is where the error happens
End Sub
Apparently Error 424 usually happens when calls return objects, and can be fixed by appending set to the variable assignment; however, here the same error happens.

In addition to the behavior described above, Error 424 happens when trying to call .NET functions from VBA. This is not something VBA is able to do.

Related

vba code suddenly encounters problems with enumeration

This visual basic code related to outlook used to work without problems
Sub cmdExample()
Dim myOlApp As Object
Set myOlApp = CreateObject("Outlook.Application")
Set myoSession = myOlApp.Session
Set myoCalendar = myoSession.GetDefaultFolder(olFolderCalendar)
End Sub
Now I obtain the runtime error 5 (Invalid procedure call or argument)
Reason found from debugging: at runtime olFolderCalendar is empty (by the way, same problem for other enumerations like olAppointmentItem, olBusy). My workaround in the above code is calling .GetDefaultFolder(9).
However I would rather use the enumerations and I would like to understand why all of them are empty.
If you want to pass literal constants instead of numbers you need to add a COM reference to Outlook. Read more about that in the How to Add an Object Library Reference in VBA article.
Also, you may try to use the Logon method before getting the folder. See NameSpace.Logon for more information.

VBA Test for Object Model

I'm trying to write a PPT macro that will work in both PPT 2010 and 2013-2016. There is one object in the 2013-16 Object Model (ActivateChartDataWindow) that is not in the 2010 model that I want to use. I thought of using code like this to test the application version and then use the correct object:
With theChart.ChartData
If CInt(Application.Version) >= 15 Then
.ActivateChartDataWindow
Else
.Activate
End If
....
End With
The problem is this won't compile in 2010 since the .ActivateChartDataWindow object is not found. So there wouldn't be run-time error, but there is a compile-time error.
What's the best way to do this? Is there a way to disable compile-time checking in the code itself?
You're making an early-bound member call; if the code contains member calls that can't be compiled using an earlier version of a type library, then the solution is to switch to late-bound calls, which by definition are only bound at run-time (i.e. no compile-time validation).
So instead of With theChart.ChartData, you declare an Object variable, and Set it to theChart.ChartData:
Dim lateBoundChartData As Object
Set lateBoundChartData = theChart.ChartData
And now any member calls made against that lateBoundChartData will only be validated at run-time - watch out for typos, Option Explicit can't help you here!
With lateBoundChartData
If CInt(Application.Version) >= 15 Then
'no intellisense & compile-time validation here
.ActivateChartDataWindow
Else
'no intellisense & compile-time validation here
.Activate
End If
End With
The funny thing is that people write late-bound code all the time, without even realizing: anything you write against Object is always going to be late-bound.

VBA Error 424 Object Required on call to sub in Workbook_Open

I am very new to vba. I am getting a 424 Ojbect required error when I open the document. I tried using Call subName but that made no difference.
Please let me know what else I can send or explain.
Code found in ThisWorkbook:
Private Sub Workbook_Open()
Sheet1.Worksheet_Activate//<- Object Required
Sheet2.Worksheet_Activate
Sheet3.Worksheet_Activate
Sheet4.Worksheet_Activate
Sheet5.Worksheet_Activate
Sheet6.Worksheet_Activate
Sheet7.Worksheet_Activate
...
Code found in Sheet1:
'INITIAL SETUP OF THE SHEET
Public Sub Worksheet_Activate()
If Sheet9.Range("B1").Text = "TRUE" Then
'RUN INIT FUNCTIONS HERE
Me.initReqLink
Me.initVersion
Me.initCbApplicaiton
Sheet9.Range("B1").Text = "FALSE"
End If
End Sub
Thank you in advance.
Just change it to Sheet1.Activate and that event will be called.
You shouldn't be invoking event handlers manually, but that's not why you're having that error; by making the method Public you've exposed it onto Sheet1's interface, making these calls perfectly "legal" - so the methods do get invoked, no doubt.
Go to Tools / Options / General / Error Trapping, and set it to Break on Unhandled Errors.
Then you should have a better idea of exactly which statement is blowing up, most likely located somewhere in one of these:
Me.initReqLink
Me.initVersion
Me.initCbApplicaiton
..which you haven't posted.
Side note, Call doesn't make a difference, because it's an obsolete syntax that hasn't had a purpose since the advent of the implicit call syntax way back when. You never need an explicit Call statement. Never.

Why isn't this a type mismatch?

I answered this question, apparently to OP's satisfaction, but still find their question puzzling. Their question involved an expression in which a workbook object was being concatenated with a string, triggering Run-time Error '438': Object doesn't support this property or method. You can reproduce this sort of error by simply typing
?"Hello, " & ThisWorkbook
In the Immediate Window.
My question is -- why does this raise that error, instead of a error 13 -- type mismatch? A reasonable guess is that VBA tries to find a default property for a workbook object and that a default property doesn't exist. But, if so, I would expect it to be the following error from Microsoft's list of Visual Basic 6.0 error codes: Automation object doesn't have a default value (Error 443).
It is mostly of academic interest, but if the result of concatenating an object without a default property with a string is always Error 438, and that is the only way of triggering Error 438 rather than possibly another error when concatenating a string with an object, then the following code might be of use:
Function HasDefault(O As Variant) As Boolean
Dim i As Long
If Not IsObject(O) Then Exit Function
On Error Resume Next
i = Len("Hello, " & O)
If Err.Number = 438 Then
HasDefault = False
Else
HasDefault = True
End If
End Function
I've tested this on a variety of objects, and for those I've tested it on it has returned False exactly when _Default doesn't show up as a (hidden) member of the object when viewed in the Object Browser. Nevertheless, I don't quite trust this function and am still puzzled by what is going on here.
VBA will try and convert the expressions on each side of the & operator to a data value. The language spec states that:
If the value type of the expression’s target variable is a class:
If the declared type of the target is Variant, runtime error 9
(Subscript out of range) is raised.
If the declared type of the target is not Variant, and the target has
a public default Property Get or function, the data value’s value is
the result of invoking this default member for that target with this
argument list. This consumes the argument list.
Otherwise, runtime error 438 (Object doesn’t support this property or
method) is raised.
As regards your function, I'd just use:
callbyname(O, "_Default", VbGet)
which will raise a 438 error as appropriate.

Error on ActiveSelection.Tasks

Does anyone know what this means
Set oProjTasks = ActiveSelection.Tasks
I have a macro that generates status reports from MS project and exports them directly into MS Word. It is a slick tool when it works.
When I run it now it throws "runtime error '424': object required" at this point.
How do I fix this?
The code that you are displaying is a set statement, that is setting the object ProjTasks equal to the task that is selected in the message box. The ActiveSelection property returns a selection object that represents the active selection.
It could be that you are experiencing an issue where there are no items selected, in which case it will throw a trappable error code 424. There is a code snippet that you can modify from the MSDN that will work to prevent this type of error from occuring.
Here is the link to the MSDN article... just remember to not use this code verbatim, but modify it to work with your macro.
http://msdn.microsoft.com/en-us/library/aa169315%28v=office.11%29.aspx
You could try just wrapping the error check around the set statement. I've written a small macro on a non-empty project file:
Sub Testing()
On Error GoTo ActiveSelectionErrHandler:
Set oProjTasks = ActiveSelection.Tasks
If oProjTasks Is Nothing Then
MsgBox "No tasks in current project"
End If
ActiveSelectionErrHandler:
Set oProjTasks = ThisProject.Tasks 'or something like that
Resume Next
End Sub
This handles the error but as Steve has already expressed more work is required to integrate the code.
You will have to follow the code to make changes to handle oProjTasks being empty where it is expected to have some values. Otherwise you will see more errors perhaps where the oProjTasks is found to be empty.
Another alternative solution could be to launch the macro after selecting a project as the code you have quoted will work fine if something is selected.