[英]Using VBA to Import XML into MS Access with xslt within VBA
我正在嘗試將XML文件導入MS-Access。 至此,這是一個保存的導入方法,其中XML文件由XSLT文件轉換。 通過單擊表單,宏將調用importmethod。
我想將其放入VBA,目標是也將XSLT文件寫入VBA。 這樣就不必依賴單獨的文件,也不必冒着刪除XSLT文件和Access不再導入該文件的風險。
我從類似的stackoverflow問題中找到了代碼,這些代碼顯示了如何解決部分問題,但這些代碼引用的是單獨的XSLT文件。
有人可以幫我把XSLT放入VBA尾聲
將XSLT代碼粘貼到xslDoc.Load中會導致錯誤。
Public Sub TransformAndImportXML()
' INCLUDE Microsoft XML, v3.0 REFERENCE
Dim xmlDoc As New MSXML2.DOMDocument, xslDoc As New MSXML2.DOMDocument, newDoc As New MSXML2.DOMDocument
xmlDoc.Load "C:\Path\To\XML\Input.xml"
xslDoc.Load "C:\Path\To\XML\File.xsl"
xmlDoc.transformNodeToObject xslDoc, newDoc
newDoc.Save "C:\Path\To\XML\Output.xml"
Set xmlDoc = Nothing: Set xslDoc = Nothing: Set newDoc = Nothing
Application.ImportXML "C:\Path\To\XML\Output.xml"
MsgBox "Successfully transformed and imported XML!", vbInformation
End Sub
我陷入了錯誤的方向。 我將代碼更改為首先使用以下命令在特定文件夾中打印xsl文件
Public Function CreateXSLFile()
Dim XSLDirectory As String
Dim XSLFile As String
XSLDirectory = "XXXXX"
XSLFile = XSLDirectory & "\YYYY.xsl"
Open XSLFile For Output As #1
Print #1, "<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"">"
Print #1, "<xsl:output method=""xml"" version=""1.0"" encoding=""UTF-8"" indent=""yes"" />"
Print #1, ""
...
Close #1
End Function
比宏仍然調用保存的importmethod。
比宏調用一個函數刪除XSL文件(如果找到):
Public Function RemoveXSLFile()
Dim XSLDirectory As String
Dim XSLFile As String
XSLDirectory = "XXXXX"
XSLFile = XSLDirectory & "\YYYY.xsl"
If Len(Dir(XSLFile)) > 0 Then
SetAttr XSLFile, vbNormal
Kill XSLFile
End If
End Function
這樣,我確定沒有人會意外刪除xsl文件,並且不需要包含Microsoft v XML3.0。
親切的問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.