D
DYang
Hi
I am developing an Automation program in C# for Word XP. The program is running on Windows 2000 SP4 with Word XP SP3
Question 1: The CommandBarButton.Click event is not triggered for the first few items, but the rest works fine. What's wrong with my code
Question 2: The Normal.dot file grows around 9K bytes every time the program is executed. By opening Normal.dot, a section that has "Custom Popup ..." keeps increasing. All items in the command bar are removed before Word is closed and all items are added to the command bar with Temporary parameter set to true. After a while the file size of Normal.dot increases to around 3MB and keep growing, which makes opening and closing MS Word very slow. Is there anything I can do to stop Normal.dot from growing
Thanks a lot
DYan
Below is the sections of my program that adds and removes commmand bar
...
/// <summary
/// Create Office command bar items, designed to have all merge fields (@codes) from CU hos
/// </summary
/// <param name="MergeFields">DataView of all merge fields, sorted</param
/// <param name="cbrFields">Reference of the top level command bar for merge fields</param
/// <param name="CreateBarOnly">If true, only popup will be added; otherwise, the command bar buttons will be added
/// This parameter is essential to have popups appears at the top of the list and buttons follows all popup
/// Because the DataView is sorted by all columns and empty strings are sorted before non-empty ones
/// </param
private void AddCommandBarButtons(DataView MergeFields, ref Office.CommandBar cbrFields, bool CreateBarOnly
StringBuilder preCategory = new StringBuilder("preCat"); //intialize StringBuilder with non-empty strin
StringBuilder preSubCat1 = new StringBuilder("preSub1"); // so that Remove method will not generate exceptio
StringBuilder preSubCat2 = new StringBuilder("preSub2"); // the first time it is calle
StringBuilder category = new StringBuilder("category")
StringBuilder subCat1 = new StringBuilder("subCat1")
StringBuilder subCat2 = new StringBuilder("subCat2")
StringBuilder code = new StringBuilder("code")
StringBuilder name = new StringBuilder("name")
Office.CommandBarPopup cbrPopupParent = null
Office.CommandBarPopup cbrCurrentCategory = null, cbrCurrentCat1 = null, cbrCurrentCat2 = null
foreach (DataRowView row in MergeFields
tr
category.Remove(0, category.Length); //Clear the value in StringBuilde
subCat1.Remove(0, subCat1.Length)
subCat2.Remove(0, subCat2.Length)
code.Remove(0, code.Length)
name.Remove(0, name.Length)
if (row["Category"].ToString().Trim() == "") //set the value from DataView to StringBuilde
category.Append("[Not Categorized]")
els
category.Append(row["Category"].ToString())
subCat1.Append(row["SubCat1"].ToString())
subCat2.Append(row["SubCat2"].ToString())
code.Append(row["Code"].ToString())
name.Append(row["Name"].ToString())
// Add category if it does not exis
if (category.ToString() != preCategory.ToString()
cbrCurrentCategory = (Office.CommandBarPopup) cbrFields.FindControl(Office.MsoControlType.msoControlPopup, Optional, category.ToString(), true, true)
if (cbrCurrentCategory == null
cbrCurrentCategory = (Office.CommandBarPopup)cbrFields.Controls.Add
Office.MsoControlType.msoControlPopup, Optional, Optional, Optional, true)
cbrCurrentCategory.BeginGroup = true
cbrCurrentCategory.Caption = category.ToString()
cbrCurrentCategory.Tag = category.ToString()
preCategory.Remove(0, preCategory.Length)
preCategory.Append(category.ToString())
preSubCat1.Remove(0, preSubCat1.Length)
preSubCat1.Append("preSubCat1")
preSubCat2.Remove(0, preSubCat2.Length)
preSubCat2.Append("preSubCat2")
cbrPopupParent = cbrCurrentCategory
if (subCat1.Length > 0) //only check sub-categories when the string is not empt
if (subCat1.ToString() != preSubCat1.ToString()
{
cbrCurrentCat1 = (Office.CommandBarPopup)cbrPopupParent.CommandBar.FindControl(Office.MsoControlType.msoControlPopup, Optional, subCat1.ToString(), true, true);
if (cbrCurrentCat1 == null)
{
cbrCurrentCat1 = (Office.CommandBarPopup)cbrPopupParent.CommandBar.Controls.Add(
Office.MsoControlType.msoControlPopup, Optional, Optional, Optional, true);
cbrCurrentCat1.BeginGroup = true;
cbrCurrentCat1.Caption = subCat1.ToString();
cbrCurrentCat1.Tag = subCat1.ToString();
}
preSubCat1.Remove(0, preSubCat1.Length);
preSubCat1.Append(subCat1.ToString());
}
cbrPopupParent = cbrCurrentCat1;
if (subCat2.Length >0)
{
if (subCat2.ToString() != preSubCat2.ToString())
{
cbrCurrentCat2 = (Office.CommandBarPopup)cbrPopupParent.CommandBar.FindControl(Office.MsoControlType.msoControlPopup, Optional, subCat2.ToString(), true, true);
if (cbrCurrentCat2 == null)
{
cbrCurrentCat2 = (Office.CommandBarPopup)cbrPopupParent.CommandBar.Controls.Add(
Office.MsoControlType.msoControlPopup, Optional, Optional, Optional, true);
cbrCurrentCat2.BeginGroup = true;
cbrCurrentCat2.Caption = subCat2.ToString();
cbrCurrentCat2.Tag = subCat2.ToString();
}
preSubCat2.Remove(0, preSubCat2.Length);
preSubCat2.Append(subCat2.ToString());
}
cbrPopupParent = cbrCurrentCat2;
}
}
if (!CreateBarOnly)
{
Office.CommandBarButton popFields = (Office.CommandBarButton) cbrPopupParent.CommandBar.Controls.Add(
Office.MsoControlType.msoControlButton, Optional, Optional, Optional, true);
if (name.Length>0)
{
popFields.Caption = name.ToString();
popFields.Tag = name.ToString();
}
else
{
popFields.Caption = code.ToString();
popFields.Tag = code.ToString();
}
progressForm.Increment();
}
}
catch (Exception e)
{
CUWordErrorHandler.HandleError(e, this, "AddMergeFieldsCommandBar", "");
}
} //end of foreach loop
} // end of AddCommandBarButtons
....
/// <summary>
/// Subroutine to assign button click delegate to the Click event of the buttons on command bar
/// </summary>
/// <param name="fields">DtatView of the data source file that contains all merge fields</param>
/// <param name="cbrFields">object of the command bar</param>
private void AddCommandButtonClickEvent(DataView fields, Office.CommandBar cbrFields)
{
#if DEBUG
int i=0;
#endif
foreach(DataRowView row in fields)
{
try
{
Office.CommandBarButton btn = (Office.CommandBarButton) cbrFields.FindControl(Office.MsoControlType.msoControlButton, Optional, row["Name"].ToString(), true, true);
if (btn != null)
{
btn.Click += new Office._CommandBarButtonEvents_ClickEventHandler(cbrPopupButtonControl_Click);
#if DEBUG
i++;
#endif
}
}
catch (Exception e)
{
CUWordErrorHandler.HandleError(e, this, "AddMergeFieldsCommandBar", e.Message);
}
}
#if DEBUG
MessageBox.Show(i.ToString());
#endif
}
.....
/// <summary>
/// Remove command bar created
/// </summary>
public void RemoveCommandBar()
{
try
{ //remove each every controls on commandbar that created, otherwise the size of Normal.dot
//will increase every time the template createion is called
//deleting the root command bar will not do the trick as for Word XP
Office.CommandBar cbrControl = oApp.CommandBars["TCS Merge Fields"];
string[] fields = new string[500];
int count=0;
if (cbrControl != null)
{
foreach(Office.CommandBarPopup popup in cbrControl.Controls)
{
foreach(Office.CommandBarControl popup2 in popup.Controls)
{
if (popup2.Type == Office.MsoControlType.msoControlPopup)
{
Office.CommandBarPopup tmp = (Office.CommandBarPopup) popup2;
foreach(Office.CommandBarControl ctrl in tmp.Controls)
{
Office.CommandBarButton tmp1 = (Office.CommandBarButton) ctrl;
fields[count++]=ctrl.Tag;
tmp1.Delete(false);
}
tmp=null;
}
fields[count++]=popup2.Tag;
popup2.Delete(false);
}
fields[count++]=popup.Tag;
popup.Delete(false);
}
cbrControl.Delete();
cbrControl = null;
StreamWriter wr = File.CreateText(@"c:\fields.txt");
for (int i=0;i<count;i++)
wr.WriteLine(fields);
wr.Flush();
wr.Close();
}
}
catch(Exception e)
{
CUWordErrorHandler.HandleError(e, this, "RemoveCommandBar", "Name = Collector System Merge Fields");
}
}
I am developing an Automation program in C# for Word XP. The program is running on Windows 2000 SP4 with Word XP SP3
Question 1: The CommandBarButton.Click event is not triggered for the first few items, but the rest works fine. What's wrong with my code
Question 2: The Normal.dot file grows around 9K bytes every time the program is executed. By opening Normal.dot, a section that has "Custom Popup ..." keeps increasing. All items in the command bar are removed before Word is closed and all items are added to the command bar with Temporary parameter set to true. After a while the file size of Normal.dot increases to around 3MB and keep growing, which makes opening and closing MS Word very slow. Is there anything I can do to stop Normal.dot from growing
Thanks a lot
DYan
Below is the sections of my program that adds and removes commmand bar
...
/// <summary
/// Create Office command bar items, designed to have all merge fields (@codes) from CU hos
/// </summary
/// <param name="MergeFields">DataView of all merge fields, sorted</param
/// <param name="cbrFields">Reference of the top level command bar for merge fields</param
/// <param name="CreateBarOnly">If true, only popup will be added; otherwise, the command bar buttons will be added
/// This parameter is essential to have popups appears at the top of the list and buttons follows all popup
/// Because the DataView is sorted by all columns and empty strings are sorted before non-empty ones
/// </param
private void AddCommandBarButtons(DataView MergeFields, ref Office.CommandBar cbrFields, bool CreateBarOnly
StringBuilder preCategory = new StringBuilder("preCat"); //intialize StringBuilder with non-empty strin
StringBuilder preSubCat1 = new StringBuilder("preSub1"); // so that Remove method will not generate exceptio
StringBuilder preSubCat2 = new StringBuilder("preSub2"); // the first time it is calle
StringBuilder category = new StringBuilder("category")
StringBuilder subCat1 = new StringBuilder("subCat1")
StringBuilder subCat2 = new StringBuilder("subCat2")
StringBuilder code = new StringBuilder("code")
StringBuilder name = new StringBuilder("name")
Office.CommandBarPopup cbrPopupParent = null
Office.CommandBarPopup cbrCurrentCategory = null, cbrCurrentCat1 = null, cbrCurrentCat2 = null
foreach (DataRowView row in MergeFields
tr
category.Remove(0, category.Length); //Clear the value in StringBuilde
subCat1.Remove(0, subCat1.Length)
subCat2.Remove(0, subCat2.Length)
code.Remove(0, code.Length)
name.Remove(0, name.Length)
if (row["Category"].ToString().Trim() == "") //set the value from DataView to StringBuilde
category.Append("[Not Categorized]")
els
category.Append(row["Category"].ToString())
subCat1.Append(row["SubCat1"].ToString())
subCat2.Append(row["SubCat2"].ToString())
code.Append(row["Code"].ToString())
name.Append(row["Name"].ToString())
// Add category if it does not exis
if (category.ToString() != preCategory.ToString()
cbrCurrentCategory = (Office.CommandBarPopup) cbrFields.FindControl(Office.MsoControlType.msoControlPopup, Optional, category.ToString(), true, true)
if (cbrCurrentCategory == null
cbrCurrentCategory = (Office.CommandBarPopup)cbrFields.Controls.Add
Office.MsoControlType.msoControlPopup, Optional, Optional, Optional, true)
cbrCurrentCategory.BeginGroup = true
cbrCurrentCategory.Caption = category.ToString()
cbrCurrentCategory.Tag = category.ToString()
preCategory.Remove(0, preCategory.Length)
preCategory.Append(category.ToString())
preSubCat1.Remove(0, preSubCat1.Length)
preSubCat1.Append("preSubCat1")
preSubCat2.Remove(0, preSubCat2.Length)
preSubCat2.Append("preSubCat2")
cbrPopupParent = cbrCurrentCategory
if (subCat1.Length > 0) //only check sub-categories when the string is not empt
if (subCat1.ToString() != preSubCat1.ToString()
{
cbrCurrentCat1 = (Office.CommandBarPopup)cbrPopupParent.CommandBar.FindControl(Office.MsoControlType.msoControlPopup, Optional, subCat1.ToString(), true, true);
if (cbrCurrentCat1 == null)
{
cbrCurrentCat1 = (Office.CommandBarPopup)cbrPopupParent.CommandBar.Controls.Add(
Office.MsoControlType.msoControlPopup, Optional, Optional, Optional, true);
cbrCurrentCat1.BeginGroup = true;
cbrCurrentCat1.Caption = subCat1.ToString();
cbrCurrentCat1.Tag = subCat1.ToString();
}
preSubCat1.Remove(0, preSubCat1.Length);
preSubCat1.Append(subCat1.ToString());
}
cbrPopupParent = cbrCurrentCat1;
if (subCat2.Length >0)
{
if (subCat2.ToString() != preSubCat2.ToString())
{
cbrCurrentCat2 = (Office.CommandBarPopup)cbrPopupParent.CommandBar.FindControl(Office.MsoControlType.msoControlPopup, Optional, subCat2.ToString(), true, true);
if (cbrCurrentCat2 == null)
{
cbrCurrentCat2 = (Office.CommandBarPopup)cbrPopupParent.CommandBar.Controls.Add(
Office.MsoControlType.msoControlPopup, Optional, Optional, Optional, true);
cbrCurrentCat2.BeginGroup = true;
cbrCurrentCat2.Caption = subCat2.ToString();
cbrCurrentCat2.Tag = subCat2.ToString();
}
preSubCat2.Remove(0, preSubCat2.Length);
preSubCat2.Append(subCat2.ToString());
}
cbrPopupParent = cbrCurrentCat2;
}
}
if (!CreateBarOnly)
{
Office.CommandBarButton popFields = (Office.CommandBarButton) cbrPopupParent.CommandBar.Controls.Add(
Office.MsoControlType.msoControlButton, Optional, Optional, Optional, true);
if (name.Length>0)
{
popFields.Caption = name.ToString();
popFields.Tag = name.ToString();
}
else
{
popFields.Caption = code.ToString();
popFields.Tag = code.ToString();
}
progressForm.Increment();
}
}
catch (Exception e)
{
CUWordErrorHandler.HandleError(e, this, "AddMergeFieldsCommandBar", "");
}
} //end of foreach loop
} // end of AddCommandBarButtons
....
/// <summary>
/// Subroutine to assign button click delegate to the Click event of the buttons on command bar
/// </summary>
/// <param name="fields">DtatView of the data source file that contains all merge fields</param>
/// <param name="cbrFields">object of the command bar</param>
private void AddCommandButtonClickEvent(DataView fields, Office.CommandBar cbrFields)
{
#if DEBUG
int i=0;
#endif
foreach(DataRowView row in fields)
{
try
{
Office.CommandBarButton btn = (Office.CommandBarButton) cbrFields.FindControl(Office.MsoControlType.msoControlButton, Optional, row["Name"].ToString(), true, true);
if (btn != null)
{
btn.Click += new Office._CommandBarButtonEvents_ClickEventHandler(cbrPopupButtonControl_Click);
#if DEBUG
i++;
#endif
}
}
catch (Exception e)
{
CUWordErrorHandler.HandleError(e, this, "AddMergeFieldsCommandBar", e.Message);
}
}
#if DEBUG
MessageBox.Show(i.ToString());
#endif
}
.....
/// <summary>
/// Remove command bar created
/// </summary>
public void RemoveCommandBar()
{
try
{ //remove each every controls on commandbar that created, otherwise the size of Normal.dot
//will increase every time the template createion is called
//deleting the root command bar will not do the trick as for Word XP
Office.CommandBar cbrControl = oApp.CommandBars["TCS Merge Fields"];
string[] fields = new string[500];
int count=0;
if (cbrControl != null)
{
foreach(Office.CommandBarPopup popup in cbrControl.Controls)
{
foreach(Office.CommandBarControl popup2 in popup.Controls)
{
if (popup2.Type == Office.MsoControlType.msoControlPopup)
{
Office.CommandBarPopup tmp = (Office.CommandBarPopup) popup2;
foreach(Office.CommandBarControl ctrl in tmp.Controls)
{
Office.CommandBarButton tmp1 = (Office.CommandBarButton) ctrl;
fields[count++]=ctrl.Tag;
tmp1.Delete(false);
}
tmp=null;
}
fields[count++]=popup2.Tag;
popup2.Delete(false);
}
fields[count++]=popup.Tag;
popup.Delete(false);
}
cbrControl.Delete();
cbrControl = null;
StreamWriter wr = File.CreateText(@"c:\fields.txt");
for (int i=0;i<count;i++)
wr.WriteLine(fields);
wr.Flush();
wr.Close();
}
}
catch(Exception e)
{
CUWordErrorHandler.HandleError(e, this, "RemoveCommandBar", "Name = Collector System Merge Fields");
}
}