Current window and new window using office PIA 2002

L

Liming

Hi,

I have a custom CommandBar with button, each click of the button will
activate a webform (just a dropdownlist) and insert values of the
webform into the current document.

This works all fine. But if I choose "file-new", another new word
doucment opens up, then I click the same commandBar button, the webform
no long pops up. Why is that?
 
L

Liming

Here is the code.

public void changeStyle_Click(Microsoft.Office.Core.CommandBarButton
barButton, ref bool someBool)
{
object newStyle = "";
// my windows form
IFBProperties ifbprop = new IFBProperties();
// This is the method causing the problem.
*applicatonObject was
// initaled to application object in
onConnection method
ConnectToIFB(applicationObject);
if(properties!=null)
{
ifbprop.CustomPropertiesText.DataSource =
properties.Tables[0].DefaultView;
ifbprop.CustomPropertiesText.DisplayMember = "PROP_NAME";
ifbprop.CustomPropertiesText.ValueMember = "FIELD_CODE";
ifbprop.CustomPropertiesValue.Text = "";
}

if (ifbprop.ShowDialog() ==
System.Windows.Forms.DialogResult.OK)
{
newStyle = ifbprop.CustomPropertiesText.SelectedValue;
}

if (wordApp != null)
{
this.wordApp.ActiveWindow.Selection.InsertBefore(Convert.ToString(newStyle));

}
}


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 );
}
}


}
 
B

- B@rney

Sorry, I should have been more specific. I need to see the code where you add
your commandbarbutton...
 
L

Liming

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;
}
}
}
 
B

- B@rney

As per kb article 826931 you need to set the Tag property of the
commandbarbutton.

http://support.microsoft.com/?kbid=826931

HTH

--
- B@rney
___________________________
http://www.sharepointdemo.biz/
http://www.sharepointdemo.biz/blogs/bjarne/


Liming said:
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;
}
}
}
 
L

Liming

Hello Barney,

I'm running against xp 2002, not 2003 and the problem is that it opens
up a new window using this.wordApp.ActiveWindow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top