【问题标题】:Excel FilePicker dialogbox in Outlook code opens in backgroundOutlook 代码中的 Excel FilePicker 对话框在后台打开
【发布时间】:2019-11-06 12:59:36
【问题描述】:

我开发了一个 Outlook 宏,让用户可以选择并打开 Excel 文件。

文件对话框在后台打开。在用户看来,Outlook 应用程序似乎被卡住了,而它只是在等待用户在所有其他窗口后面的 Filepicker 对话框中选择一个文件。

Dim SigFolder2 As String
Dim fd As Office.FileDialog
Dim selectedItem As Variant
Dim ExcelFileName As String
Dim FileName As String
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook

'Suggested Folder--Downloads
SigFolder2 = "C:\Users\" & Environ("UserName") & "\Downloads\"

'Dialog Settings

Set fd = objExcel.FileDialog(msoFileDialogFilePicker)
With fd
    .Filters.Clear
    .InitialFileName = SigFolder2
    .AllowMultiSelect = False
    .Title = "Select Signature File"
End With

'Getting the file
If fd.Show = -1 Then
    For Each selectedItem In fd.SelectedItems
        SigFolder = selectedItem
    Next
Else
    Exit Sub
End If

ExcelFileName = SigFolder
FileName = Left(fso.GetFileName(ExcelFileName), InStr(fso.GetFileName(ExcelFileName), ".") - 1)
Debug.Print ExcelFileName 'file Path with filename

有什么办法可以改正吗?

【问题讨论】:

    标签: excel vba outlook filepicker


    【解决方案1】:

    这个问题是由于 Excel 窗口对 Outlook 窗口一无所知。如果您想让它始终位于另一个窗口的顶部,则必须为您的子窗口对话框设置一个父窗口。例如:

    Public Declare Function SetForegroundWindow _
    Lib "user32" (ByVal hwnd As Long) As Long
    
    Public Sub Bring_to_front()
        Dim setFocus As Long
    
        ThisWorkbook.Worksheets("Sheet1").Activate
        setfocus = SetForegroundWindow(Application.hwnd)
    End Sub
    

    在您的情况下,它将是一个对话窗口:

    Private Declare Function FindWindowA Lib "user32" (ByVal class As String, ByVal caption As String) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal win As Long) As Long
    
    Dim hxl As Long
    
    Set objExcel = New Excel.Application
    Set fd = xl.FileDialog(msoFileDialogFilePicker)
    hxl = FindWindowA("XLMAIN", "Excel")
    If (hxl <> 0) Then
        res = SetForegroundWindow(hxl)
    End If
    With fd
    .Filters.Clear
     .InitialFileName = SigFolder2
     .AllowMultiSelect = False
     .Title = "Select Signature File"
    End With
    res = fd.Show
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2012-09-24
      • 1970-01-01
      • 2015-03-05
      相关资源
      最近更新 更多