Vấn đề hiển thị Menu tự tạo trong Excel 2007

Với excel 2007, tôi muốn cái Menu tự tạo (nó tự động group lại trong tab Add-Ins), khi tôi mở file, thì mặc định tab Add-Ins này hiển thị, không phải là tab Home có được không?

Xin cám ơn rất nhiều!

Trước tiên vài lời về cấu trúc nội dung mà ta sẽ phải soạn thảo.
Nội dung bắt đầu bằng 2 thẻ:

<customUI xmlns="[URL]https://schemas.microsoft.com/office/2006/01/customui[/URL]" >
<ribbon>

và kết thúc bằng

</ribbon>
</customUI>

Phần trên là bắt buộc. Mọi cái ta thêm vào thì ta viết trong thẻ:

<ribbon>
...
</ribbon>

Tiếp theo nếu ta muốn thêm menu vào nút Office thì ta phài có thẻ

<officeMenu>
...
</officeMenu>

Còn nếu ta muốn thêm vào thẻ có sẵn hoặc tạo thẻ mới thì ta phải có thẻ

<tabs>
...
</tabs>

Và trong thẻ đó ta phải có "bằng này" thẻ

<tab>
...
</tab>

nếu ta muốn thêm menu vào "bằng này" thẻ có sẵn – tạo mới.
Trong mỗi thẻ

<officeMenu>
...
</officeMenu>
hoặc
<tab>
...
</tab>

thì nội dung tùy thuộc vào menu mà ta định thêm vào. Mục menu đơn? Nhóm menu? v…v
Tôi đã soạn hình như là đủ mọi kiểu menu.
Tóm lại cấu trúc của văn bản soạn cho Custom UI Editor là:

<customUI xmlns="[URL]https://schemas.microsoft.com/office/2006/01/customui[/URL]" >
<ribbon>
<officeMenu>
...
</officeMenu>
<tabs>
<tab>
...
</tab>
...
<tab>
...
</tab>
</tabs>
</ribbon>
</customUI>

Trong ví dụ đính kèm tôi gộp cả 3 loại ví dụ. Tức thêm menu "ở mọi nơi"

——————–
Một trong các cách thêm các nút lệnh, trình đơn vào trong dải menu của Excel là cách dùng nội dung XML. Cách làm như sau:

1. Cần tải về và cài đặt Custom UI Editor. Tôi cũng đã đính kèm Custom UI Editor ở dưới
Trước khi cài có thể cần cài .NET framework ≥ 3

2. Soạn nội dung mà ta sẽ phải dán vào cửa sổ của Custom UI Editor
Tùy theo việc ta định làm gì mà nội dung khác nhau. Sau đây là 3 nội dung ví dụ:

a. Thêm nút hoặc menu vào Backstage hoặc nút Office. Nội dung:
<!– Thêm mục mới trong danh sách menu hiển thị sau khi nhấn vào nút Office. Nội dung được thêm vào tập tin chan_dai.xlsm –>
<customUI xmlns="https://schemas.microsoft.com/office/2006/01/customui" >
<!– Thêm 1 nút và 1 menu có 7 lựa chọn vào menu của Excel trong nút Office–>
<!– Thêm 1 nút vào menu Print của Excel và thêm 1 nút vào menu Prepare–>
<!– chú ý cách dùng & để gạch dưới một chữ cái trong tên của menu –>
<ribbon>
<officeMenu>
<!– Thêm 1 nút vào menu –>
<button id="customButton1" label="&Nút của tôi" onAction="Macro1" imageMso="DirectRepliesTo" />
<!– Thêm 1 nút và 1 menu có 7 lựa chọn vào menu của Excel –>
<menu id="MyDropdownMenu" label="&Trình đơn của tôi" imageMso="HappyFace" >
<button id="customButton2" label="Nút thứ 2" onAction="Macro2" imageMso="TextAlignGallery" />
<button id="customButton3" label="Nút thứ 3" onAction="Macro3" imageMso="TextAlignGallery" />
<button id="customButton4" label="Nút thứ 4" onAction="Macro4" imageMso="TextAlignGallery" />
<button id="customButton5" label="Nút thứ 5" onAction="Macro5" imageMso="TextAlignGallery" />
<button id="customButton6" label="Nút thứ 6" onAction="Macro6" imageMso="TextAlignGallery" />
<button id="customButton7" label="Nút thứ 7" onAction="Macro7" imageMso="TextAlignGallery" />
<button id="customButton8" label="Nút thứ 8" onAction="Macro8" imageMso="TextAlignGallery" />
</menu>
<!– Thêm 1 nút vào menu Print của Excel –>
<splitButton idMso="FilePrintMenu">
<menu>
<button id="customButton9" label="Nút thứ 9" onAction="Macro9" imageMso="FilePrint" description="Miêu tả gì đó"/>
</menu>
</splitButton>
<!– Thêm 1 nút vào menu Prepare của Excel –>
<menu idMso="FilePrepareMenu">
<button id="customButton10" label="Nút thứ 10" onAction="Macro10" imageMso="HappyFace" description="Một chút miêu tả"/>
</menu>
</officeMenu>
</ribbon>
</customUI>

b. Thêm nút hoặc menu vào thẻ có sẵn của Excel. Nội dung:
<!– Thêm nút hoặc menu vào một thẻ nào đó, vd. thẻ Home –>
<customUI xmlns="https://schemas.microsoft.com/office/2006/01/customui">

<ribbon>
<tabs>
<!– thêm 1 nhóm gồm 3 nút và menu có 5 mục vào thẻ Home –>
<tab idMso="TabHome" >
<group id="customGroup1" label="Nhóm gì đó" insertAfterMso="GroupEditingExcel" >
<button id="customButton1" label="Nút thứ 1" size="normal" onAction="Macro1" imageMso="DirectRepliesTo" />
<button id="customButton2" label="Nút thứ 2" size="normal" onAction="Macro2" imageMso="AccountMenu" />
<button id="customButton3" label="Nút thứ 3" size="normal" onAction="Macro3" imageMso="RegionLayoutMenu" />
<separator id="MySeparator1" />
<menu id="MyDropdownMenu" label="Menu của riêng tôi" size="large" imageMso="TextAlignGallery" >
<button id="customButton4" label="Nút thứ 4" onAction="Macro4" imageMso="TextAlignGallery" />
<button id="customButton5" label="Nút thứ 5" onAction="Macro5" imageMso="TextAlignGallery" />
<button id="customButton6" label="Nút thứ 6" onAction="Macro6" imageMso="TextAlignGallery" />
<button id="customButton7" label="Nút thứ 7" onAction="Macro7" imageMso="TextAlignGallery" />
<button id="customButton8" label="Nút thứ 8" onAction="Macro8" imageMso="TextAlignGallery" />
</menu>
</group>
</tab>
</tabs>
</ribbon>
</customUI>

c. Thêm thẻ mới vào Ribbon. Nội dung:
<customUI xmlns="https://schemas.microsoft.com/office/2006/01/customui">
<!– Thêm thẻ mới vào Ribbon –>
<ribbon>
<tabs>
<tab id="MyCustomTab" label="Thẻ riêng của tôi" insertAfterMso="TabHome">
<!– thêm nhóm gồm 2 nút –>
<group id="customGroup1" label="Nhóm thứ 1">
<button id="customButton1" label="Nút thứ 1" size="normal" onAction="Macro1" imageMso="PrintPreviewShrinkOnePage" />
<separator id="MySeparator1" />
<button id="customButton2" label="Nút thứ 2" size="large" onAction="Macro2" imageMso="ReviewPreviousComment" />
</group>
<!– thêm nhóm gồm menu có 2 nút –>
<group id="customGroup2" label="Nhóm thứ 2">
<menu id="MyDropdownMenu" label="Menu của tôi" size="large" imageMso="TextAlignGallery" >
<button id="customButton3" label="Nút thứ 3" onAction="Macro3" imageMso="TextAlignGallery" />
<button id="customButton4" label="Nút thứ 4" onAction="Macro4" imageMso="TextAlignGallery" />
</menu>
</group>
<!– thêm nhóm gồm splitButton trong đó có 1 nút và 1 menu có 2 nút –>
<group id="customGroup3" label="Nhóm thứ 3">
<splitButton id="mySplitButton" size="large">
<button id="customButton5" imageMso="HappyFace" label="Nút thứ 5" supertip="Đây là splitButton." onAction="Macro5"/>
<menu id="splitMenu" itemSize="large">

<button id="customButton6" imageMso="FormatPainter" label="Nút thứ 6" onAction="Macro6" description="Miêu tả gì đó"/>
<button id="customButton7" imageMso="FormatPainter" label="Nút thứ 7" onAction="Macro7" description="Miêu tả nhé"/>
</menu>
</splitButton>
</group>
<!– thêm nhóm gồm 2 cụm nút, mỗi cụm 2 nút –>
<group id="customGroup4" label="Nhóm thứ 4">
<buttonGroup id="mybuttonGroup1">
<button id="customButton8" label="Nút thứ 8" showLabel="false" onAction="Macro8" imageMso="ReviewPreviousComment" />
<button id="customButton9" label="Nút thứ 9" showLabel="false" onAction="Macro9" imageMso="ReviewPreviousComment" />
</buttonGroup>
<buttonGroup id="mybuttonGroup2">
<button id="customButton10" label="Nút thứ 10" showLabel="false" onAction="Macro10" imageMso="HappyFace" />
<button id="customButton11" label="Nút thứ 11" showLabel="false" onAction="Macro11" imageMso="HappyFace" />
</buttonGroup>
</group>
</tab>
</tabs>
</ribbon>
</customUI>

3. Các bước thực hiện
a. Mở Excel –> lưu lại với tên vd. chan_dai.xlsm –> đóng Excel

b.

Mở Custom UI Editor –> mởi tập tin chan_dai.xlsm vừa lưu –> chuột phải vào chan_dai.xlsm và chọn "Office 2007 (2010) Custom UI Part –> dán nội dung ở phần 2 vào cửa sổ bên phải –> nhấn nút cuối cùng trên dải menu (Generate Callbacks) –> bôi đen nội dung vừa được tạo ra và dán vào bộ nhớ đệm (Ctrl + C) –> click vào chan_dai.xlsm ở nửa trái để hiển thị lại nội dung viết ở phần 2 –> lưu lại tập tin chan_dai.xlsm –> đóng Custom UI Editor
Chú ý: Khi mở Excel thì Excel xóa clipboard nên hoặc ta mở Excel xong rồi mới dán vào clipboard hoặc dán vào clipboard rồi từ đó dán tạm thời vào notepad rồi mở Excel.

c. Mở Excel –> mở tập tin chan_dai.xlsm –> ta đã thấy có menu mà ta thêm vào. Nhưng nhấn chuột vào thì chưa có gì, thậm chí Excel thông báo lỗi, vì ta chưa viết code để gán cho các nút ta thêm vào.
Nhấn Alt + F11 để mở VBA. Nhấn Insert –> Module để thêm môđun –> Dán code đã lưu vào bộ nhớ đệm ở điểm b (Ctrl + V) –> bổ sung code để phục vụ các nút.
Chọn save để lưu lại chan_dai.xlsm

d. Nếu chan_dai.xlsm ta định làm addin thì ta làm như sau:
Trong cửa sổ VBA ta nhấn vào ThisWorkbook –> chuyển IsAddin sang TRUE –> chọn save –> VBA chỉ cho phép ta lưu lại thành chan_dai.xlam.
chan_dai.xlam ta thêm vào Add_ins để mỗi lần mở Excel là có.

www.giaiphapexcel.com/diendan/threads/v%e1%ba%a5n-%c4%91%e1%bb%81-hi%e1%bb%83n-th%e1%bb%8b-menu-t%e1%bb%b1-t%e1%ba%a1o-trong-excel-2007.36385/#post-454302

Khoá học Trưởng phòng nhân sự
Khóa học SprinGO phù hợp

Khoá học Trưởng phòng nhân sự

Nguồn nhân lực là một trong Tứ trụ kinh doanh của doanh nghiệp, có tác động tới sự tồn tại và phát triển bền...

Xem khóa học
Chia sẻ: