Ok, I'm new to VSTO programming, and I'm working on a project using it. Basicly what I'm trying to do is setup a command bar with a button that saves form data from a form, and saves a copy of the form in a specified directory. This button will be used with several different forms, so a config file will be made that will hold the information for where to save the data and the file, by comparing the name of the form to a line of text. The VSTO code I'm using is based heavily on code borrowed from a co-worker. The problem is, he was a contractor, and is no longer here. So I thought I'd see if anyone ehre could point me in the right direction.
The source looks like this:
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 CommandBar AppCommandBar;
private CommandBarButton cbbSaveData;
private CommandBarButton cbbEPAID;
private 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
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
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 = Document.Name;
StreamReader sr = new StreamReader(@"C:\Filetest\FATtext.txt"); //set this to correct path later
compline = sr.ReadLine();
while ((asConfigText = compline.Split('@'))[0] != docname)
{
compline = sr.ReadLine();
asConfigText[0] = compline.Split('@')[0];
}
filename = asConfigText[2];
sr.Close();
}
}
}
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
EBUG;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
bj\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,155): error CS0103: The name 'ThisAddIn_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(34,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(35,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(59,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(97,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(72,23): (Related location)
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(98,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(110,23): error CS0103: The name 'Document' does not exist in the current context
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 'ThisAddIn_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 155 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 34 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 35 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 59 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 97 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 98 45 Form Commands
Error 7 The name 'Document' does not exist in the current context \\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs 110 23 Form Commands
The source looks like this:
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 CommandBar AppCommandBar;
private CommandBarButton cbbSaveData;
private CommandBarButton cbbEPAID;
private 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
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
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 = Document.Name;
StreamReader sr = new StreamReader(@"C:\Filetest\FATtext.txt"); //set this to correct path later
compline = sr.ReadLine();
while ((asConfigText = compline.Split('@'))[0] != docname)
{
compline = sr.ReadLine();
asConfigText[0] = compline.Split('@')[0];
}
filename = asConfigText[2];
sr.Close();
}
}
}
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
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(32,155): error CS0103: The name 'ThisAddIn_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(34,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(35,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(59,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(97,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(72,23): (Related location)
\\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs(98,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(110,23): error CS0103: The name 'Document' does not exist in the current context
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 'ThisAddIn_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 155 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 34 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 35 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 59 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 97 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 98 45 Form Commands
Error 7 The name 'Document' does not exist in the current context \\file1\mis\dheald\My Documents\Visual Studio 2005\Projects\Save Doc Button\Save Doc Button\ThisAddIn.cs 110 23 Form Commands