本例創(chuàng)建環(huán)境:Windows7(64位)+office2013+VS2015
二、創(chuàng)建項目
1、創(chuàng)建名為ExcelRibbon 的“Excel外接程序”項目,如下圖所示。
創(chuàng)建“外接程序”選項卡,請將“功能區(qū)(XML)”項添加到項目中。同時向此選項卡添加一些按鈕。
2、在“添加新項”對話框中,選擇“功能區(qū)(XML)”。如下圖圖所
在 ExcelRibbon.vb 文件將在設(shè)計器中打開。還會將名為 ExcelRibbon.xml 的 XML 文件添加到項目中。
三、將按鈕添加到“外接程序”選項卡
1、在“解決方案資源管理器”中,右鍵單擊 ExcelRibbon.xml,然后單擊“打開”。
- 將以下內(nèi)容替換:
- <?xml version="1.0" encoding="UTF-8"?>
- <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
- <ribbon>
- <tabs>
- <tab idMso="TabAddIns">
- <group id="MyGroup"
- label="My Group">
- </group>
- </tab>
- </tabs>
- </ribbon>
- </customUI>
- 替換后的內(nèi)容為
- <?xml version="1.0" encoding="UTF-8"?>
- <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
- <ribbon>
- <tabs>
- <tab id="henrylam" label="我的測試">
- <group id="test"
- label="測試">
- <button id="Button1"
- label="插入內(nèi)容"
- size="large"
- imageMso="HappyFace"
- onAction="test1" />
- <button id="Button2"
- label="關(guān)于"
- size="large"
- imageMso="AnimationTransitionGallery"
- screentip="使用說明"
- supertip="鼠標(biāo)放在上面顯示使用說明"
- onAction="test2" />
- </group >
- </tab>
- </tabs>
- </ribbon>
- </customUI>
復(fù)制代碼
四、添加按鈕的回調(diào)方法
1、在“解決方案資源管理器”中,右鍵單擊 Ribbon.vb,然后單擊“打開”。
2.將以下代碼添加到WordRibbon.vb文件的頂部。 此代碼將為Microsoft.Office.Interop.Word 命名空間創(chuàng)建別名
Imports Excel =Microsoft.Office.Interop. Excel ‘命名空間創(chuàng)建別名
插入后如下圖
3、將以下方法添加到ExcelRibbon類。 這是“插入文本”按鈕的回叫方法,用于在光標(biāo)的當(dāng)前位置向活動文檔添加字符串。
Public Subtest1(ByVal control As Office.IRibbonControl)
Dim ashAsExcel.Worksheet = CType(Globals.ThisAddIn.Application.ActiveSheet,Excel.Worksheet)
ash.Range("A1:e1").Value= {"FileName", "File Type", "File Size", "LastChange", "File Path"}
ash.Cells.Borders.ColorIndex = 1
ash.Range("A1:e1").Borders.ColorIndex= 1
End Sub
插入位置如下圖
插入后的如下圖
五、測試 VSTO 外接程序
1.按 F5 運行項目?;?/font>
2.確認(rèn)“外接程序”選項卡在功能區(qū)上可見。
3.單擊“外接程序”選項卡。
4.確認(rèn)“我的測試”組在功能區(qū)上可見。
5.單擊“我的測試”組中的“插入內(nèi)容”按鈕。
向表格填充內(nèi)容。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。