This may be a silly question - I haven't found anything on Google about this. No new programs populate in the task manager when VBE is opened from an Office Application, and I don't see an executable that looks probable. DLL maybe?
Some Office applications provide the Run method for the Application class. For example, see Run Excel Macro from Outside Excel Using VBScript From Command Line.
Yes, you can call VBA macro at runtime.
There are applications that don't provide any specific methods for that, for example, Outlook. In that case you can use the late-binding technology (see Type.InvokeMemeber) - see Calling Outlook VBA Macro from outside (VB/VBScript/C#).
You can invoke a CommandBarButton's OnAction macro as a proxy for Application.Run.
For details, see this question How can I run a macro from a VBE add-in, without Application.Run?
Related
I'm maintaining a legacy Excel workbook that has a significant amount done in VBA. For threading's sake, I need to move some of that work out of VBA. I have prototyped a DLL written in C#, but deployment (with registration) is more difficult.
Can you interact with the new HTML5 style add-ins using VBA? So, in this case, writing a function in Javascript, and then calling it with specific parameters from the existing VBA macro. If so, how?
Unfortunately this is not a scenarios supported by Microsoft nowadays.
I am fairly experienced with Excel and VBA and I know how to create add-ins. However, I haven't found a satisfactory solution for protecting my VBA code once it is in an add-in. How can I make this code inaccessible to the user?
You can protect your VBA code against viewing, however it is relatively easy to crack.
You can easily Google out how to protect a VBA module.
Alternatively, you can create an Add in using Visual Studio. Yet again, .Net code can be decompiled in seconds. Thus, you would need to use a code obfuscator.
I have this rather heavy Excel macro. When it runs Outlook freezes.
I know it is not using all the CPU power because
a) I have a powerful multiple-core machine
b) all my other programs (even heavy-duty ones) are running fine.
It seems like Outlook and Excel are sharing a process that Excel is hogging.
My macro does nothing Outlook-related, at least not knowingly. It pulls data from Bloomberg and does some calculation.
I appreciate the horse has very much left the corral...however, I too have experienced this and with no references in my code to the Outlook model. I've also noticed my Excel locking up whilst macros in another Excel instance are running.
To resolve this you need add a
DOEVENTS
line to your code, to quote the MSDN site
DoEvents passes control to the operating system. Control is returned
after the operating system has finished processing the events in its
queue
Where to put the DOEVENTS line very much depends on your code, but in a rather heavy macro of mine where I make a number of SQL calls, I've placed it just before each SQL call which appears to work well. Allowing me to work quite happily in one Excel instance whilst the background instance does a number of SQL fetches.
The same thing happened to me when migrating my macros to Office 2016. The solution: uncheck the library "Microsoft Office 16.0 Object Library", which is included by default when you create a new vba module.
Keep in mind that all calls to the Outlook Object Model are marshaled to the Outlook's main thread, so if your Excel macro is using the Outlook Object Model, Outlook can and will become unresponsive.
Extended MAPI (C++ or Delphi) can be used in a multi-threaded environment.
What does your macro do? Please post the relevant snippet of your code.
I have the same issue. I avoid other Excel workbooks locking up when I run code in a workbook by opening them in new instances. Note that by default Excel now opens all workbooks in the same instance. For workbooks that I have VBA in and for large workbooks - I always try to open in a new instance to avoid freezing and confusion over where the VBA should run. I do this holding the ALT key down when opening Excel (if another instance is already open) and verify I want to open a new instance. This works for other applications as well.
However - I don't know how to tell Outlook to open in a new instance since it is the only instance of Outlook running. Sometimes Outlook locks up when I run code in Excel and sometimes it doesn't (running the same code). I've had some luck in closing Outlook and opening it back up to get it "detached" from my Excel instance I want to run long running macros in.
I just started to learn VBA. As far as I understood, one uses VBA to write macros in MS-Excel. But I wonder if it the only area of application for VBA. Can it be also used like many other standard languages - like Python, for example.
If it is the case, how can I compile a VBA program? Should it be compiled? How can I run such a VBA program?
VBA is compiled to p-code. P-code is an intermediate language that requires an additional runtime in order to execute. This runtime is hosted by most Microsoft Office applications (including various non-Microsoft applications).
In short, you cannot write a VBA only app that is compiled to an .EXE.
To create a stand-alone VBA-like program, you would need to use Visual Basic 6 or earlier. The successor of Visual Basic 6, of course, is VB.NET, which is a very different animal.
VBA can be licensed, and there are quite a few pproducts outside office that use VBA. MS no longer issues new licenses. There are non-MS VBA implementations, like from Summit software. In any case you need to have your own product that would host VBA.
A notable application supporting VBA is AutoDesk AutoCAD. It licenses the VBA runtime and has its own object model.
If you don't compile the program ahead of time (in the Visual Basic Editor click Debug -> Compile), then Microsoft Office compiles it at run time. You really should compile frequently though because that is how you find compile errors.
How you run a VBA application depends entirely on how you have set it up to run. In Excel for example you can have the Workbook_Open event start your code when the workbook is opened or create custom menus that users click on to run the code. In Access, you can set a form to display when the database opens or create an autoexec macro that will run when the database opens. etc. etc.
Like someone else said above, you can't create .exe files of VBA. The code has to run in a Microsoft Office Application.
How can I call a function defined in a COM Addin using excel macro ?
Basically I have created a Excel COM Addin which does some bunch of stuff.
Now I want to invoke this via excel macros.
Is it possible to do it ?
Or
Is it possible to install COM Addin using a macro ?
You can reference COM libraries in the VBA editor by going to tools -> references (I think) then you can select (or browse for) whatever DLL you've created. When you've selected it there you can use the functions contained within in the usual way.
You should see a big list of libraries to select from.
When I've done VBA development in the past I normally end up using a few things like the Microsoft XML libraries through this method.