void CAutoProjectDlg::OnBnClickedRun()
{
// Declare variables.
CApplication oExcel;
CWorkbooks oBooks;
CWorkbook oBook;
CCommandBars oCommandBars;
CCommandBar0 oCommandBar;
CCommandBarControls oControls;
CCommandBarButton oButton, oPopupButton;
CCommandBarPopup oPopup;
int iConn = 0;
COleVariant covOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
ZeroMemory(m_EventInfo,sizeof(EventInfo)*5);
// Start Excel.
if (!oExcel.CreateDispatch("Excel.Application")) {
AfxMessageBox("Couldn't create Excel",MB_SETFOREGROUND,NULL);
return;
}
m_pEightBallButton = new CClearButton();
m_pEightBallButton->pExcelDlg = this;
m_pBreakButton = new CPopupButton();
m_pBreakButton->pExcelDlg = this;
m_pComboEvent = new CComboEvent();
m_pComboEvent->pExcelDlg = this;
// Show Excel and set UserControl.
oExcel.put_Visible(TRUE);
oExcel.put_UserControl(TRUE);
// Add a new workbook
oBooks = oExcel.get_Workbooks();
oBook = oBooks.Add(covOptional);
// Get the command bars collection.
oCommandBars = oExcel.get_CommandBars();
try {
// Check to see if the command bar exists.
oCommandBar = oCommandBars.get_Item(COleVariant("Billiards Sample"));
}
catch(...)
{
TRACE("Billiards command bar doesn't exist. Creating it now.\r\n");
oCommandBar = oCommandBars.Add(COleVariant("Billiards Sample"),covOptional,covOptional,COleVariant((short)true));
}
// Show the command bar to the user.
oCommandBar.put_Visible(TRUE);
// Add a button to the command bar.
oControls = oCommandBar.get_Controls();
// 1 = Office.MsoControlType.msoControlButton
oButton = oControls.Add(COleVariant((short)1),covOptional,covOptional,covOptional,covOptional);
// Set the caption and face ID.
oButton.put_Caption("New game");
oButton.put_FaceId(1845);
SetupConnection(oButton.m_lpDispatch,&m_EventInfo[iConn],(IUnknown *)m_pEightBallButton,DIID__CommandBarButtonEvents);
iConn++;
// Add an edit box to the command bar.
// 2 = Office.MsoControlType.msoControlEdit
oEdit = oControls.Add(COleVariant((short)2),covOptional,covOptional,covOptional,covOptional);
// Show a vertical separator.
oEdit.put_BeginGroup(TRUE);
// Clear the text and show a caption.
oEdit.put_Text("");
oEdit.put_Caption("Enter your name:");
oEdit.put_Style(1); // 1 = Office.MsoComboStyle.msoComboLabel;
SetupConnection(oEdit.m_lpDispatch,&m_EventInfo[iConn],(IUnknown *)m_pComboEvent,DIID__CommandBarComboBoxEvents);
iConn++;
// Add a combo box to the command bar.
// 4 = Office.MsoControlType.msoControlComboBox
oCombo = oControls.Add(COleVariant((short)4),covOptional,covOptional,covOptional,covOptional);
// Add items to the combo box.
oCombo.AddItem("Sharky",covOptional);
oCombo.AddItem("Cash",covOptional);
oCombo.AddItem("Lucky",covOptional);
// Set the caption and style.
oCombo.put_Caption("Choose your opponent:");
oCombo.put_Style(1); // 1 = Office.MsoComboStyle.msoComboLabel;
SetupConnection(oCombo.m_lpDispatch,&m_EventInfo[iConn],(IUnknown *)m_pComboEvent,DIID__CommandBarComboBoxEvents);
iConn++;
// Add a drop-down list box to the command bar.
// 3 = Office.MsoControlType.msoControlDropdown
oDrop = oControls.Add(COleVariant((short)3),covOptional,covOptional,covOptional,covOptional);
// Add items to the list box.
oDrop.AddItem("8 Ball",covOptional);
oDrop.AddItem("9 Ball",covOptional);
oDrop.AddItem("Straight Pool",covOptional);
oDrop.AddItem("Bowlliards",covOptional);
oDrop.AddItem("Snooker",covOptional);
// Set the value to the first in the list.
oDrop.put_ListIndex(1);
// Set the caption and style.
oDrop.put_Caption("Choose your game:");
oDrop.put_Style(1); // 1 = Office.MsoComboStyle.msoComboLabel;
SetupConnection(oDrop.m_lpDispatch,&m_EventInfo[iConn],(IUnknown *)m_pComboEvent,DIID__CommandBarComboBoxEvents);
iConn++;
// Add a pop-up menu to the command bar.
// 10 = Office.MsoControlType.msoControlPopup
oPopup = oControls.Add(COleVariant((short)10),covOptional,covOptional,covOptional,covOptional);
// Add a separator before the pop-up button.
oPopup.put_BeginGroup(TRUE);
// Set the caption.
oPopup.put_Caption("Rack 'em Up!");
// Add a button to the pop-up.
// 1 = Office.MsoControlType.msoControlButton
oControls = oPopup.get_Controls();
oPopupButton = oControls.Add(COleVariant((short)1),covOptional,covOptional,covOptional,covOptional);
// Change the face ID and caption for the button.
oPopupButton.put_FaceId(643);
oPopupButton.put_Caption("Break!");
SetupConnection(oPopupButton.m_lpDispatch,&m_EventInfo[iConn],(IUnknown *)m_pBreakButton,DIID__CommandBarButtonEvents);
}