using
System;using
System.IO;using
System.Text;using
System.Windows.Forms;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
Microsoft.VisualStudio.Tools.Applications.Runtime;using
Word = Microsoft.Office.Interop.Word;using
Office = Microsoft.Office.Core;using
Microsoft.Office.Core;using
Microsoft.Office.Interop;using
Microsoft.Office.Tools;namespace
Save_Doc_Button{
public partial class ThisAddIn{
private Microsoft.Office.Core.CommandBar AppCommandBar; private Microsoft.Office.Core.CommandBarButton cbbSaveData; private Microsoft.Office.Core.CommandBarButton cbbEPAID; private Microsoft.Office.Core.CommandBarButton cbbdate; string docname; string filename;private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.BuildFormCommandBar();((Microsoft.Office.Interop.Word.
ApplicationEvents4_Event)Application).NewDocument += new Microsoft.Office.Interop.Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);Application.WindowActivate +=
new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowActivateEventHandler(Application_WindowActivate);Application.DocumentOpen +=
new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e){
}
#region
VSTO generated code ///{
this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);}
#endregion
private void BuildFormCommandBar(){
AppCommandBar = CreateCommandBar(
"Form Tools");cbbSaveData = CreateCommandBarButton(
"Extract and Save", 0, Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption, "Extract Data", "Extract Data from Form");cbbSaveData.Click +=
new Office._CommandBarButtonEvents_ClickEventHandler(cbbSaveData_Click);}
private Microsoft.Office.Core.CommandBar CreateCommandBar(string name){
return this.Application.CommandBars.Add(name, Microsoft.Office.Core.MsoBarPosition.msoBarFloating, false, true);}
static string Data_File_Name()
{
int namevar = 01; string namevar_2 = namevar.ToString();string flnm = "Test'namevar_2'.txt"; string path = @"c:\temp\'filename"; while (File.Exists(path))
{
namevar++;
namevar_2 = namevar.ToString();
}
flnm = path;
return flnm;}
private void cbbSaveData_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{
this.Application.ActiveDocument.SaveFormsData = true;Data_File_Name(filename);
this.Application.ActiveDocument.save(filename);}
private void file_path_check(){
string[] asConfigText = new string[2]; string compline;docname = Microsoft.Office.Interop.Word.
Document.Name; StreamReader sr = new StreamReader(@"C:\Filetest\FATtext.txt"); //set this to correct path latercompline = sr.ReadLine();
while ((asConfigText = compline.Split('@'))[0] != docname){
compline = sr.ReadLine();
asConfigText[0] = compline.Split(
'@')[0];}
filename = asConfigText[2];
sr.Close();
}
}
}
and the output looks like this:
------ Build started: Project: Form Commands, Configuration: Debug Any CPU ------
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701,1702 /warn:4 /define:DEBUG;TRACE /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Accessibility.dll /reference:"c:\Program Files\Common Files\Microsoft Shared\VSTO\8.0\Microsoft.Office.Tools.Common.dll" /reference:"c:\Program Files\Common Files\Microsoft Shared\VSTO\8.0\Microsoft.VisualStudio.Tools.Applications.Runtime.dll" /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.XML.dll /reference:C:\WINDOWS\assembly\GAC\Office\11.0.0.0__71e9bce111e9429c\Office.dll /reference:C:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Word\11.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll /reference:C:\WINDOWS\assembly\GAC\stdole\7.0.3300.0__b03f5f7f11d50a3a\stdole.dll /debug+ /debug:full /optimize- /out:"obj\Debug\Save Doc Button.dll" /resource:obj\Debug\Save_Doc_Button.Properties.Resources.resources /target:library Properties\AssemblyInfo.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs ThisAddIn.cs ThisAddIn.Designer.cs
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(32,175): error CS0103: The name 'Application_NewDocument' does not exist in the current context
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(33,123): error CS0103: The name 'Application_WindowActivate' does not exist in the current context
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(34,119): error CS0103: The name 'Application_DocumentOpen' does not exist in the current context
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(58,27): error CS0103: The name 'CreateCommandBarButton' does not exist in the current context
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(96,13): error CS1501: No overload for method 'Data_File_Name' takes '1' arguments
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(71,23): (Related location)
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(97,45): error CS0117: 'Microsoft.Office.Interop.Word.Document' does not contain a definition for 'save'
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(109,23): error CS0120: An object reference is required for the nonstatic field, method, or property 'Microsoft.Office.Interop.Word._Document.Name.get'
c:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Word\11.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll: (Related file)
Compile complete -- 7 errors, 0 warnings
------ Skipped Build: Project: Save Doc ButtonSetup, Configuration: Debug ------
Project not selected to build for this solution configuration
========== Build: 0 succeeded or up-to-date, 1 failed, 1 skipped ==========
And the error list looks like this:
Error 1 The name 'Application_NewDocument' does not exist in the current context \\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs 32 175 Form Commands
Error 2 The name 'Application_WindowActivate' does not exist in the current context \\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs 33 123 Form Commands
Error 3 The name 'Application_DocumentOpen' does not exist in the current context \\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs 34 119 Form Commands
Error 4 The name 'CreateCommandBarButton' does not exist in the current context \\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs 58 27 Form Commands
Error 5 No overload for method 'Data_File_Name' takes '1' arguments \\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs 96 13 Form Commands
Error 6 'Microsoft.Office.Interop.Word.Document' does not contain a definition for 'save' \\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs 97 45 Form Commands
Error 7 An object reference is required for the nonstatic field, method, or property 'Microsoft.Office.Interop.Word._Document.Name.get' \\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs 109 23 Form Commands
They never really taught this stuff in school, and I'm having a hard time pinning down a good reference document for how to do this stuff. Alot of the code is adapted from a project done by a coworker, but he's not here to ask what I did wrong. Not sure when or if he'll be back since he's a contractor.
joe_cat_okPosted Apr 5, 2007, 5:28 PM
Be sure that you have the methods in place to actually handle the events your are wiring up. In your case the following methods would work:
private void Application_NewDocument(Microsoft.Office.Interop.Word.Document Doc)
private void Application_WindowActivate( Microsoft.Office.Interop.Word.Document Doc, Microsoft.Office.Interop.Word.Window wn)
}
{
}
private void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
{
}
{
Hope this helps,
Scott the wayward consultant.