static void Main(string[] args) { Word.ApplicationClass MyWord = new Word.ApplicationClass(); MyWord.Visible = true; System.Windows.Forms.Application.Run(); }
/// <summary> /// Preactivation /// It's usefull, if you need more speed in the main Program /// so you can preload Word. /// </summary> public void PreActivate() { if (wAppC == null) { wAppC = new Word.ApplicationClass(); } }
/// <summary> /// Sets all internal state back to null /// </summary> private void ResetState() { m_wordApp = null; m_wordDoc = null; m_originalFileName = null; m_newFileName = null; m_newFormatType = null; m_handleTrackedChanges = false; m_statusFileName = null; }
/// <summary> /// Creates the Word Application Class and sets the default web options /// (UTF-8, optimize for browser, etc.) /// </summary> private void CreateWordAppClass() { m_wordApp = new Word.ApplicationClass(); m_wordApp.Visible = false; m_wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; m_wordApp.DefaultWebOptions().Encoding = Office.MsoEncoding.msoEncodingUTF8; m_wordApp.DefaultWebOptions().AlwaysSaveInDefaultEncoding = true; m_wordApp.DefaultWebOptions().BrowserLevel = Word.WdBrowserLevel.wdBrowserLevelMicrosoftInternetExplorer5; m_wordApp.DefaultWebOptions().OptimizeForBrowser = false; }
/// <summary> /// Exports the argument to Microsoft Word using COM /// </summary> /// <param name="a"></param> public void outputToWord(Argument a) { Node n; DrawTree d; Bitmap b; arg = a; Document aDoc = null; WordApp = new Word.ApplicationClass(); object missing = System.Reflection.Missing.Value; object fileName = "normal.dot"; // template file name object newTemplate = false; object docType = 0; object isVisible = true; // Create a new Document, by calling the Add function in the Documents collection try { aDoc = WordApp.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("Cannot create new document in MS Word.\n" + e.Message); } WordApp.Visible = true; aDoc.Activate(); WordApp.Selection.Font.Bold = (int)Word.WdConstants.wdToggle; WordApp.Selection.TypeText("Argument"); WordApp.Selection.Font.Bold = (int)Word.WdConstants.wdToggle; WordApp.Selection.TypeParagraph(); n = arg.findHead(); d = new DrawTree(0, 0, 1F); // no offset or zoom b = d.drawTree(n); System.Windows.Forms.Clipboard.SetDataObject(b); WordApp.Selection.Paste(); WordApp.Selection.TypeParagraph(); writeWordDoc(); // release the COM object. GC.Collect(); would be overkill System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp); }
private void CreateWordFile(string strFilePath) { //Creating the instance of Word Application Word.Application newApp = new Word.ApplicationClass(); object Source = strFilePath; // specifying the Source & Target file names string strNewPath = strFilePath.Replace(".html", ".rtf"); object Target = strNewPath; // Use for the parameter whose type are not known or // say Missing object Unknown = Type.Missing; Word.Document objDoc; // Source document open here // Additional Parameters are not known so that are // set as a missing type objDoc = newApp.Documents.Open(ref Source, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown); // Specifying the format in which you want the output file //object format = Word.WdSaveFormat.wdFormatDocument; object format = Word.WdSaveFormat.wdFormatRTF; objDoc.Activate(); foreach (Paragraph rPrg in newApp.ActiveDocument.Paragraphs) { rPrg.Range.Font.Name = "VNI-Times"; rPrg.Range.Font.Size = 14; } newApp.Selection.TypeParagraph(); newApp.Selection.Font.Name = "VNI-Times"; //Changing the format of the document newApp.ActiveDocument.SaveAs(ref Target, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown); newApp.ActiveDocument.Save(); // for closing the application newApp.Quit(ref Unknown, ref Unknown, ref Unknown); }
static void Main(string[] args) { Office.CommandBarButton Button; Office.CommandBar CommandBar; object Missing = System.Reflection.Missing.Value; Office._CommandBarButtonEvents_ClickEventHandler ButtonHandler; Word.ApplicationClass MyWord = new Word.ApplicationClass(); MyWord.Visible = true; CommandBar = MyWord.CommandBars.Add("MyCommandBar", Missing, Missing, Missing); Button = (Office.CommandBarButton)CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, Missing, Missing, Missing, Missing); Button.Caption = "MyButton"; Button.FaceId = 1845; ButtonHandler = new Office._CommandBarButtonEvents_ClickEventHandler(OnClick_Button); Button.Click += ButtonHandler; System.Windows.Forms.Application.Run(); }
static void Main(string[] args) { // Find the full path of our document System.IO.FileInfo ExecutableFileInfo = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location); object docFileName = System.IO.Path.Combine(ExecutableFileInfo.DirectoryName, "document.doc"); // Create the needed Word.Application and Word.Document objects object nullObject = System.Reflection.Missing.Value; Word.Application application = new Word.ApplicationClass(); Word.Document document = application.Documents.Open(ref docFileName, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject); string wholeTextContent = document.Content.Text; wholeTextContent = wholeTextContent.Replace('\r', ' '); // Delete lines between paragraphs string[] splittedTextContent = wholeTextContent.Split(' '); // Get the separate words int index = 1; foreach (string singleWord in splittedTextContent) { if (singleWord.Trim().Length > 0) // We don´t need to store white spaces { Console.WriteLine("Word: " + singleWord + "(position: " + index.ToString() + ")"); index++; } } // Dispose Word.Application and Word.Document objects resources document.Close(ref nullObject, ref nullObject, ref nullObject); application.Quit(ref nullObject, ref nullObject, ref nullObject); document = null; application = null; Console.ReadLine(); }
static void Main(string[] args) { object Missing = Missing.Value; object BuiltInProps; object CustomProps; Word._Document Doc; Word.ApplicationClass MyWord = new Word.ApplicationClass(); MyWord.Visible = true; Doc = MyWord.Documents.Add(ref Missing, ref Missing, ref Missing, ref Missing); BuiltInProps = Doc.BuiltInDocumentProperties; Type TypeBuiltingProp = BuiltInProps.GetType(); //Setting abuilt-in property string Prop = "Author"; string PropValue; object AuthorProp = TypeBuiltingProp.InvokeMember("item", BindingFlags.Default | BindingFlags.GetProperty, null, BuiltInProps, new Object[] { Prop }); Type TypeAuthorProp = AuthorProp.GetType(); PropValue = TypeAuthorProp.InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, AuthorProp, new Object[] {}).ToString(); System.Windows.Forms.Application.Run(); }
public void LoadDocument(string t_filename, string ID, string TableName, bool ManualOpen) { deactivateevents = true; FileID = ID; this.TableName = TableName; filename = t_filename; try { if (wAppC == null) { wAppC = new Word.ApplicationClass(); } try { wAppC.CommandBars.AdaptiveMenus = false; wAppC.DocumentBeforeClose += new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(OnClose); wAppC.NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc); wAppC.DocumentOpen += new Word.ApplicationEvents2_DocumentOpenEventHandler(OnOpenDoc); wAppC.DocumentBeforeSave += new Word.ApplicationEvents2_DocumentBeforeSaveEventHandler(OnBeforSaveDoc); wAppC.ApplicationEvents2_Event_Quit += new Word.ApplicationEvents2_QuitEventHandler(OnQuit); } catch { } if (wDoc != null) { try { object dummy = null; wAppC.Documents.Close(ref dummy, ref dummy, ref dummy); wordWnd = 0; } catch { } } if (wordWnd == 0) { wordWnd = FindWindow("Opusapp", null); } if (wordWnd != 0) { if (!ManualOpen) { SetParent(wordWnd, this.Handle.ToInt32()); } object fileName = filename; object newTemplate = false; object docType = 0; object readOnly = true; object isVisible = true; object missing = System.Reflection.Missing.Value; try { if (wAppC == null) { throw new WordInstanceException(); } if (wAppC.Documents == null) { throw new DocumentInstanceException(); } if (wAppC != null && wAppC.Documents != null) { //document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible); object file = fileName; //this is the path object nullobject = System.Reflection.Missing.Value; wDoc = wAppC.Documents.Open(ref file, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject); //MessageBox.Show(document.Path + document.FullName); } if (wDoc == null) { throw new ValidDocumentException(); } } catch { } try { wAppC.ActiveWindow.DisplayRightRuler = false; wAppC.ActiveWindow.DisplayScreenTips = false; wAppC.ActiveWindow.DisplayVerticalRuler = false; wAppC.ActiveWindow.DisplayRightRuler = false; wAppC.ActiveWindow.ActivePane.DisplayRulers = false; wAppC.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView; //wd.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;//wdWebView; // .wdNormalView; } catch { } /// Code Added /// Disable the specific buttons of the command bar /// By default, we disable/hide the menu bar /// The New/Open buttons of the command bar are disabled /// Other things can be added as required (and supported ..:) ) /// Lots of commented code in here, if somebody needs to disable specific menu or sub-menu items. /// int counter = wAppC.ActiveWindow.Application.CommandBars.Count; for (int i = 1; i <= counter; i++) { try { String nm = wAppC.ActiveWindow.Application.CommandBars[i].Name; /// MessageBox.Show("The menu:" + nm); ////int count_control1 = wd.ActiveWindow.Application.CommandBars[i].Controls.Count; ////for (int j = 1; j <= count_control1; j++) //////for (int j = 1; j <= count_control; j++) ////{ //// try //// { //// //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption); //// //wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = false; //// } //// catch (Exception ex) { } ////} if (nm == "Standard") { //nm=i.ToString()+" "+nm; //MessageBox.Show(nm); int count_control = wAppC.ActiveWindow.Application.CommandBars[i].Controls.Count; for (int j = 1; j <= 2; j++) //for (int j = 1; j <= count_control; j++) { try { //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption); wAppC.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = false; } catch (Exception ex) { } } } if (nm == "Menu Bar") { //To disable the menubar, use the following (1) line wAppC.ActiveWindow.Application.CommandBars[i].Enabled = false; /// If you want to have specific menu or sub-menu items, write the code here. /// Samples commented below // MessageBox.Show(nm); //int count_control=wd.ActiveWindow.Application.CommandBars[i].Controls.Count; //MessageBox.Show(count_control.ToString()); /* * //for(int j=1;j<=count_control;j++) * for(int j=1;j<=count_control-1;j++) * { * /// The following can be used to disable specific menuitems in the menubar * //wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled=false; * wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption = "سلام"; * wd.ActiveWindow.Application.CommandBars[i].Controls[j].Delete((object)true); * * ////MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].ToString()); * ////MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption); * ////MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].accChildCount.ToString()); * * * ///The following can be used to disable some or all the sub-menuitems in the menubar * * * Office.CommandBarPopup c; * c = (Office.CommandBarPopup)wd.ActiveWindow.Application.CommandBars[i].Controls[j]; * * for (int k = 1; k <= c.Controls.Count; k++) * { * //MessageBox.Show(k.ToString()+" "+c.Controls[k].Caption + " -- " + c.Controls[k].DescriptionText + " -- " ); * try * { * c.Controls[k].Enabled = false; * // c.Controls["Close Window"].Enabled = false; * } * catch * { * * } * } * * * * //wd.ActiveWindow.Application.CommandBars[i].Controls[j].Control Controls[0].Enabled=false; * } */ } nm = ""; } catch (Exception ex) { ErrorManager.WriteMessage("LoadDocument,t_filename", ex.ToString(), this.ParentForm.Text); MessageBox.Show(ex.ToString()); } } // Show the word-document try { wAppC.Visible = true; if (ManualOpen) { wAppC.Activate(); } if (!ManualOpen) { SetWindowPos(wordWnd, this.Handle.ToInt32(), 0, 0, this.Bounds.Width, this.Bounds.Height, SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME | SWP_NOSIZE); } //Call onresize--I dont want to write the same lines twice OnResize(); } catch { MessageBox.Show("Error: do not load the document into the control until the parent window is shown!"); } /// We want to remove the system menu also. The title bar is not visible, but we want to avoid accidental minimize, maximize, etc ..by disabling the system menu(Alt+Space) try { int hMenu = GetSystemMenu(wordWnd, false); if (hMenu > 0) { int menuItemCount = GetMenuItemCount(hMenu); RemoveMenu(hMenu, menuItemCount - 1, MF_REMOVE | MF_BYPOSITION); RemoveMenu(hMenu, menuItemCount - 2, MF_REMOVE | MF_BYPOSITION); RemoveMenu(hMenu, menuItemCount - 3, MF_REMOVE | MF_BYPOSITION); RemoveMenu(hMenu, menuItemCount - 4, MF_REMOVE | MF_BYPOSITION); RemoveMenu(hMenu, menuItemCount - 5, MF_REMOVE | MF_BYPOSITION); RemoveMenu(hMenu, menuItemCount - 6, MF_REMOVE | MF_BYPOSITION); RemoveMenu(hMenu, menuItemCount - 7, MF_REMOVE | MF_BYPOSITION); RemoveMenu(hMenu, menuItemCount - 8, MF_REMOVE | MF_BYPOSITION); DrawMenuBar(wordWnd); } } catch { }; if (!ManualOpen) { this.Parent.Focus(); } } deactivateevents = false; } catch (Exception ex) { deactivateevents = false; ID = string.Empty; ErrorManager.WriteMessage("LoadDocument,t_filename2", ex.ToString(), this.ParentForm.Text); } }
public WordHelp(Word.ApplicationClass wordapp) { oWordApplic = wordapp; }
public WordHelp() { // activate the interface with the COM object of Microsoft Word oWordApplic = new Word.ApplicationClass(); }