/// <summary> /// Get the captions of all the open word documents /// </summary> /// <returns></returns> public static List <string> GetWordDocuments() { List <string> documents = new List <string>(); try { using (IWordApplication wordApplication = COMWrapper.GetInstance <IWordApplication>()) { if (wordApplication != null) { if (version == null) { version = wordApplication.Version; } for (int i = 1; i <= wordApplication.Documents.Count; i++) { IWordDocument document = wordApplication.Documents.item(i); if (document.ReadOnly) { continue; } if (isAfter2003()) { if (document.Final) { continue; } } documents.Add(document.ActiveWindow.Caption); } } } } catch (Exception ex) { LOG.Warn("Problem retrieving word destinations, ignoring: ", ex); } return(documents); }
/// <summary> /// Call this to get the running word application, or create a new instance /// </summary> /// <returns>IWordApplication</returns> private static IWordApplication GetOrCreateWordApplication() { IWordApplication wordApplication = COMWrapper.GetOrCreateInstance <IWordApplication>(); InitializeVariables(wordApplication); return(wordApplication); }
/// <summary> /// Sets up the system template document so that Word can pick it up. /// </summary> private void SetupSystemTemplate() { IWordApplication app = this.UnityContainer.Resolve <IWordApplication>(); string installedPath = Path.Combine(Utilities.ReadExecutingDirectory(), Constants.SystemTemplateName); string roamingProfileDir = app.UserTemplatesPath; string roamingProfilePath = Path.Combine(roamingProfileDir, Constants.SystemTemplateName); if (this.File.Exists(installedPath)) { if (this.File.Exists(roamingProfilePath)) { if (this.File.GetLastWriteTimeUtc(installedPath) > this.File.GetLastWriteTimeUtc(roamingProfilePath)) { Nullable <DateTime> ignoreUpgrade = this.Settings.IgnoreSystemTemplateUpgradeFor; if (!ignoreUpgrade.HasValue || (ignoreUpgrade.HasValue && ignoreUpgrade.Value != this.File.GetLastWriteTimeUtc(installedPath))) { if (this.TeamRibbonPresenter.AskYesNoQuestion(FlowControllerResources.UpgradeSystemTemplateQuestion)) { this.CopyTemplateToRoamingProfile(installedPath, roamingProfilePath); } else { this.Settings.IgnoreSystemTemplateUpgradeFor = this.File.GetLastWriteTimeUtc(installedPath); } } } } else { this.CopyTemplateToRoamingProfile(installedPath, roamingProfilePath); } } }
/// <summary> /// Initializes a new instance of the <see cref="TeamProjectDocumentManager"/> class. /// </summary> /// <param name="container">The Unity container to be used to create <see cref="ITeamProjectDocument"/> objects.</param> /// <param name="application">The underlying Word application.</param> /// <param name="settings">The settings to be used.</param> /// <param name="logger">The object to be used for logging.</param> public TeamProjectDocumentManager(IUnityContainer container, IWordApplication application, ISettings settings, ILogger logger) { if (container == null) { throw new ArgumentNullException("container"); } if (application == null) { throw new ArgumentNullException("application"); } if (settings == null) { throw new ArgumentNullException("settings"); } if (logger == null) { throw new ArgumentNullException("logger"); } this.container = container; this.application = application; this.settings = settings; this.logger = logger; }
public SaveDocumentDirectoryDocumentsCommandHandler(IDatabase database, IWordApplication wordApplication) { Prevent.ParameterNull(database, nameof(database)); Prevent.ParameterNull(wordApplication, nameof(wordApplication)); _database = database; _wordApplication = wordApplication; }
/// <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; } }
// TODO: Not happy passing a word app or doc to this constructor /// <summary> /// Initializes a new instance of the <see cref="TeamProjectTemplate"/> class. /// </summary> /// <param name="logger">The logger used for logging.</param> /// <param name="wordTemplate">The Word template that underlies the team project template.</param> /// <param name="application">The Word application.</param> public TeamProjectTemplate(ILogger logger, IWordTemplate wordTemplate, IWordApplication application) { if (logger == null) { throw new ArgumentNullException("logger"); } this.logger = logger; this.wordTemplate = wordTemplate; this.application = application; this.logger.Log(TraceEventType.Verbose, "Constructing template"); }
private static void InsertIntoNewDocument(IWordApplication wordApplication, string tmpFile) { LOG.Debug("No Document, creating a new Document"); // Create new Document object template = string.Empty; object newTemplate = false; object documentType = 0; object documentVisible = true; wordApplication.Documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible); // Add Picture AddPictureToSelection(wordApplication.Selection, tmpFile); }
/// <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) { // Make sure the selected document is active, otherwise the insert will be made in a different document! try { wordApplication.Activate(); } catch { } if (wordApplication.Selection != null) { // Add Picture using (IInlineShape shape = AddPictureToSelection(wordApplication.Selection, tmpFile)) { if (!string.IsNullOrEmpty(address)) { object screentip = Type.Missing; if (!string.IsNullOrEmpty(tooltip)) { screentip = tooltip; } try { wordDocument.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.ActiveWindow.ActivePane.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 {} try { wordDocument.ActiveWindow.Activate(); } catch {} return(true); } return(false); }
/// <summary> /// Initialize static outlook variables like version and currentuser /// </summary> /// <param name="wordApplication"></param> private static void InitializeVariables(IWordApplication wordApplication) { if (wordApplication == null || wordVersion != null) { return; } try { wordVersion = new Version(wordApplication.Version); LOG.InfoFormat("Using Word {0}", wordVersion); } catch (Exception exVersion) { LOG.Error(exVersion); LOG.Warn("Assuming Word version 1997."); wordVersion = new Version((int)OfficeVersion.OFFICE_97, 0, 0, 0); } }
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> /// Initialises the flow controller. /// </summary> public void Initialise() { this.Logger.Log(TraceEventType.Information, "Flow controller initialising"); this.TeamRibbonPresenter.Initialise(this.HandleDocumentRebind); this.TeamRibbonPresenter.Import += new System.EventHandler(this.HandleTeamRibbonImport); this.TeamRibbonPresenter.Refresh += new System.EventHandler(this.HandleTeamRibbonRefresh); this.TeamRibbonPresenter.ShowLayoutDesigner += new EventHandler(this.HandleShowLayoutDesigner); this.TeamRibbonPresenter.HideLayoutDesigner += new EventHandler(this.HandleHideLayoutDesigner); this.SetupLayoutDesignerPresenterEvents(); this.SetupSystemTemplate(); IWordApplication app = this.UnityContainer.Resolve <IWordApplication>(); app.Initialise(); this.Manager.Initialise(); this.TeamRibbonPresenter.LoadState(); }
/// <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 = COMWrapper.GetInstance <IWordApplication>()) { if (wordApplication != null) { for (int i = 1; i <= wordApplication.Documents.Count; i++) { IWordDocument wordDocument = wordApplication.Documents.item(i); if (wordDocument.ActiveWindow.Caption.StartsWith(wordCaption)) { return(InsertIntoExistingDocument(wordDocument, tmpFile)); } } } } return(false); }
/// <summary> /// Initialize static outlook variables like version and currentuser /// </summary> /// <param name="wordApplication"></param> private static void InitializeVariables(IWordApplication wordApplication) { if (wordApplication == null || _wordVersion != null) { return; } try { _wordVersion = new Version(wordApplication.Version); Log.Info().WriteLine("Using Word {0}", _wordVersion); } catch (Exception exVersion) { Log.Error().WriteLine(exVersion); Log.Warn().WriteLine("Assuming Word version 1997."); _wordVersion = new Version((int)OfficeVersions.Office97, 0, 0, 0); } }
public static void InsertIntoNewDocument(string tmpFile) { using (IWordApplication wordApplication = COMWrapper.GetOrCreateInstance <IWordApplication>()) { if (wordApplication != null) { wordApplication.Visible = true; wordApplication.Activate(); // Create new Document object template = string.Empty; object newTemplate = false; object documentType = 0; object documentVisible = true; IWordDocument wordDocument = wordApplication.Documents.Add(ref template, ref newTemplate, ref documentType, ref documentVisible); // Add Picture AddPictureToSelection(wordApplication.Selection, tmpFile); wordDocument.Activate(); wordDocument.ActiveWindow.Activate(); } } }
/// <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); }
/// <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); }
public static void ExportToWord(string tmpFile) { using (IWordApplication wordApplication = WordApplication()) { if (wordApplication != null) { wordApplication.Visible = true; LOG.DebugFormat("Open Documents: {0}", wordApplication.Documents.Count); if (wordApplication.Documents.Count > 0) { if (wordApplication.Selection != null) { LOG.Debug("Selection found!"); AddPictureToSelection(wordApplication.Selection, tmpFile); return; } } else { InsertIntoNewDocument(wordApplication, tmpFile); return; } } } }
/// <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); } }
/// <summary> /// Internal method for the insert /// </summary> /// <param name="wordApplication"></param> /// <param name="wordDocument"></param> /// <param name="tmpFile"></param> /// <param name="address">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 { // ignored } using (var selection = wordApplication.Selection) { if (selection == null) { Log.Info().WriteLine("No selection to insert {0} into found.", tmpFile); return(false); } // Add Picture using (var shape = AddPictureToSelection(selection, tmpFile)) { if (!string.IsNullOrEmpty(address)) { var screentip = Type.Missing; if (!string.IsNullOrEmpty(tooltip)) { screentip = tooltip; } try { using (var hyperlinks = wordDocument.Hyperlinks) { hyperlinks.Add(shape, screentip, Type.Missing, screentip, Type.Missing, Type.Missing); } } catch (Exception e) { Log.Warn().WriteLine("Couldn't add hyperlink for image: {0}", e.Message); } } } try { using (var activeWindow = wordDocument.ActiveWindow) { activeWindow.Activate(); using (var activePane = activeWindow.ActivePane) { using (var view = activePane.View) { view.Zoom.Percentage = 100; } } } } catch (Exception e) { Log.Warn().WriteLine("Couldn't set zoom to 100, error: {0}", e.InnerException?.Message ?? e.Message); } try { wordApplication.Activate(); } catch { // ignored } try { using (var activeWindow = wordDocument.ActiveWindow) { activeWindow.Activate(); var hWnd = activeWindow.Hwnd; if (hWnd > 0) { // TODO: Await? InteropWindowFactory.CreateFor(new IntPtr(hWnd)).ToForegroundAsync(); } } } catch { // ignored } return(true); } }
public WordsController(IWordApplication wordApplication) { this.wordApplication = wordApplication; }
public ConvertService() { this._logger = new Logger(); this._word = new WordApplication(this._logger); this._word.SetDocumentSize(DocumentWidth, DocumentHeight); }