从对话框获取文件名

时间:2020-05-27 19:24:22

标签: excel vba

我正在尝试使用另一个Excel文件读取XML文件并修改XML中的日期,并对这个Excel文件进​​行vlookup。覆盖日期之后,我要保存XML文件。

但是打开Excel File时出现错误MisMatch类型13错误。我玩过暗淡的类型,但似乎没有任何作用。有人可以帮忙吗?

代码如下:

Function GetFile() As String
    Application.ScreenUpdating = False

'**Open XML'**

    Dim XML_in As Variant
    XML_in = Application.GetOpenFilename(FileFilter:="XML (*.XML), *.XML", Title:="Select XML Order List")
    If XML_in = False Then Exit Sub
    GetFile = XML_in
    Set oXMLFile = CreateObject("Msxml2.DOMDocument.3.0") 
    XMLFileName = XML_in 'XML IN Filename'
    Debug.Print XML_in

'**Manipulate oXML'**

    oXMLFile.Load (XMLFileName) 'Read XML'
    Set xmlGroup = oXMLFile.SelectNodes("/ppr:pprdata/ppr:Group") 'Navigate to ppr:Group'
    Set xmlOrderLists = xmlGroup(1) 'Navigate to <ppr:Group name="Order lists">'
    Set xmlProdPgm = xmlOrderLists.SelectNodes("ppr:ProductionProgram")
    Set xmlFirmOrders = xmlProdPgm(0).ChildNodes

'**Open AMS File'**

    Dim src As Variant '
    Set src = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx", Title:="Open AMS File")
    If src = False Then Exit Sub
    'Workbooks.Open("AMS Schedule example.xlsx", True, True)
    Set Sheet = src.Worksheets("Sheet1")
    Set ran = Sheet.range("A1:B9")

    For i = 1 To xmlFirmOrders. Length - 1 
        Debug.Print xmlFirmOrders(i).Attributes.getNamedItem("ppr:id").Value
        Set xmlNumber = xmlFirmOrders(i).SelectSingleNode("ppr:number")
        Set xmlStartTime = xmlFirmOrders(i).SelectSingleNode("ppr:startTime")
        xmlStartTime.Text = Application.WorksheetFunction.IfError(Application.WorksheetFunction.vlookup(xmlNumber, ran, 2, 0), "NOTFOUND") 'Update ppr:Starttime'
    Next i

'**Close AMS File**

    src.Close False
    Set src = Nothing

'**Save XML'**

    Dim XML_out As Variant
    XML_out = Application.GetSaveAsFilename(FileFilter:="XML (*.XML), *.XML", Title:="Output XML File for Avix")
    If XML_out = False Then Exit Sub
    GetFile = XML_out
    oXMLFile.Save XML_out
    Debug.Print XML_out

    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Function

1 个答案:

答案 0 :(得分:0)

我将使用以下代码。首先获取FileName作为字符串,该字符串使用Workbooks.Open打开文件。

 Dim src As String
 Dim wb As Workbook
 src = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx", Title:="Open AMS File")
 Set wb = Workbooks.Open(src)