简体   繁体   English

使用VBA在VBA中通过xslt将XML导入MS Access

[英]Using VBA to Import XML into MS Access with xslt within VBA

I am trying to import an XML file into MS-Access. 我正在尝试将XML文件导入MS-Access。 At this point, it is a saved importmethod where the XML file is transformed by a XSLT file. 至此,这是一个保存的导入方法,其中XML文件由XSLT文件转换。 By clicking in a form, a Macro calls the importmethod. 通过单击表单,宏将调用importmethod。

I want to make this into VBA and the goal is to have also the XSLT file written in the VBA. 我想将其放入VBA,目标是也将XSLT文件写入VBA。 This is to have no dependencies on separate files and no risk of someone removing the XSLT file and Access no longer importing the file. 这样就不必依赖单独的文件,也不必冒着删除XSLT文件和Access不再导入该文件的风险。

I have found code from similar stackoverflow questions that show how to do part of my question, but those refer to a separate XSLT file. 我从类似的stackoverflow问题中找到了代码,这些代码显示了如何解决部分问题,但这些代码引用的是单独的XSLT文件。

Can someone help me put the XSLT into the VBA-coda 有人可以帮我把XSLT放入VBA尾声

Pasting the XSLT code into the xslDoc.Load results in errors. 将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

I was stuck thing in the wrong direction. 我陷入了错误的方向。 I have changed my code to first printing the xsl-file in a specific folder using 我将代码更改为首先使用以下命令在特定文件夹中打印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

Than the macro still calls the saved importmethod. 比宏仍然调用保存的importmethod。

Than the macro calls a function to delete the XSL-file if found: 比宏调用一个函数删除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

This way i'm sure no-one will remove the xsl-file accidently and I don't need to include the Microsoft v XML3.0. 这样,我确定没有人会意外删除xsl文件,并且不需要包含Microsoft v XML3.0。

Kind regards 亲切的问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM