VBA中操作XML


OFFICE2007之后使用了OpenXml標准(偉大的改變),定制文本級的Ribbon可以通過修改壓縮包內的xml文件來實現。

    先學習一下VBA中操作XML的方法

    先引用Microsoft XML V6.0,對應的文件是msxml6.dll。前期綁定,方便使用智能提示。

    一、DOM方式創建一個XML文件:內容是自定義Ribbon的一個簡單框架。文件保存在桌面。

 1 Sub CreateXmlFile()
 2 
 3    '創建文檔對象模型
 4    Dim xmldoc As New DOMDocument
 5    
 6    '創建根節點,XML文檔有且必須僅有一個根節點。
 7    Dim root As IXMLDOMElement
 8    'http://schemas.microsoft.com/office/2006/01/customui  2007版本使用的命名空間
 9    'http://schemas.microsoft.com/office/2009/07/customui  2010版本使用的命名空間
10         Set root = xmldoc.createElement("customUI")
11             root.setAttribute "xmlns", "http://schemas.microsoft.com/office/2009/07/customui"
12         Set xmldoc.DocumentElement = root       '指定根節點
13    
14    'ribbon元素
15    Dim xmlribbon As IXMLDOMElement
16         Set xmlribbon = xmldoc.createElement("ribbon")
17 root.appendChild xmlribbon 18 19 'tabs元素 20 Dim xmltabs As IXMLDOMElement 21 Set xmltabs = xmldoc.createElement("tabs") 22 xmlribbon.appendChild xmltabs 23 24 'tab元素 25 Dim xmltab As IXMLDOMElement 26 Set xmltab = xmldoc.createElement("tab") 27 xmltab.setAttribute "id", "CustomTab" 28 xmltab.setAttribute "label", "自定義標簽" 29 xmltabs.appendChild xmltab 30 31 'group元素 32 Dim xmlgroup As IXMLDOMElement 33 Set xmlgroup = xmldoc.createElement("group") 34 xmlgroup.setAttribute "id", "CustomGroup" 35 xmlgroup.setAttribute "label", "自定義分組" 36 xmltab.appendChild xmlgroup 37 38 'button元素,並設置button的屬性 39 Dim xmlbutton As IXMLDOMElement 40 Set xmlbutton = xmldoc.createElement("button") 41 xmlbutton.setAttribute "id", "btn" 42 xmlbutton.setAttribute "label", "插入公司名稱" 43 xmlbutton.setAttribute "size", "large" 44 xmlbutton.setAttribute "onAction", "InsertCompanyName" 45 xmlgroup.appendChild xmlbutton 46 47 '文件保存到桌面 48 xmldoc.Save ("C:\Users\stone\Desktop\1.xml") 49 End Sub

 下面是創建出來xml文件內容。創建出的文件標簽不能自動換行。只好手工排版。 

 1 <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
 2     <ribbon>
 3         <tabs>
 4             <tab id="CustomTab" label="自定義標簽">
 5                 <group id="CustomGroup" label="自定義分組">
 6                     <button id="btn" label="插入公司名稱" size="large" onAction="InsertCompanyName"/>
 7                 </group>
 8             </tab>
 9         </tabs>
10     </ribbon>
11 </customUI>

二、對已有的XML文件添加新的元素:給group添加一個menu元素,menu元素下添加兩個button子元素

 1 Sub AppendXmlFile()
 2 
 3     Dim xmldoc As New DOMDocument
 4     
 5     Dim b As Boolean
 6     '加載xml文件,成功返回true
 7     b = xmldoc.Load("C:\Users\stone\Desktop\1.xml")
 8     If Not b Then Exit Sub
 9 
10 '    Dim xmlgroups As IXMLDOMNodeList
11 '    Dim xmlgroup As IXMLDOMElement
12 '    Set xmlgroups = xmldoc.getElementsByTagName("group")     '所有的group元素,返回一個集合
13 '    Set xmlgroup = xmlgroups(0)                              '取集合中第一個group元素
14      
15     Dim xmlgroup As IXMLDOMNode
16     'Xpath  選取第一個group節點
17     Set xmlgroup = xmldoc.SelectSingleNode("//tab[@id='CustomTab']/group[0]")
18                  
19     'menu元素
20     '為了避免產生xmlns="",要添加的節點的namespaceURI需要和父節點一致。下同
21     Dim xmlmenu As IXMLDOMElement
22     Set xmlmenu = xmldoc.createNode(NODE_ELEMENT, "menu", xmlgroup.NamespaceURI)
23         xmlmenu.setAttribute "id", "CustomMenu"
24         xmlgroup.appendChild xmlmenu
25     
26     'button元素
27     Dim xmlbutton1 As IXMLDOMElement
28     Set xmlbutton1 = xmldoc.createNode(NODE_ELEMENT, "button", xmlmenu.NamespaceURI)
29         xmlbutton1.setAttribute "id", "btn1"
30         xmlbutton1.setAttribute "label", "按鈕一"
31         
32     Dim xmlbutton2 As IXMLDOMElement
33     Set xmlbutton2 = xmldoc.createNode(NODE_ELEMENT, "button", xmlmenu.NamespaceURI)
34         xmlbutton2.setAttribute "id", "btn2"
35         xmlbutton2.setAttribute "label", "按鈕二"
36         
37     xmlmenu.appendChild xmlbutton1
38     xmlmenu.appendChild xmlbutton2
39         
40     Debug.Print xmldoc.XML
41     xmldoc.Save ("C:\Users\stone\Desktop\1.xml")
42     
43 End Sub

下面是添加新節點后的XML文件內容

 1 <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
 2     <ribbon>
 3         <tabs>
 4             <tab id="CustomTab" label="自定義標簽">
 5                 <group id="CustomGroup" label="自定義分組">
 6                     <button id="btn" label="插入公司名稱" size="large" onAction="InsertCompanyName"/>
 7                     <menu id="CustomMenu">
 8                         <button id="btn1" label="按鈕一"/>
 9                         <button id="btn2" label="按鈕二"/>
10                     </menu>
11                 </group>
12             </tab>
13         </tabs>
14     </ribbon>
15 </customUI>

 一個小知識點:

創建子節點時,如果沒有指名namespaceURI,在appendchild后,子節點帶一個xmlns=""的屬性。如果創建子節點時指定namespaceURI等於父節點的URI可以屏蔽這個麻煩,就不會生成xmlns=""了。

DOMDocument的兩個方法:

Function selectNodes(queryString As String) As IXMLDOMNodeList

Function selectSingleNode(queryString As String) As IXMLDOMNode

queryString是一個Xpath表達式。

Xpath語法參考W3C網站:http://www.w3school.com.cn/xpath/xpath_syntax.asp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM