public static void InsertIntoNewDocument(string tmpFile, string address, string tooltip) { using (IWordApplication wordApplication = GetOrCreateWordApplication()) { if (wordApplication == null) { return; } wordApplication.Visible = true; wordApplication.Activate(); // Create new Document object template = string.Empty; object newTemplate = false; object documentType = 0; object documentVisible = true; using (IDocuments documents = wordApplication.Documents) { using (IWordDocument wordDocument = documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible)) { using (ISelection selection = wordApplication.Selection) { // 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 { wordDocument.Activate(); } catch { } try { wordDocument.ActiveWindow.Activate(); } catch { } } } } }
/// <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); } }