Can't run VBA macro from the extension.

2513
1
07-23-2014 01:53 AM
Highlighted
Occasional Contributor II

Hi,

My scenario is as below .

1. I am opening an MXD from a standalone exe file.This is fine, the MXD is getting opened successfully.

2. I have developed an ArcMap extension and it it's Startup event, I want to call the the VBA macro inside the MXD.

3. The problem is here, I get an error "Error running VBA code : Error in macro"

4. The macro works fine manually, I means I open the MXD and run the macro manually, it runs fine.

Code to tun the macro is as below:

   private bool RunMyMacro(IApplication pApplication)

        {

            try

            {

                IMxDocument pMxDoc = pApplication.Document as IMxDocument;

                IMapDocument pMapDoc = pMxDoc as IMapDocument;

                //check the name of the MXD. IF it is what we wantm run the macro

                if (String.IsNullOrEmpty(pMapDoc.DocumentFilename) == false)

                {

                    if (pMapDoc.DocumentFilename.Contains("MyMXDName"))

                    {

                        //run the macro

                        IVbaApplication pVBAApps = pApplication as IVbaApplication;

                        pVBAApps.RunVBAMacro("Project", "ThisDocument", "Trace_Transformers_LV", null);

                        return false;

                    }

                }

                return true;

            }

            catch (Exception ex)

            {

                string errMsg = ex.Message;

                string stkTrace = ex.StackTrace;

                return false;

            }

        }

//Code of StartUp event of an extension

public void Startup(ref object initializationData)

{

IApplication pApps = initializationData as IApplication;

RunMyMacro(pApps);

...

}

Earlier, the macro was getting called from the EXE it self and it works fine .However, after running fine for a week or so, it starts throwing an error. To work around this, I'm trying to call the macro from the extension, but it is also not working.Even a simple HelloWorld macro is not getting called from the extension.

I would really appreciate any help on this issue.

Thanks,

S.

Tags (2)
Reply
0 Kudos
1 Reply
Highlighted
by Esri Contributor
Esri Contributor

The following link from Microsoft might be of some help:

How to dynamically add and run a VBA macro from Visual Basic

https://support.microsoft.com/en-us/kb/219905

Just as an FYI, ESRI no longer supports VBA. Support for VBA stopped at ArcGIS version 10.0. Please migrate your VBA customizations to ArcObjects .NET (VB.NET or C#). The link below shows how this can be done.

Migrating VBA customizations to ArcGIS 10.1

http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/#/Migrating_VBA_customizations_to_...

Reply
0 Kudos