none
Not able to load Macro programmatically in word 2016 RRS feed

  • Question

  • Hi,

    I have copied the macro in to the below path,

    "C:\Program Files (x86)\Microsoft Office\Office16\STARTUP"

    After that if i opened any word 2016 document manually the macro is getting loaded,but if i open it through the c# .net application using word interop i'm not able to load macro in the document.

    Kindly suggest us asap.

    Thanks in advance..

    Wednesday, November 30, 2016 11:28 AM

All replies

  • Hi sumathi sakthi velayutham,

    first of all you only mentioned the description of the problem. it is more helpful for us if you post a code.

    from your description I can understand that you are trying to run a macro when you open the document.

    so here it is possible that you are trying to run AutoExec macro.

    but there is another way named AutoOpen macro.

    in these both ways there are some situations when this macro execution get suppressed.

    Macro Behaviors in Different Situations

    The following table summarizes the behavior of these two macros   when Word is started, or when a document is opened by various means: Action                                          AutoExec       AutoOpen
    -------------------------------------------------------------------------------------------------------------
    ->Start Word with a blank document by typing                           Runs           Does not run
    the following at a command prompt:
    Winword.exe

    ->Start Word with a saved document by typing                           Runs                 Runs
    the following at a command prompt:
    Winword.exe C:\<Yourfile>.doc

    ->Embed a Word document in an OLE container                          Does not run       Runs
    by typing the following at a command
    prompt:
    WinWord.exe /embedding C:\<Yourfile>.doc

    ->Use code to automate Word and open                                     Does not run        Runs
    C:\<Yourfile>.doc by using the following
    code:
    Set oWord = CreateObject("Word.Application")
    oWord.Visible = True
    oWord.Documents.Open "C:\YourFile.doc"

    ->Browse to the document in Internet Explorer                            Does not run        Runs
    or the WebBrowser control

    Reference:

    Description of behaviors of AutoExec and AutoOpen macros in Word

    if you see the second last point in above mentioned table then you will find that if you are trying to open the document using automation code then Autoexec Macro will not run. so the solution is to create AutoOpen Macro.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 1, 2016 12:37 AM
    Moderator
  • Thanks for your response Deepak,here is my code

    ************************************************

    using Microsoft.Office.Interop.Word;
    using System;

    namespace ConsoleApplication8
    {
        class Program
        {
            private static object vk_false = false;
            private static object vk_true = false;

            static void Main(string[] args)
            {
                Program obj = new Program();
                string filename = @"C:\testFiles\TestDocument.docx";
                object missing = System.Reflection.Missing.Value;
                Document document = new Document();
                Application WordApp = new Application();

                try
                {
                    document = WordApp.Documents.Open(filename, ref missing, ref vk_true, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref vk_false, ref missing, ref missing, ref vk_true);
                    document.RunAutoMacro(WdAutoMacros.wdAutoOpen);
                    WordApp.Visible = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }

    **********************************************************

    As per your suggestion i have used "WdAutoMacros.wdAutoOpen" option but the file opens without macro and its getting hanged after that.

    Looking forwad for your answers..


    sumathi

    Thursday, December 1, 2016 7:42 AM
  • Hi sumathi sakthi velayutham,

    I try to test your code on my side. so I created a AutoOpen macro and run the code to open the file.

    on my side file open successfully , AutoOpen Macro get called without getting hanged.

    it working perfectly on my side.

    so I would recommend you to check the following things.

    (1) Make sure when you open the word file manually AutoOpen Macro get called.

    (2) you need to set the security settings of Word Application to low.

    in the trust center settings you need to select "Trust all installed Add-ins and Templates".

    (3) can you tell me what code are you executing in macro. because I just use a simple msgbox in that to verify that it is working when I open the document. is it complex code or need any other resources or dependent on any other MS Office Application? please let me know about this so that we can try to provide further suggestions.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 2, 2016 2:49 AM
    Moderator
  • Hi Deepak,

    Yes its a complex macro code, the macro file format is .dotm and it placed in the startup path.

    You said,

    ->   in the trust center settings you need to select "Trust all installed Add-ins and Templates".

                             this option is not available,were we have to enable this option in word document or in code?

    If possible can you share your sample code what was worked.


    sumathi

    Friday, December 2, 2016 1:54 PM
  • Hi sumathi sakthi velayutham,

    you had mentioned that,"If possible can you share your sample code what was worked."

    I test with the your above posted code and it worked on my side.

    as you had mentioned that the macro is complex.

    if possible then try to share the code with us. so that we can try to look in to that and test it on our side.

    we will try to see at which point code get failed and word get hanged.

    without code at this point I am not able to suggest you properly.

    if you can then try to simplify your code.

    other thing you had asking about to turn on the option.

    in Word 2016, you can do like mentioned below.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 5, 2016 4:47 AM
    Moderator
  • Hi sumathi sakthi,

    Did you check Startup location set to "C:\Program Files (x86)\Microsoft Office\Office16\STARTUP".

    (File >>Options >> Advanced >> File Locations (Under 'Genaral' category) >> Select Startup >> Modify)


    .

    Tuesday, December 6, 2016 3:01 PM