A Day in the Life

A day in my life. Thoughts on leadership, management, startups, technology, software, concurrent development, etc... Basically the stuff I think about from 10am to 6pm.

10/19/2005

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

1 Comments:

At August 16, 2011 8:24 AM, Anonymous Javin @ java arraylist tutorial said...

Interesting piece of code. could be a very useful utility. Thanks for sharing.

Javin
10 tips on java debugging with eclipse

 

Post a Comment

Links to this post:

Create a Link

<< Home