Referencing VBA code in .vb files (with UiPath Invoke VBA activity) - excel

So I'm not sure if this question requires knowledge on the UiPath software in order to be answered or not. I have been developing a lot of Excel macros for my company, and as I get more and more macros, it's harder to manage them.
We have been working on some robots as well within the company, and I notice that the robot has an activity where you can invoke VBA code within an Excel application scope. This activity reads a .vb file with code and invokes whatever method you specify within that file as a macro in the activated Excel application.
Ideally I would want to extract all my VBA code into separate files, and have the robots execute the macros through this activity. This would make it a lot easier to manage the code.
My question is then, if it is somehow possible for me to also extract the modules I've created that contain utility methods that I repeat throughout many macros into a .vb file, and reference this in the other macro files?
I don't know exactly how this activity invokes the code and what restrictions are placed on it. Within Excel, I can store re-usable methods in modules and call on them from other modules. This is what I want replicated on a file level. If there was some way of adding import statements to the top of the code to retrieve methods from other modules, so I could call them within the file.
My worries (and assumption) is that the activity simply reads it as a text file, and just imports it as a macro right into Excel. That if I wanted to reference any modules within my method, the modules would have to already exist in the Excel application.
I could always paste the utility methods into every .vb file, but that sort of defeats the purpose of making it easier to manage.
Is there anything I can do here?
Thanks,
TRS

You definitely can reference external assemblies (.dlls) in UiPath. I haven't done it with VB.Net Projects, but I have done it with C# Projects which is in this case, the same thing.
Please, follow this tutorial: https://www.uipath.com/kb-articles/how-to-include-external-dll
To be able to generate the .dll, you will need to download Visual Studio Community Edition and follow a couple of tutorials on how to compile VB code.
All your VB code will exist in this .dll. This would be your general repository or main library that you would access every time that you need it. As I understand, this is your main goal anyway right? "To access utility methods".
I don't know if inside your macros, you use specific Excel references that could lead to compilation issues. So, be ready to reference everything that you need inside the code.
At the end, to access your custom methods, you would need to reference the .dll and use the activity called: Invoke Method.
I hope this helps.

Related

Call VBA UDF across different VBAProjects

I am developing an Excel Add-in to help harmonize calculations done by my coworkers. It is basically a collection of industry-specific UDFs bundled together in regular and class modules.
There are a number of functions that are highly specialized and are used in only specific applications. As such, these functions would be included as a module in the individual workbooks where they will be used.
I would like to be able to re-use several "helper" functions from the Add-in to simplify these "external" functions. As near as I can tell, UDFs are not available across VBAProjects. I've tried to add a reference to the Add-In, but while the Add-in is loaded, Excel complains that
"Name conflicts with existing module, project, or object library"
which makes sense. It's trying to add something with the same name.
Removing the add-in and only adding it as a reference seems to give the desired result (workbook-specific UDFs can call add-in UDFs and Subs), but it only appears to work for that specific workbook.
Is there a way to call UDFs across separate VBAProjects?
Additional trial-and-error testing has revealed the error to me: I was attempting to add a reference to the add-in to itself, which would presumably be recursive and cause issues.
Selecting a module in the specific workbook allowed me to add a reference to the add-in and successfully use the functions it contains.

Add external libraries using code in VBA for Excel

I am trying to add external libraries and use them in VBA for Excel. I have found two questions about it, but I can't get them to work in a real application.
Both of them work fine on their own, but when I try to use them in a program I get a compiler error. It seems that I cannot compile the program before I add the library (makes sense, I use unknown objects) and I cannot add the references before I compile the program.
How can I solve this?
Question 1:
Connect references (Tools>References) with VBA code (macros)
Question 2:
How to add a reference programmatically
It seems the references can be added using the Workbook_Open sub in Excel. This sub automatically runs when the workbook is opened. It works fine if it does not call any routines that use objects from the libraries that need to be added.
One of my concerns was that the macro would not run if the user has to enable macros after the book is opened. Most of the information I can find indicate that it should still run. I have not been able to test it yet though, because it seems very difficult to "untrust" a workbook to has been set to "trusted" before.

Is there a place to store VBA code that's accessible to all Microsoft Office products..?

Is there a way of storing VBA source code that's common and accessible to all Microsoft Office products..? I have a variety of functions which I use in both Access and Excel, and sometimes even Word. I modify and add to the the functions and modules frequently. The copying, pasting, and maintaining of code between projects can get tedious, and I inevitably overlook something.
I know I could do something like write a class/ocx/addin in VB6 or DotNet, compile it, and reference it in my VBA projects, but I was hoping for something more simple.
If there was a way to store VBA code in independent files, and open them with the VBA IDE in a standalone mode of some sort, that would be outstanding, but I know the IDE doesn't work that way...as far as I know.
I know I could do something like write a class/ocx/addin in VB6 or DotNet, compile it, and reference it in my VBA projects, but I was hoping for something more simple.
Unfortunately that's your best bet.
As you know VBA code is hosted, wrapped-up in a host document: a VBA add-in is still tied to its host document (.xlam, etc.), and as such can't be shared with another host.
In VBA-land what's meant to be shared between VBA projects, is type libraries - things you compile separately from the VBA project, and reference from as many projects as you like.
If you have a VB6 IDE, you can compile a 32-bit DLL that VBA projects can reference. The problem is that it won't work with 64-bit hosts - the solution is a .NET type library, written in the .NET language of your choice, made COM-visible. Note that COM-compatibility does restrict what you can expose in your API: for example you can't expose generics, and method overloads will look weird.
If a type library isn't an avenue you wish to explore, then your choices are rather limited, and IMO sub-optimal.
IMO the only thing that can "work" is a bunch of exported code files in some common folder, and the VBA projects that need to use that code need to literally import these code files. The risk here being, that if you make any changes, other VBA projects using an unmodified version of that code will not "see" these changes, IOW by doing that you're multiplying the number of times you need to fix a bug by the number of projects using that code.
Or you could have some code that uses the VBIDE extensibility type library to ensure the set of imported modules always match exactly with the exported files in that common location.
If you want to use VBA in the same Office program (e.g. one Excel file to another, or one Access file to another), you don't need to use any special kind of file. You can add a reference to an external database or worksheet with macro's enabled.
Navigate Tools - References - Browse - Excel/Access file - Add the file
See the following screenshot:
You can't use files across different Office applications this way, but you shouldn't. Each application has different built-in functions, so your code is likely incompatible anyway.

Is it possible to store reusable VBA code in a library and call from Outlook?

I have code written in Outlook 2010 VBA that I want to share with co-workers (an Outlook rule runs that calls the code, which saves the current e-mail to a network folder as a text file). What I would like to do is save the VBA code in a library somewhere on the network, and have Outlook call it there. This way, others can call the same code and there is one set of code that all instances of Outlook can point to. If you can point me to info on setting this up I would appreciate it!
I do a similar thing in VBscript programs, using executeglobal to basically run a file containing the functions (acts like an include file) and am looking for how to do it in VBA.
Well it seems its not as easy as I was hoping. After perusing the links supplied by MP24 (Thanks MP24!), I learned the Outlook OTM file is not really intended to be shared. It errored for a colleague when I saved it to a network drive and she tried to use it via tools/references. To share code, one can export it as a .bas file, and another user can import it. This will add it to their own code if some exists already. The proper way seems to be to write an add-in, but that is beyond the scope of what I need for what I am doing. So, if a colleague wants to use my code I'll export and they can import it. The trouble is if I change something they will have to delete it and re-import. Oh well.

How can I set up an Access VBA library

I have some VBA code that I use across multiple projects - about 40 or so different data bases all use many of the same functions.
I try to use the same code in application each for consistency. Is there a way I can put all the code in one library and make it available to each data base? I'm getting tired of copying the code to a new data base every time I want to re-use it. I'm especially getting tired of maintaining it in multiple places when I want to change the functions.
I have the same question for Excel - can I create an Excel library and include it in all my workbooks?
If the VBA code works in VB6, you could create a DLL using VB6. The DLL would be a library of methods you could use across board just by referencing the DLL.
If it does not, you should create an Ms Access MDA file (add-in file) for all your reusable code, that can be added into any Access (or probably Excel) project.
Create one or many excel's per project, save those as *.xla. (Excel Add-In)
Go to your Excel Options and configure to load automatically those add-ins or thru VBA in your workbooks.
Using VBA Automation:
Application.AddIns.Add(XLA_Path)

Resources