none
Accessing/reading other open Word documents from "current" instance of Word 2013 RRS feed

  • Question

  • Hi , I have a Word 2013 add-in that populates various bookmarks with content from a server. One of the bookmarks is a document ID, which is a unique ID for that individual document. If the user opens a previous version of this document (ie same doc ID) but it has a different saved name while this current document with the same ID is open, is it possible to have Word/the add-in programmatically (C#) check the other instances of Word for this document ID? Links or explanation would be appreciated.

    PS: Im not talking about Linking files that you can do with Insert > Object > Create from File, and clicking "Link to File"


    • Edited by Luke415 Tuesday, January 26, 2016 4:16 PM
    Tuesday, January 26, 2016 2:08 PM

Answers

  • It's possible to check whether a file is open by checking its lock state (see https://support.microsoft.com/en-us/kb/209189 for a VBA implementation) but, if you're concerned the current user might have it open and want to be able to access it, you might do better to have your code check whether Word is already running before creating a new instance and only create a new one if Word is not already running. That way, if the other document is already opened by the user, you'd be able to access it in the current instance (see https://support.microsoft.com/en-au/kb/212714 for a VBA test if a document id open in the current instance).

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by David_JunFeng Friday, February 5, 2016 9:09 AM
    Wednesday, January 27, 2016 1:01 AM
  • Hi, Luke415

    According to your description, If I don't misunderstand, I suggest that you could use Marshal.GetActiveObject Method to obtain a running instance of a COM object without a registered ProgID, refer to below code:

    using System;
    using System.Runtime.InteropServices;
    
    class MainFunction
    {
        static void Main()
            {
            Console.WriteLine("\nSample: C# System.Runtime.InteropServices.Marshal.GetActiveObject.cs\n"); 
    
            GetObj(1, "Word.Application");
            GetObj(2, "Excel.Application");
            }
    
        static void GetObj(int i, String progID)
     {
            Object obj = null;
    
            Console.WriteLine("\n" +i+") Object obj = GetActiveObject(\"" + progID + "\")");
            try
               { obj = Marshal.GetActiveObject(progID); }
            catch (Exception e)
               {
               Write2Console("\n   Failure: obj did not get initialized\n" + 
                             "   Exception = " +e.ToString().Substring(0,43), 0); 
               }
    
            if (obj != null)
               { Write2Console("\n   Success: obj = " + obj.ToString(), 1 ); }
     }
    
    
        static void Write2Console(String s, int color)
            {
            Console.ForegroundColor = color == 1? ConsoleColor.Green : ConsoleColor.Red;
            Console.WriteLine(s); 
            Console.ForegroundColor = ConsoleColor.Gray;
     }
    }

    For more information, click here to refer about Marshal.GetActiveObject Method


    Wednesday, January 27, 2016 7:46 AM

All replies

  • It's possible to check whether a file is open by checking its lock state (see https://support.microsoft.com/en-us/kb/209189 for a VBA implementation) but, if you're concerned the current user might have it open and want to be able to access it, you might do better to have your code check whether Word is already running before creating a new instance and only create a new one if Word is not already running. That way, if the other document is already opened by the user, you'd be able to access it in the current instance (see https://support.microsoft.com/en-au/kb/212714 for a VBA test if a document id open in the current instance).

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by David_JunFeng Friday, February 5, 2016 9:09 AM
    Wednesday, January 27, 2016 1:01 AM
  • Hi, Luke415

    According to your description, If I don't misunderstand, I suggest that you could use Marshal.GetActiveObject Method to obtain a running instance of a COM object without a registered ProgID, refer to below code:

    using System;
    using System.Runtime.InteropServices;
    
    class MainFunction
    {
        static void Main()
            {
            Console.WriteLine("\nSample: C# System.Runtime.InteropServices.Marshal.GetActiveObject.cs\n"); 
    
            GetObj(1, "Word.Application");
            GetObj(2, "Excel.Application");
            }
    
        static void GetObj(int i, String progID)
     {
            Object obj = null;
    
            Console.WriteLine("\n" +i+") Object obj = GetActiveObject(\"" + progID + "\")");
            try
               { obj = Marshal.GetActiveObject(progID); }
            catch (Exception e)
               {
               Write2Console("\n   Failure: obj did not get initialized\n" + 
                             "   Exception = " +e.ToString().Substring(0,43), 0); 
               }
    
            if (obj != null)
               { Write2Console("\n   Success: obj = " + obj.ToString(), 1 ); }
     }
    
    
        static void Write2Console(String s, int color)
            {
            Console.ForegroundColor = color == 1? ConsoleColor.Green : ConsoleColor.Red;
            Console.WriteLine(s); 
            Console.ForegroundColor = ConsoleColor.Gray;
     }
    }

    For more information, click here to refer about Marshal.GetActiveObject Method


    Wednesday, January 27, 2016 7:46 AM