【问题标题】:How to set default directory for Excel's GetOpenFilename using Outlook VBA?如何使用 Outlook VBA 为 Excel 的 GetOpenFilename 设置默认目录?
【发布时间】:2021-06-10 18:42:00
【问题描述】:

我正在尝试为 VBA 函数 GetOpenfilename 设置默认目录。我之前设法让它工作,但在保存之前丢失了代码。

Sub Sample2()
    Dim myFile As Variant
    Dim i As Integer
    Dim myApp As Excel.Application
    Dim strCurDir As String
    Set myApp = New Excel.Application

    ChDrive ("H:\")
    ChDir ("H:\99 - Temp")

    'Open File to search
    myFile = myApp.GetOpenFileName(MultiSelect:=True)

    If myFile <> False Then
        If IsArray(myFile) Then  '<~~ If user selects multiple file
            For i = LBound(myFile) To UBound(myFile)
                Debug.Print myFile(i)
            Next i
        Else '<~~ If user selects single file
            Debug.Print myFile
        End If
    Else
        Exit Sub
    End If

End Sub

我尝试了该代码的几种变体,但发现的帖子都非常旧。它将成为 Outlook 2016 中更大代码的一部分。

【问题讨论】:

标签: excel vba outlook-2016 getopenfilename


【解决方案1】:

改用 Excel 对象的 FileDialog 属性...

Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")

Dim myFile As Variant
With xlApp.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = True
    .ButtonName = "Select"
    .Title = "Select File"
    .InitialFileName = "H:\99 - Temp\"
    If .Show = 0 Then Exit Sub 'user cancelled
    For Each myFile In .SelectedItems
        Debug.Print myFile
    Next myFile
End With

Set xlApp = Nothing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多