I want to register a function kind of CalculateHours(work_hour, rest_hour) to Excel VBA, in an Addin like Works.xla
I tried using Application.MacroOptions to register the function. The following code is in a Class file, the function is in another module file. They would load when we open Excel and the Addin.
Code:
Private Function AddFunctions()
With MyFunction
Application.MacroOptions .Name, _
.Description, , , , , .Category, , , .HelpFilePath
End With
End Function
Expectation:
I wanna get the argument help, function description in Excel function wizard as other built-in functions. With the help button link to my help file.
Result:
Error number: 1004 Application-defined or object-defined error
or
Method "MacroOptions" of object "_Application" failed
Is there anything (or everything) wrong?
I have kind of 10 functions and need to add them automatically to Excel function wizard every time load the Addin.
07/12/2016 well after dragging my function around following pieces of advise from some posts and doing a number of other pointless things, I found this error happens if Application.MacroOptions Description:=FuncDesc exceeds 255 characters. So essentially don't get too verbose with the description of your user defined function, or just add a
If Len(FuncDesc) > 255 then
Scary warning message about Run-time error '1004'
End if
Another possible issue (and solution) that was affecting me... The function code needs to be written in a module - if it it written in the ThisWorkbook page, Excel won't be able to find the code.
For ease of future readers, here is a compilation of the multiple answers (i.e. list of potential things to check)...
The function code needs to be written in a module (not ThisWorkbook)
Make sure the function description does not exceed 255 characters
If defined in another workbook, try including the workbook name - e.g. Macro:="'PERSONAL.xlsb'!Macro/UDF_Name"
If defined in an Excel add-in, call ThisWorkbook.Activate before Application.MacroOptions
Hope this helps, please one-up the respective solution poster if their answer helped you đđź
I understand that question is old, but will post my solution to this error, as it may be common and exception message is not informative.
I fixed it with passing a macro/UDF-holder workbook name into "Macro" parameter, like "'Workbook.xls(x/m/b)'!Macro/UDF_Name":
' Adding a macro from Personal.xlsb
Application.MacroOptions _
Macro:="'PERSONAL.xlsb'!Macro/UDF_Name", _
Description:="Description", _
ArgumentDescriptions:=ArgumentsDescription()
Another observation: if the Application.MacroOptions in being run from VBA add-in with ThisWorkbook.IsAddin = True e. g. from Workbook_AddinInstall or Workbook_AddinUninstall, and there is no other workbook opened, Excel throws error 1004. Problem can be solved if one calls ThisWorkbook.Activate before calling Application.MacroOptions.
Related
I am using MS Access 2016, Windows 10, USAF Laptop running SDC.
I am having an issue with debugging some code. When I hit a line in Break mode I get a Run-time Error '13':Type Mismatch, When I run the exact same code outside of break mode, the code functions properly.
Here is the basic code that is causing me problems:
Private Sub btn_ReqKey_Click() 'This is not all of the code only those lines that are necessary
Dim trgt_CACID as String
Dim Temp_KeyCode as String
trgt_CACID = Me.CACID.Value 'Pulls value from short text field of "1234567890"
'Here is the issue
Temp_KeyCode = UnlockCode(trgt_CACID) 'Run-time Error '13': Type mismacth
More code
End Sub
Public Function UnlockCode(ByVal CACID as String) As String
lots of code that generates a 25 digit Alpha code
End Function
When I click on the button the code fails at "Temp_KeyCode = UnlockCode(trgt_CACID)" Run-tine Error '13': type mismatch.
However, if I reset the code execution and run this in the immediate window without pressing the button.
trgt_CACID = "1234567890"
?UnlockCode(trgt_CACID)
Returns the proper code without throwing an error. So the function UnlockCode() works properly, just not at execution.
Additionally, When in break mode from pressing the button and I attempt the same line of code in the immediate window, it returns the same run-time error '13': Type mismatch.
Furthermore, I exit Break mode, reload the variable and execute the same line of code it works again.
So I know something in the execution environment is being affected, I cannot figure out what that is. This is the first time I have seen a disparity with how the immediate window functions as compare to code execution.
Side Note/Quirk, in the immediate when I enter the function, the helper pops up to guide me for the variables required. When I type the same in the module code, the helper pop ups do not display. So weird. Right? Further evidence something is different between the two environments.
Any help or assistance would be greatly appreciated.
Thanks in advance.
KO
Based on the discussions with Erik A and Gustav, I changed the Function name that was having a conflict with a table field name I was using. Once I changed everything to be dissimilar: table Field Name, Form Textbox name, and Function being called. Everything worked fine. So apparently the context I was calling from already had something using the name of the function and that field name was being picked up and sent to a function with the same name as the field. This created a subtle conflict that explains the disparity between execution in the context of table and form and when being Manually called in the Immediate Window AND why it worked in the Immediate window, but not during execution. Thank you Erik A and Gustav for their assistance.
Try running it with a dummy function:
Public Function UnlockCode(ByVal CACID As Variant) As String
UnlockCode = String(25, "0")
End Function
If that runs, debug your secret function.
MS Access (2007 - 2016) in Office 365
I'm trying/failing to capture a value passed into Access from the command line using the Command() function in a Macro. THis is the macro that I created with the wizard...
If Command()="Update_Burndown_Metrics" Then
RunSQL
SQL Statement insert .... blah, blah
End If
No error when I save the macro, but when I run...
The expression you entered has a function name that Microsoft Access can't find
If I replace the...
If Command()="Update_Burndown_Metrics"
with
If 1=1
It runs fine. IOW, it's not the SQL. It's the "Command() function that it can't find.
I got the idea to use Command() from Opening Microsoft Access with parameters . Doesn't seem to work for me. But that coding approach is also confirmed here... http://www.utteraccess.com/wiki/Command-Line_Switches . So I think it's something else.
Eventually, I would like to pass the Update_Burndown_Metrics arg using /cmd on the command line.
Why can't it find Command() as a valid function ? Is it a scoping thing? Do I have to give Command() context somehow, maybe with some sort of prefix ?
I can't seem to reproduce the issue that you are describing, though, I am using an earlier version of MS Access, and so there may be some differences in the behaviour of this function.
The Command function should be globally accessible, even outside of VBA (it can be referenced by the ControlSource property of a text box, for example), and so this isn't an issue of scope.
I do observe from the Office 365 documentation, that the Command function is being invoked without the use of parentheses in the sample VBA Sub provided; therefore it may be worth you trying your code without including such parentheses, e.g.:
If Command = "Update_Burndown_Metrics" Then
MsgBox "Test succeeded."
Else
MsgBox "Test failed."
End If
The workaround I created is to create a function in vba that gets and returns the command...
Public Function GetCommand() As String
GetCommand = Command()
End Function
Then...
If GetCommand() = "Update_Burndown_Metrics" Then ...
I am trying to have the file dialog box pop up so the user can select a file path to export a file in VBA but for some reason it throws this error on the following line of code.
Error: Method 'FileDialog' of object '_Application' failed
Code: longResult = Application.FileDialog(msoFileDialogFolderPicker).Show
All Code:
If choice = 6 Then
Dim intResult As Long
Dim strPath As String
'the dialog is displayed to the user
longResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog
If intResult <> 0 Then
'dispaly message box
Call MsgBox(Application.FileDialog(msoFileDialogFolderPicker _
).SelectedItems(1), vbInformation, "Selected Folder")
End If
Else
End
End If
I am really unsure how to fix this issue. I checked my syntax and everything.
I know that this is a bit of an old question at this point, but since it doesn't actually have the answer, and I needed one today, I'm going to chime in with what I found just in case anyone else needs the answer too.
To fix this you need to add a reference to "Microsoft Office [yourversion] Object Library" in Visual Basic Editor >> Tools >> References...
The dialog in question should look like this:
Was trying to do the same thing myself and found this question. I realize that it is over a year old.
Try using the actual number (4) instead of msoFileDialogFolderPicker, that worked for me. I think something needs to be installed for the msoFileDialog constants to be initialized, when I tried printing any of the constants defined in the help file in the immediate window nothing was printed.
Also, why does your code have one variable longResult and one variable intResult?
Almost nothing is an integer in VB6 as integer is a VB4 16 bit type. Win32 Integers are called Long in VB6/VBA.
This was to make porting 16 bit code to 32 bit easy.
Check out http://msdn.microsoft.com/en-us/library/office/ff865217%28v=office.15%29.aspx for more information on proper syntax with FileDialogue.Show method. It appears you need a Set in front of your variable.
If you are after some cool UI, you can checkout my Github for sample database using .NET wrapper dll. Which allows you to simply call a function and to open filedialog with file-drag-and-drop function
Dim FilePaths As String
FilePaths = gDll.DLL.ShowDialogForFile("No multiple files allowed", False)
'Will return a JSONArray string.
'Multiple files can be opend by setting AllowMulti:=true
here what it looks like;
I migrated a database from access 2003 to access 2007.
It seems to work but when I clicked the buttons from A-Z I'm getting this error message:
"Microsoft office can't find the object 'A_Z Schaltfläche'
If 'A_Z Schaltfläche is a new macro or macro group, make sure you have saved it and that you have typed it's name correctly"
I didn't make a new macro but I deleted a word in one of the tables which I think causes the problem: "Like [Formulare]![Frm_Fahrzeugdaten]![Schaltflächenkriterium]"
I found it under the "Record Source" and under one field, that expression is written in the criteria field. If I don't delete this I'm getting a box which says: "Enter Parameter Value" Formulare!Frm_Fahrzeugdaten!Schaltflächenkriterium
My skills in VBA is not really so good, and I need some help how to correct the problem
Thanks for your help.
=======
additional info's:
When I open the VBA code under the Form_Fahrzeugen, here is what I saw:
Private Sub Auswahl_Click()
On Error GoTo Auswahl_Click_Err
' Programmablauf
Me.Filter = "[A_Fahrzeugtyp] like '*'"
Me.Namen_Filter.Value = 27
Me.Schaltflächenkriterium = "*"
Schaltflächenkriterium = "*"
Me.Requery
Me.lfd_Name.Requery
DoCmd.GoToRecord acDataForm, "Frm_Fahrzeugdaten", acGoTo, 1
Me.lfd_Name.Selected(1) = True
Me.A_Inventarnummer.SetFocus
GoTo Auswahl_Click_End
Auswahl_Click_Err:
Call ErrorHandler("Form_Frm_Fahrzeugdaten", "Auswahl_Click", Error$, Err, Erl)
Resume Next
Auswahl_Click_End:
'AusfĂźhrungen vor den verlassen der Routine
End Sub
Does it have something to do with the macro name? This is the macro name by the way: "A_Z Schaltfläche"
If I go to the design view of "A_Z Schaltfläche" this is what I got, the conditions are marked yellow I'm not really sure if this is a good sign though:
Thanks
==============
Updates about my problem:
I was able to find the solution of my problem. It was the version of my microsoft office which was causing it. The original database was written in German and when I did the migration, I migrated it to the english version of Access 2007. The reason why it can't find the object because of the name "Ereignisprozedur" in German and "Event procedure" in English. I changed it to Event Procedure because the error says:
"Microsoft Office Access can't find the object "Ereignisprozedur"
If Ereignisprozedur is a new macro or macro group, make sure you have saved it and that you have typed its name correctly
Access was unable to locate the macro or VBA function. If you are trying to call a macro, make sure that the name of the macro and the name of the macro group are spelled correctly.
If you are trying to call a user-defined VBA function, be sure to use the following syntax:
=FunctionName() or =FunctionName(argument1,argument2,...)
Make sure that the function is either:
Defined in the code for the form or report.
- or -
A public function that is in a module (not a class module)."
And the word "Formulare" to Forms. Then the program works.
I can't understand why microsoft programs are language independent???
Do you mean you deleted a word in one of the Queries? What it is is a reference to a form called Frm_Fahrzeugdaten and a control (field, column) called Schaltflächenkriterium. It is under criteria, so the query is saying:
Select such and such where this field (column) is Like this form and this control
It is usual to have
Like "*" & [Formulare]![Frm_Fahrzeugdaten]![Schaltflächenkriterium] & "*"
Or
Like [Formulare]![Frm_Fahrzeugdaten]![Schaltflächenkriterium] & "*"
That is, with wild cards (*)
[Schaltflächenkriterium] seems to me to be the name of an edit control (textbox) in form [Frm_Fahrzeugdaten] for the user to filter a record set.
If you deleted the textbox control, just re-insert it and name it correctly.
Very simple problem: I have a Public Sub (in a module) that I want to call from a button on a form. The name of the function I want to call from the module is GenerateKML.
I've read this post:
How do I call a VBA Function into a Sub Procedure
And tried all of the suggested methods, none of which are working for me. There may be a problem with my code, but when I'm in Code view (editing the module) and press the 'play' button the code runs properly (a KML file is created).
If I use the second method suggested (call a subroutine in a module from a form) I get this error message:
Compile Error
Expected variable or procedure, not module
And if I use the third method (call a subroutine from a form without using an event procedure) I get this:
The expression On Click you entered as the event property...: The expression you entered has a function name that [my DB name] can't find.
So I suspect there's something wrong with how I'm calling the code I want to run.
This is how the code for my module starts:
Option Compare Database
Public Sub GenerateKML()
'
' GenerateKML Macro
' Macro recorded 26/09/2006 by simon_a
' Adapted and imported to Access by SAA
' 03 aug 2007 - v3.0 - 2007 08 06 19 24
'
' DECLARE VARIABLES
Dim filename As String
Dim docname As String
Maybe you have named your code modules the same as the procedures within it.
(just a thought)
i.e. the sub GenerateKML, sits in a module you have named GenerateKML. This creates a conflict & resulting error message.
If you have named your module GenerateKML as well as your sub, you need to call it using:
GenerateKML.GenerateKML arguments
(or just rename one or the other, which is probably easier)
try renaming your module with a mod prefix: modGenerateKML.
You don't reference the module name from forms, just the name of your public sub or function.