Invoke VBA Error in StudioX

Hi There - I’m attempting to use an invoke VBA command in a StudioX script to call on text scripts that I’ve saved in a project folder. I’m trying to structure the project this way, as I want to avoid creating separate macros for each individual file that I am accessing - as I am iterating through several excel files and copying and pasting certain cells. Whenever I try to run it I get the following error, even though I have the box checked it refers to. Image below of my invoke VBA command - I’m probably doing it wrong.

image

VBA Text file - Sub SelectRange()
’ Select cells A1 to F47 on the active worksheet
Range(“A1:F47”).Select
End Sub

Error message below -

23.10.5+Branch.support-v23.10.Sha.1a13e22180538f0c894f0828e9d5af447df5806e

Source: Invoke VBA

Message: In order for this activity to work, Trust Access to the VBA project object model must be enabled from Excel (File > Options > Trust Center > Trust Center Settings > Macro Settings > Select the Trust Access to the VBA project object model check box)

Exception Type: UiPath.Excel.ExcelException

UiPath.Excel.ExcelException: In order for this activity to work, Trust Access to the VBA project object model must be enabled from Excel (File > Options > Trust Center > Trust Center Settings > Macro Settings > Select the Trust Access to the VBA project object model check box) —> System.Runtime.InteropServices.COMException: Cannot run the macro ‘SelectRange’. The macro may not be available in this workbook or all macros may be disabled. at Microsoft.Office.Interop.Excel._Application.Run(Object Macro, Object Arg1, Object Arg2, Object Arg3, Object Arg4, Object Arg5, Object Arg6, Object Arg7, Object Arg8, Object Arg9, Object Arg10, Object Arg11, Object Arg12, Object Arg13, Object Arg14, Object Arg15, Object Arg16, Object Arg17, Object Arg18, Object Arg19, Object Arg20, Object Arg21, Object Arg22, Object Arg23, Object Arg24, Object Arg25, Object Arg26, Object Arg27, Object Arg28, Object Arg29, Object Arg30)
at UiPath.Excel.WorkbookApplication.ExecuteMacro(String macroName, IEnumerable1 macroParameters) at UiPath.Excel.WorkbookApplication.InvokeVBA(String codeFilePath, String entryMethodName, IEnumerable1 entryMethodParams)
at UiPath.Excel.Activities.Business.InvokeVBAX.ExecuteDoWork(ActivityContext context, ExecuteVBADescriptor descriptor)
— End of inner exception stack trace —
at UiPath.Excel.Activities.Business.InvokeVBAX.ExecuteDoWork(ActivityContext context, ExecuteVBADescriptor descriptor)
at UiPath.Shared.Activities.BaseParentActivityWithDescriptor`2.OnCompleted(NativeActivityContext context, ActivityInstance _)
at System.Activities.Runtime.ActivityCompletionCallbackWrapper.Invoke(NativeActivityContext context, ActivityInstance completedInstance)
at System.Activities.Runtime.CompletionCallbackWrapper.CompletionWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)

Hi @mwells

In the error message only they have given what to do

Message: In order for this activity to work, Trust Access to the VBA project object model must be enabled from Excel.

Change this setting in your Excel.

(File > Options > Trust Center > Trust Center Settings > Macro Settings > Select the Trust Access to the VBA project object model check box)

Once you’ve enabled Trust Access to the VBA project object model, you should be able to run your activities without encountering this error.

Hope this helps :slight_smile:

@mwells,

Long story short, you will have to enable Macros in excel. This is one time settings you will have to do it.

Follow these steps:

Macro Settings

Thanks,
Ashok :slight_smile:

1 Like

Hey guys - i do have that checked unfortunately. That was the first thing I tried. Do you think there is an issue with me calling VBA code from a text file rather than saving the macro within the file itself? I was trying to just call the code externally from the text file rather than save a macro in 30 individual files.

@mwells,

I was recently able to recreate this issue and it has strange solution but it works.

Thanks,
Ashok :slight_smile:

1 Like

Thank you for the tip. This is probably a dumb question, but how am i supposed to save my text file to an ‘input’ screen within the project? I am using studioX and don’t see anywhere to do so.

@mwells,

You can save it in your project folder.

Hey - I created an input folder (there wasn’t one in my project folder initially) and saved it in there. I referenced the file as below. Is this something I’d have to do in Studio rather than StudioX?

image

Folder with the red arrow created by me.

No need of dot prefix to the folder name but this also should work.

Thanks,
Ashok :slightly_smiling_face:

Unfortunately it does not. Maybe I’ll put in a support ticket.