//From my answer here: http://stackoverflow.com/questions/8673726/get-specific-window-handle-using-office-interop/41462638#41462638 private static Process GetProcess(Word.Application app) { var tempDocument = app.Documents.Add(); var project = tempDocument.VBProject; var component = project.VBComponents.Add(NetOffice.VBIDEApi.Enums.vbext_ComponentType.vbext_ct_StdModule); var codeModule = component.CodeModule; codeModule.AddFromString("#If Win64 Then\r\n Declare PtrSafe Function GetCurrentProcessId Lib \"kernel32\" () As Long\r\n#Else\r\n Declare Function GetCurrentProcessId Lib \"kernel32\" () As Long\r\n#End If"); var result = app.Run("GetCurrentProcessId"); var process = Process.GetProcessById((int)result); tempDocument.Close(false); return(process); }
public void RunExample() { // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApplication.Visible = true; // create a utils instance, not need for but helpful to keep the lines of code low CommonUtils utils = new CommonUtils(wordApplication); // add a new document Word.Document newDocument = wordApplication.Documents.Add(); // add new module and insert macro // the option "Trust access to Visual Basic Project" must be set VB.CodeModule module = newDocument.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule).CodeModule; // set the modulename module.Name = "NetOfficeTestModule"; //add the macro string codeLines = string.Format("Public Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "Selection.TypeText (\"This text is written by a automatic created macro with NetOffice...\")"); module.InsertLines(1, codeLines); //start the macro NetOfficeTestModule wordApplication.Run("NetOfficeTestModule!NetOfficeTestMacro"); // save the document string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example05", DocumentFormat.Macros); newDocument.SaveAs(documentFile); if (utils.ApplicationIs2007OrHigher) { newDocument.SaveAs(documentFile, WdSaveFormat.wdFormatXMLDocumentMacroEnabled); } else { newDocument.SaveAs(documentFile); } //close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); // show end dialog HostApplication.ShowFinishDialog(null, documentFile); }
public void RunExample() { // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApplication.Visible = true; // add a new document Word.Document newDocument = wordApplication.Documents.Add(); // add new module and insert macro // the option "Trust access to Visual Basic Project" must be set VB.CodeModule module = newDocument.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule).CodeModule; // set the modulename module.Name = "NetOfficeTestModule"; //add the macro string codeLines = string.Format("Public Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "Selection.TypeText (\"This text is written by a automatic created macro with NetOffice...\")"); module.InsertLines(1, codeLines); //start the macro NetOfficeTestModule wordApplication.Run("NetOfficeTestModule!NetOfficeTestMacro"); string fileExtension = GetFileExtension(wordApplication); string documentFile = string.Format("{0}\\Example05{1}", _hostApplication.RootDirectory, fileExtension); double wordVersion = Convert.ToDouble(wordApplication.Version, CultureInfo.InvariantCulture); if (wordVersion >= 12.0) { newDocument.SaveAs(documentFile, WdSaveFormat.wdFormatXMLDocumentMacroEnabled); } else { newDocument.SaveAs(documentFile); } //close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); // show dialog for the user(you!) _hostApplication.ShowFinishDialog(null, documentFile); }
public TestResult DoTest() { Word.Application application = null; DateTime startTime = DateTime.Now; try { application = COMObject.Create <Word.Application>(COMObjectCreateOptions.CreateNewCore); application.DisplayAlerts = WdAlertLevel.wdAlertsNone; application.Visible = true; Word.Document newDocument = application.Documents.Add(); // add new module and insert macro // the option "Trust access to Visual Basic Project" must be set NetOffice.VBIDEApi.CodeModule module = newDocument.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule).CodeModule; // set the modulename module.Name = "NetOfficeTestModule"; //add the macro string codeLines = string.Format("Public Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "Selection.TypeText (\"This text is written by a automatic created macro with NetOffice...\")"); module.InsertLines(1, codeLines); //start the macro NetOfficeTestModule application.Run("NetOfficeTestModule!NetOfficeTestMacro"); newDocument.Close(false); return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, "")); } catch (Exception exception) { return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, "")); } finally { if (null != application) { application.Quit(WdSaveOptions.wdDoNotSaveChanges); application.Dispose(); } } }
public TestResult DoTest() { Word.Application application = null; DateTime startTime = DateTime.Now; try { application = new Word.Application(); application.DisplayAlerts = WdAlertLevel.wdAlertsNone; application.Visible = true; Word.Document newDocument = application.Documents.Add(); // add new module and insert macro // the option "Trust access to Visual Basic Project" must be set NetOffice.VBIDEApi.CodeModule module = newDocument.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule).CodeModule; // set the modulename module.Name = "NetOfficeTestModule"; //add the macro string codeLines = string.Format("Public Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "Selection.TypeText (\"This text is written by a automatic created macro with NetOffice...\")"); module.InsertLines(1, codeLines); //start the macro NetOfficeTestModule application.Run("NetOfficeTestModule!NetOfficeTestMacro"); newDocument.Close(false); return new TestResult(true, DateTime.Now.Subtract(startTime), "", null, ""); } catch (Exception exception) { return new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, ""); } finally { if (null != application) { application.Quit(WdSaveOptions.wdDoNotSaveChanges); application.Dispose(); } } }
public void RunExample() { // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApplication.Visible = true; // create a utils instance, not need for but helpful to keep the lines of code low CommonUtils utils = new CommonUtils(wordApplication); // add a new document Word.Document newDocument = wordApplication.Documents.Add(); // add new module and insert macro // the option "Trust access to Visual Basic Project" must be set VB.CodeModule module = newDocument.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule).CodeModule; // set the modulename module.Name = "NetOfficeTestModule"; //add the macro string codeLines = string.Format("Public Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "Selection.TypeText (\"This text is written by a automatic created macro with NetOffice...\")"); module.InsertLines(1, codeLines); //start the macro NetOfficeTestModule wordApplication.Run("NetOfficeTestModule!NetOfficeTestMacro"); // save the document string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example05", Word.Tools.DocumentFormat.Macros); newDocument.SaveAs(documentFile); if(utils.ApplicationIs2007OrHigher) newDocument.SaveAs(documentFile, WdSaveFormat.wdFormatXMLDocumentMacroEnabled); else newDocument.SaveAs(documentFile); //close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); // show dialog for the user(you!) HostApplication.ShowFinishDialog(null, documentFile); }
public void RunExample() { // start word and turn off msg boxes Word.Application wordApplication = new Word.Application(); wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApplication.Visible = true; // add a new document Word.Document newDocument = wordApplication.Documents.Add(); // add new module and insert macro // the option "Trust access to Visual Basic Project" must be set VB.CodeModule module = newDocument.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule).CodeModule; // set the modulename module.Name = "NetOfficeTestModule"; //add the macro string codeLines = string.Format("Public Sub NetOfficeTestMacro()\r\n {0}\r\nEnd Sub", "Selection.TypeText (\"This text is written by a automatic created macro with NetOffice...\")"); module.InsertLines(1, codeLines); //start the macro NetOfficeTestModule wordApplication.Run("NetOfficeTestModule!NetOfficeTestMacro"); string fileExtension = GetFileExtension(wordApplication); string documentFile = string.Format("{0}\\Example05{1}", _hostApplication.RootDirectory, fileExtension); double wordVersion = Convert.ToDouble(wordApplication.Version, CultureInfo.InvariantCulture); if (wordVersion >= 12.0) newDocument.SaveAs(documentFile, WdSaveFormat.wdFormatXMLDocumentMacroEnabled); else newDocument.SaveAs(documentFile); //close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); // show dialog for the user(you!) _hostApplication.ShowFinishDialog(null, documentFile); }