Article ID: 198522
Article Last Modified on 3/21/2005
180625 How To Use Automation to Modify the Office Menu
194906 How To Add and Run a VBA Macro Using Automation from MFC
224925 INFO: Type Libraries for Office May Change with New Release
178749 How To Create an Automation Project Using MFC and a Type Library
C:\Program Files\Microsoft Office\mso97.dll
#include "msword8.h" // or the appropriate version's header
#include "mso97.h" // ot the appropriate version's header
// Sample code
word::_Application oWord; // using namespace for the
// Word typelib's members
// Convenient values declared as ColeVariants.
COleVariant covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// Get the Word IDispatch pointer and attach it to the Word object.
if (!oWord.CreateDispatch("Word.Application"))
{
AfxMessageBox("Couldn't get Word object.");
return;
}
oWord.SetVisible(TRUE); //This shows the application.
word::Documents oDocs(oWord.GetDocuments());
word::_Document oDoc;
// Create a new document...
/* Word 97
oDoc = oDocs.Add(covOptional, covOptional);
End Word 97 */
/* Word 2002 */
oDoc = oDocs.Add(covOptional, // Template
covOptional, // New Template
covOptional, // Document Type
covTrue); // Visible
/* Office 97 - no underscore in front of CommandBars
CommandBars oBars = oWord.GetCommandBars(); // New CommandBars
// collection object
end of Office 97 syntax */
/* Office XP - note the underscore in front of CommandBars */
_CommandBars oBars = oWord.GetCommandBars(); // New CommandBars
// collection object
CommandBar oBar;
CommandBarControls oControls;
/* Office 97 usage - no underscore in front of CommandBarButton
CommandBarButton oButton;
end of Office 97 usage */
/* Office XP usage - note the underscore in front of CommandBarButton */
_CommandBarButton oButton;
VARIANT vName;
vName.vt = VT_BSTR;
vName.bstrVal = SysAllocString(L"MyNewCommandBar");
VARIANT vPosition;
vPosition.vt = VT_I2;
vPosition.iVal = 4; // 4 = Floating; 0 = top;
// Add new bar to the command bars collection
CommandBar oNewBar = oBars.Add(vName, // const Variant Name
vPosition, // const Variant Position
covFalse, // const Variant (replace)MenuBar
covTrue // const Variant Temporary
);
oNewBar.SetVisible(TRUE);
// Get the collection (now empty) of controls on the new commandbar
CommandBarControls oNewControls = oNewBar.GetControls();
VARIANT vType;
vType.vt = VT_I4;
vType.lVal = (long)1;
// Add a commandbarbutton to the collection of controls
// on new commandbar
// CommandBarButton oNewButton1 = // Office XP uses underscore
_CommandBarButton oNewButton1 = // Office XP - note leading underscore
oNewControls.Add(vType, // Type = msoControlButton
covOptional, // Id
covOptional, // Parameter
covOptional, // Before
covTrue // Temporary
);
oNewButton1.SetStyle(3); // msoButtonIconAndCaption
oNewButton1.SetCaption("New");
oNewButton1.SetTooltipText("This is a new button");
oNewButton1.SetVisible(TRUE);
oNewButton1.SetState(0); // msoButtonUp
OpenClipboard(); // Reserve clipboard for this program
// ***************************************************************
// There are two alternative sources of bitmaps that this sample
// can use. Choose either the 4 lines (next) that use "MyBitmap"
// from the resource file, or the line below that which loads a
// bitmap from a bitmap file.
// Alternative 1: This code places a bitmap drawn by you in the
// resource editor onto a button
/* Remove the comment if you use this alternative
CBitmap MyBitmap;
MyBitmap.LoadBitmap(IDB_BITMAP1); // A Bitmap you drew in the
// Resource Editor
HBITMAP MyBitmapHandle = (HBITMAP)MyBitmap; // Cast it to a HBITMAP
SetClipboardData(CF_BITMAP, MyBitmapHandle);
*/ //Remove the comment to use alternative 1.
// End if alternative 1
// Alternative 2: This code places a bitmap from a file onto
// the button. Comment it out if you use alternative 1 above.
if(NULL==(SetClipboardData(CF_BITMAP, LoadImage(NULL,
"C:\\Test.bmp", IMAGE_BITMAP, 0,
0, LR_DEFAULTSIZE|LR_LOADFROMFILE) )))
{
AfxMessageBox("SetClipboardData returned a NULL");
};
// End of alternative 2
// End of alternatives
// ***************************************************************
CloseClipboard(); // Free clipboard so PasteFace() can use it
oNewButton1.PasteFace(); // This places the image from the
// clipboard onto the command button
AfxMessageBox("Look at the button's face now");
oWord.Quit(covFalse, covOptional, covFalse);
// End of Sample code
Keywords: kbautomation kbbutton kbhowto KB198522