VBA:使用带有时间戳

时间:2015-08-04 20:25:15

标签: xml excel vba excel-vba

所以我可以在导出XML的地方工作:

Sub ExportXML()
'
' ExportXML Macro
'
Application.CommandBars.ExecuteMso ("XmlExport")
'
End Sub

这样可以保存具有特定预定义位置(" C:....")和名称(来自Cell A10的值)的实际Excel文档,并带有时间戳:

Sub SaveMyWorkbook()
    Dim vFile As Variant
    Dim sCurrDir As String
    Dim sName As Variant
    Dim strdate As String

    sName = [A10] 'value from A1
    strdate = Format(Now, "dd-mm-yy h-mm-ss")
    ' save current directory, and change to desired starting directory
    sCurrDir = CurDir
    ChDrive "C:\....."
    ChDir "C:\....."

    vFile = Application.GetSaveAsFilename(InitialFileName:=sName & strdate, _
        fileFilter:="Excel files (*.xls), *.xls", _
        Title:="My custom save dialog")

    If vFile <> False Then
        ThisWorkbook.SaveAs Filename:=vFile
    Else
        MsgBox "Not a valid path" 'cancel
    End If
    ' change back to initial current directory
    ChDrive sCurrDir
    ChDir sCurrDir

End Sub

但是我希望能够进行导出并将其保存到特定的预定位置(&#34; C:....&#34;)和名称(来自Cell A10的值)并带有时间戳。有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

So I was able to figure it out. Instead of using a command to hit the ribbon button I found the actual command for exporting the XML.

Sub ExportXML()
'
' ExportXML Macro
'
Dim objMapToExport As XmlMap

 Set objMapToExport = ActiveWorkbook.XmlMaps("DocumentRequests_Map")

 ActiveWorkbook.SaveAsXMLData [c12] & [c9] & Format(Now, "dd-mm-yy h-mm-ss") & ".xml", objMapToExport


'This opens the folder so you can see the saved file
Call Shell("explorer.exe" & " " & [c12], vbNormalFocus)

'
End Sub