2015-11-05 67 views
3

我正在嘗試使用Excel VBA創建自定義XML映射。我的XML文件是BookData.xml創建自定義XML映射

<?xml version='1.0'?> 
<BookInfo> 
    <Book> 
     <ISBN>989-0-487-04641-2</ISBN> 
     <Title>My World</Title> 
     <Author>Nancy Davolio</Author> 
     <Quantity>121</Quantity> 
    </Book> 
    <Book> 
     <ISBN>981-0-776-05541-0</ISBN> 
     <Title>Get Connected</Title> 
     <Author>Janet Leverling</Author> 
     <Quantity>435</Quantity> 
    </Book> 
    <Book> 
     <ISBN>999-1-543-02345-2</ISBN> 
     <Title>Honesty</Title> 
     <Author>Robert Fuller</Author> 
     <Quantity>315</Quantity> 
    </Book> 
</BookInfo> 

,並在Excel 2013我的VBA代碼:

Sub Create_XSD() 
    Dim StrMyXml As String, MyMap As XmlMap 
    Dim StrMySchema As String 
    StrMyXml = "<BookInfo>" 
    StrMyXml = StrMyXml & "<Book>" 
    StrMyXml = StrMyXml & "<ISBN>Text</ISBN>" 
    StrMyXml = StrMyXml & "<Title>Text</Title>" 
    StrMyXml = StrMyXml & "<Author>Text</Author>" 
    StrMyXml = StrMyXml & "<Quantity>999</Quantity>" 
    StrMyXml = StrMyXml & "</Book>" 
    StrMyXml = StrMyXml & "<Book></Book>" 
    StrMyXml = StrMyXml & "</ BookInfo >" 

    ' Turn off async loading. 
    Application.DisplayAlerts = False 
    ' Add the string to the XmlMaps collection. 
    Set MyMap = ThisWorkbook.XmlMaps.Add(StrMyXml) 
    Application.DisplayAlerts = True 

    ' Create an empty file and output the schema. 
    StrMySchema = ThisWorkbook.XmlMaps(1).Schemas(1).XML 
    Open "C:\Documents\MySchema.xsd" For Output As #1 
    Print #1, StrMySchema 
    Close #1 
End Sub 

這是返回錯誤:

Error

在線:

Set MyMap = ThisWorkbook.XmlMaps.Add(StrMyXml) 

我想了解如何使用VBA構建自定義XML映射的邏輯,以便我可以在另一個(更復雜的)XML文件上使用它。我遵循的代碼是here

那麼,如何解決這個錯誤呢?

+1

刪除'「」' – BruceWayne

+0

中的空格@BruceWayne看起來像MSDN dox需要更新:) –

回答

1

"</ BookInfo >"

+0

賓果!非常感謝,我沒有注意到這些空間.... –

1

刪除空格,您必須刪除第2行(後昏暗StrMyXml作爲字符串)逗號,並把該行的其餘部分在它自己的一行命令昏暗在前面的。
它應該是這樣的:

Dim StrMyXml As String

Dim MyMap As XmlMap

0

我也遇到了同樣的錯誤,並BruceWayne的反應是快速正確的!

我在< /的BookInfo >(12線) 而且在<的BookInfo >(第4行)

僅在< /的BookInfo >卸下除去空間是不充分的。