Ejemplo n.º 1
0
        /// <summary>
        /// Get the captions of all the open word documents
        /// </summary>
        /// <returns></returns>
        public static List <string> GetWordDocuments()
        {
            List <string> openDocuments = new List <string>();

            try {
                using (IWordApplication wordApplication = GetWordApplication()) {
                    if (wordApplication == null)
                    {
                        return(openDocuments);
                    }
                    using (IDocuments documents = wordApplication.Documents) {
                        for (int i = 1; i <= documents.Count; i++)
                        {
                            using (IWordDocument document = documents.item(i)) {
                                if (document.ReadOnly)
                                {
                                    continue;
                                }
                                if (isAfter2003())
                                {
                                    if (document.Final)
                                    {
                                        continue;
                                    }
                                }
                                using (IWordWindow activeWindow = document.ActiveWindow) {
                                    openDocuments.Add(activeWindow.Caption);
                                }
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                LOG.Warn("Problem retrieving word destinations, ignoring: ", ex);
            }
            openDocuments.Sort();
            return(openDocuments);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Insert the bitmap stored under the tempfile path into the word document with the supplied caption
 /// </summary>
 /// <param name="wordCaption"></param>
 /// <param name="tmpFile"></param>
 /// <returns></returns>
 public static bool InsertIntoExistingDocument(string wordCaption, string tmpFile)
 {
     using (IWordApplication wordApplication = GetWordApplication()) {
         if (wordApplication == null)
         {
             return(false);
         }
         using (IDocuments documents = wordApplication.Documents) {
             for (int i = 1; i <= documents.Count; i++)
             {
                 using (IWordDocument wordDocument = documents.item(i)) {
                     using (IWordWindow activeWindow = wordDocument.ActiveWindow) {
                         if (activeWindow.Caption.StartsWith(wordCaption))
                         {
                             return(InsertIntoExistingDocument(wordApplication, wordDocument, tmpFile, null, null));
                         }
                     }
                 }
             }
         }
     }
     return(false);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Internal method for the insert
 /// </summary>
 /// <param name="wordApplication"></param>
 /// <param name="wordDocument"></param>
 /// <param name="tmpFile"></param>
 /// <param name="adress">link for the image</param>
 /// <param name="tooltip">tooltip of the image</param>
 /// <returns></returns>
 internal static bool InsertIntoExistingDocument(IWordApplication wordApplication, IWordDocument wordDocument, string tmpFile, string address, string tooltip)
 {
     // Bug #1517: image will be inserted into that document, where the focus was last. It will not inserted into the chosen one.
     // Solution: Make sure the selected document is active, otherwise the insert will be made in a different document!
     try {
         wordDocument.Activate();
     } catch {
     }
     using (ISelection selection = wordApplication.Selection) {
         if (selection == null)
         {
             LOG.InfoFormat("No selection to insert {0} into found.", tmpFile);
             return(false);
         }
         // Add Picture
         using (IInlineShape shape = AddPictureToSelection(selection, tmpFile)) {
             if (!string.IsNullOrEmpty(address))
             {
                 object screentip = Type.Missing;
                 if (!string.IsNullOrEmpty(tooltip))
                 {
                     screentip = tooltip;
                 }
                 try {
                     using (IHyperlinks hyperlinks = wordDocument.Hyperlinks) {
                         hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing);
                     }
                 } catch (Exception e) {
                     LOG.WarnFormat("Couldn't add hyperlink for image: {0}", e.Message);
                 }
             }
         }
         try {
             using (IWordWindow activeWindow = wordDocument.ActiveWindow) {
                 activeWindow.Activate();
                 using (IPane activePane = activeWindow.ActivePane) {
                     using (IWordView view = activePane.View) {
                         view.Zoom.Percentage = 100;
                     }
                 }
             }
         } catch (Exception e) {
             if (e.InnerException != null)
             {
                 LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.InnerException.Message);
             }
             else
             {
                 LOG.WarnFormat("Couldn't set zoom to 100, error: {0}", e.Message);
             }
         }
         try {
             wordApplication.Activate();
         } catch {
         }
         try {
             wordDocument.Activate();
         } catch {
         }
         return(true);
     }
 }
Ejemplo n.º 4
0
 static extern int AccessibleObjectFromWindow(int hwnd, uint dwObjectID, byte[] riid, out IWordWindow ptr);