Software: C# Code to Walk an Excel CommandBar
I took Guogiang Wu’s code I found in this post: Programming Office Commandbars - get the ID of a CommandBarControl and ported it to C#. I also made a few changes.
1. I dump the information to a temporary file.
2. I use indenting to indicate levels. It’s easier for me to understand that way.
public void DumpBars() {
StreamWriter stOutput = File.CreateText("\\temp\\CommandBar.txt");
int iCmdCount = mApplicationObject.CommandBars.Count + 1;
stOutput.WriteLine ("CommandBar Controls");
string strIndent = " ";
for (int iCount = 1; iCount < iCmdCount; iCount++) {
DumpBar(mApplicationObject.CommandBars[iCount],
stOutput, strIndent);
}
stOutput.Close();
}
public void DumpBar(CommandBar cmdBar, StreamWriter stOutput,
string strIndent) {
stOutput.WriteLine(strIndent + " CommandBar Name: " + cmdBar.Name);
int iCmdCount = cmdBar.Controls.Count + 1;
strIndent += " ";
for (int iCount = 1; iCount < iCmdCount; iCount++) {
DumpControl(cmdBar.Controls[iCount],
stOutput, strIndent);
}
}
public void DumpControl(CommandBarControl cmdControl,
StreamWriter stOutput, string strIndent) {
stOutput.WriteLine(strIndent + " Control Caption: " +
cmdControl.Caption + " ID: " + cmdControl.Id);
if ((cmdControl.Type == MsoControlType.msoControlPopup) ||
(cmdControl.Type == MsoControlType.msoControlGraphicPopup) ||
(cmdControl.Type == MsoControlType.msoControlButtonPopup) ||
(cmdControl.Type == MsoControlType.msoControlSplitButtonPopup) ||
(cmdControl.Type == MsoControlType.msoControlSplitButtonMRUPopup)) {
CommandBarPopup cmdPopup = (CommandBarPopup) cmdControl;
int iCmdCount = cmdPopup.Controls.Count + 1;
strIndent += " ";
for (int iCount = 1; iCount < iCmdCount; iCount++) {
DumpControl (cmdPopup.Controls[iCount], stOutput, strIndent);
}
}
}
2 Comments:
Interesting piece of code. could be a very useful utility. Thanks for sharing.
Javin
10 tips on java debugging with eclipse
well explained . thanks for sharing good post . Good arraylist example collection visit
Arraylist example
Post a Comment
<< Home