I have tried everything but I cannot figure this out. I need to insert text into an already open Word Document.
I am using C#. This is an add-in, so Word is already open. If I use the new Document method like all the examples show it brings up fresh document and won't work (obviously) . What is the correct syntax for inserting text using C# if the document is already open. Thanks.
Loading

Patrick Klitzke6 reputationPosted Jun 13, 2009, 1:38 AM EDT
you can simply use the function this.Application.Selection.TypeText("This is my new Text") to type a text. Here is a short listing for an Addin, that writes Hello every time a new document opens:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using Word = Microsoft.Office.Interop.Word; using Office = Microsoft.Office.Core; using Microsoft.Office.Tools.Word; using Microsoft.Office.Tools.Word.Extensions; using Microsoft.Office.Interop.Word; namespace WordAddIn1 { public partial class ThisAddIn { private void ThisAddIn_Startup(object sender, System.EventArgs e) { // use this method at any place to insert text this.Application.Selection.TypeText("Hello"); } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { } #region VSTO generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InternalStartup() { this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); } #endregion } }Cory3 reputationPosted Jun 15, 2009, 7:07 AM EDT
namespace SignatureAddin { using System; using System.Diagnostics; using System.Drawing; using System.IO; using System.Reflection; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.Xml; using System.Text; using System.Windows.Forms; using System.Xml; using Microsoft.Office.Tools.Word; using Microsoft.Office.Tools.Word.Extensions; using Extensibility; using Microsoft.Office.Core; using stdole; using XmlDataObject = System.Security.Cryptography.Xml.DataObject; using System.Security.Principal; using System.Net; #region Read me for Add-in installation and setup information. // When run, the Add-in wizard prepared the registry for the Add-in. // At a later time, if the Add-in becomes unavailable for reasons such as: // 1) You moved this project to a computer other than which is was originally created on. // 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in. // 3) Registry corruption. // you will need to re-register the Add-in by building the SignatureAddinSetup project, // right click the project in the Solution Explorer, then choose install. #endregion /// /// The object for implementing an Add-in. /// /// [ System.Runtime.InteropServices.ComVisible(true), System.Runtime.InteropServices.GuidAttribute("AD2CACB9-286D-43a8-BE75-7FB2A86A67A0"), System.Runtime.InteropServices.ProgId("SignatureAddin.Connect") ] public class AxHost2 : AxHost { public AxHost2() : base(null) { } public new static IPictureDisp GetIPictureDispFromPicture(Image image) { return (IPictureDisp)AxHost.GetIPictureDispFromPicture(image); } } public class Connect : Object, Extensibility.IDTExtensibility2, SignatureProvider { private CommandBarButton MyButton; private object applicationObject; private object addInInstance; /// /// Implements the constructor for the Add-in object. /// Place your initialization code within this method. /// public Connect() { } public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { applicationObject = application; addInInstance = addInInst; if (connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup) { OnStartupComplete(ref custom); } } public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom) { if (disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown) { OnBeginShutdown(ref custom); } applicationObject = null; } public void OnAddInsUpdate(ref System.Array custom) { } public void OnStartupComplete(ref System.Array custom) { CommandBars oCommandBars; CommandBar oStandardBar; try { oCommandBars = (CommandBars)applicationObject.GetType().InvokeMember("CommandBars", BindingFlags.GetProperty, null, applicationObject, null); } catch (Exception) { // Outlook has the CommandBars collection on the Explorer object. object oActiveExplorer; oActiveExplorer = applicationObject.GetType().InvokeMember("ActiveExplorer", BindingFlags.GetProperty, null, applicationObject, null); oCommandBars = (CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars", BindingFlags.GetProperty, null, oActiveExplorer, null); } // Set up a custom button on the "Standard" commandbar. try { oStandardBar = oCommandBars["Standard"]; } catch (Exception) { // Access names its main toolbar Database. oStandardBar = oCommandBars["Database"]; } // In case the button was not deleted, use the exiting one. try { MyButton = (CommandBarButton)oStandardBar.Controls["My Custom Button"]; } catch (Exception) { object omissing = System.Reflection.Missing.Value; MyButton = (CommandBarButton)oStandardBar.Controls.Add(1, omissing, omissing, omissing, omissing); MyButton.Caption = "My Custom Button"; MyButton.Style = MsoButtonStyle.msoButtonCaption; } // The following items are optional, but recommended. //The Tag property lets you quickly find the control //and helps MSO keep track of it when more than //one application window is visible. The property is required //by some Office applications and should be provided. MyButton.Tag = "My Custom Button"; // The OnAction property is optional but recommended. //It should be set to the ProgID of the add-in, so that if //the add-in is not loaded when a user presses the button, //MSO loads the add-in automatically and then raises //the Click event for the add-in to handle. MyButton.OnAction = "!"; MyButton.Visible = true; MyButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click); object oName = applicationObject.GetType().InvokeMember("Name", BindingFlags.GetProperty, null, applicationObject, null); // Display a simple message to show which application you started in. // System.Windows.Forms.MessageBox.Show("This Addin is loaded by " + oName.ToString(), "MyCOMAddin"); oStandardBar = null; oCommandBars = null; } public void OnBeginShutdown(ref System.Array custom) { object omissing = System.Reflection.Missing.Value; //System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading."); MyButton.Delete(omissing); MyButton = null; } private void MyButton_Click(CommandBarButton cmdBarbutton, ref bool cancel) { } }