hi Barney,
i'm using the exact same code here. I have problem in the exactly
method "insertText_Click"
http://msdn.microsoft.com/library/d...officeadd-inswithvisualcnetvisualbasicnet.asp
I'll assemble the whoe code here in C#.
Word.Application wordApp = null;
Excel.Application excelApp = nul;
object applicationObject = null;
Microsoft.Office.Core.CommandBarButton insertText;
Microsoft.Office.Core.CommandBarButton styleText;
private void ConnectToIFB(object application)
{
Word._Document oDoc;
object oDocCustomProps;
object oMissing =
System.Reflection.Missing.Value;
// Retreive custom properties from web services
IFBSystemService service = new
IFBSystemService();
properties = service.GetAllCustomProperties();
//Add a property/value pair to the
CustomDocumentProperties
collection.
oDoc = this.wordApp.ActiveDocument;
oDocCustomProps =
oDoc.CustomDocumentProperties;
Type typeDocCustomProps =
oDocCustomProps.GetType();
foreach(DataRow row in
properties.Tables[0].Rows)
{
String strIndex =
Convert.ToString(row["PROP_NAME"]);
String strValue =
Convert.ToString(row["FIELD_CODE"]);
if(strIndex!=null && strValue!=null)
{
object[] oArgs =
{strIndex,false,
MsoDocProperties.msoPropertyTypeString,
strValue};
typeDocCustomProps.InvokeMember("Add",BindingFlags.Default |
BindingFlags.InvokeMethod, null,
oDocCustomProps, oArgs
);
}
}
}
// C#
private void SetApplicationFields(object application)
{
applicationObject = application;
if (application is Word.Application)
{
wordApp = (Word.Application)application;
excelApp = null;
}
else if (application is Excel.Application)
{
excelApp = (Excel.Application)application;
wordApp = null;
}
}
// C#
public void OnConnection(object application,
Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
// Setup code for the add-in.=
SetApplicationFields(application);
// More setup code for the add-in.
// C#
Microsoft.Office.Core.CommandBar toolBar = null;
if (wordApp != null)
{
toolBar = AddWordToolbar(wordApp, "Some useful toolbar.");
}
insertText = MakeANewButton(toolBar, "Insert text", 1044,
new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler
(insertText_Click));
}
// C#
private Microsoft.Office.Core.CommandBar AddWordToolbar(
Word.Application word, string toolbarName)
{
Microsoft.Office.Core.CommandBar toolBar = null;
try
{
// Create a command bar for the add-in
object missing = System.Reflection.Missing.Value;
toolBar = (Microsoft.Office.Core.CommandBar)
wordApp.CommandBars.Add(toolbarName,
Microsoft.Office.Core.MsoBarPosition.msoBarTop,
missing, true );
toolBar.Visible = true;
return toolBar;
}
catch
{
// Add exception handling here.
return null;
}
}
// C#
private Microsoft.Office.Core.CommandBarButton MakeANewButton(
Microsoft.Office.Core.CommandBar commandBar, string caption,
int faceID,
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler
clickHandler )
{
object missing = System.Reflection.Missing.Value;
try
{
Microsoft.Office.Core.CommandBarButton newButton;
newButton = (Microsoft.Office.Core.CommandBarButton)
commandBar.Controls.Add(
Microsoft.Office.Core.MsoControlType.msoControlButton,
missing, missing, missing, missing);
newButton.Caption = caption;
newButton.FaceId = faceID;
newButton.Click += clickHandler;
return newButton;
}
catch (System.Exception ex)
{
// Add code here to handle the exception.
return null;
}
}
public void insertText_Click(Microsoft.Office.Core.CommandBarButton
barButton, ref bool someBool)
{
string text = "";
System.Windows.Forms.IDataObject data =
System.Windows.Forms.Clipboard.GetDataObject();
ConnectToIFB(applicationObject);
if (data.GetDataPresent(System.Windows.Forms.DataFormats.Text))
{
text = data.GetData(System.Windows.Forms.DataFormats.Text).
ToString();
if (wordApp != null)
{
this.wordApp.ActiveWindow.Selection.InsertBefore(text);
}
else if (excelApp != null)
{
this.excelApp.ActiveCell.Value2 = text;
}
}
}