Esempio n. 1
0
 public void CloseDoc(string docUri)
 {
     //add code here to close the specific doc/app instance with docUri and notify the group of the closure
     WordFactory.CloseDocument(docUri);
     Clients.All.docClosed();
     Console.WriteLine($"Doc {docUri} was closed");
 }
Esempio n. 2
0
        public void OpenDoc(string docUri, string docId)
        {
            string docName = WordFactory.OpenDocument(docUri, docId);

            Clients.Caller.addMessage("server_" + Context.ConnectionId, "Word opened doc " + docName);
            Console.WriteLine($"Doc {docName} was opened");
        }
        public static void CloseWord()
        {
            string result = "Word already Closed";

            if (WordApp != null)
            {
                try
                {
                    foreach (Document doc in WordApp.Documents)
                    {
                        doc.Activate();
                        WordApp.Activate();
                        WordFactory.CloseDocument(doc.FullName);
                    }

                    //WordApp.Quit(WdSaveOptions.wdDoNotSaveChanges);
                    result = "Word Closed";
                }
                catch (System.Runtime.InteropServices.COMException ex)
                {
                    switch (ex.HResult)
                    {
                    case -2147417846:      //Dialog box is open and blocking us
                        WordApp.Activate();
                        context.Clients.All.addMessage("CloseWord", "Can't close Word, please check for open dialog box");
                        return;

                    case -2147023174:      //Word Instance died without us knowing, need to set back to null to recover
                        context.Clients.All.addMessage("CloseWord", "Word Failed, attempting to recover...");
                        break;

                    default:      //this is to catch the unknown and bubble up the details
                        context.Clients.All.addMessage("CloseWord", $"Oops... Something went wrong  Code {ex.HResult}  Message: {ex.Message}");
                        return;
                    }
                }
            }

            WordApp = null;
            context.Clients.All.addMessage("CloseWord", result);
            Console.WriteLine($"CloseWord: {result}");
        }
Esempio n. 4
0
 public void FocusDocApp(string docUri)
 {
     WordFactory.FocusDocumentApp(docUri);
 }
Esempio n. 5
0
        public void FocusDocPInoke(string docUri)
        {
            bool result = WordFactory.FocusDocumentPinvoke(docUri);

            Clients.All.addMessage($"{nameof(FocusDocPInoke)} returned with {result.ToString()}");
        }
Esempio n. 6
0
 public void CloseWord()
 {
     WordFactory.CloseWord();
     Clients.All.docClosed();
     Console.WriteLine($"Connection: {Context.ConnectionId} Word was closed");
 }
        public static string OpenDocument(string docUri, string docId)
        {
            Document doc = null;

            try
            {
                if (WordApp == null)
                {
                    WordFactory.CreateInstance();
                }

                if (string.IsNullOrEmpty(docUri))
                {
                    doc = WordApp.Documents.Add();
                }
                else
                {
                    doc = WordApp.Documents.Open(docUri);

                    string windowCaption = doc.ActiveWindow.Caption;
                    Console.WriteLine(windowCaption);

                    IntPtr docHandle = FindWindow(null, windowCaption + " - Word");

                    ShowWindowAsync(new HandleRef(null, docHandle), 9); //need if the window is minimized

                    bool result = SetForegroundWindow(docHandle);


                    DocumentEvents2_Event events = (DocumentEvents2_Event)doc;

                    //example of how to use lambda expressions to create an anonymous function for event handler
                    events.Close += () =>
                    {
                        context.Clients.All.addMessage("CloseDocument-Lambda", doc.FullName + " was closed with DocId:" + docId);
                        Console.WriteLine("CloseDocument-Lambda: Closed Document " + doc.FullName);
                    };
                }

                WordApp.Activate();

                Console.WriteLine("Doc Opened with Doc.Name=" + doc.FullName);
                return(doc.FullName);
            }
            catch (COMException ex)
            {
                string msg = string.Empty;

                switch (ex.HResult)
                {
                case -2146824128:
                    msg = $"Can't open document {docUri}: Please check to see if dialog window is open";
                    break;

                default:
                    msg = ex.HResult + ":" + ex.Message;
                    break;
                }
                Console.WriteLine(msg);
                context.Clients.All.addMessage(msg);

                return(string.Empty);
            }
        }