使用VBA在已打开的文件对话框窗口(第三方)中复制/粘贴文件路径



我的母语不是英语,而且我对VBA还比较陌生,所以我提前道歉。我的问题如下:我的VBA代码的一部分试图从剪贴板复制和粘贴以前在字符串变量中定义的文件路径。我确实有一个解决方案,但它包括使用";发送密钥"-即使工作,也不太满意

有没有Sendkeys的替代方法?例如,查找并激活已打开的文件对话框,特别是其中的输入字段,并将字符串变量直接设置到该字段中?(我希望随附的图片能比我想象的更清晰(

屏幕截图

看看下面的VB代码,使用Powershell,是否对您有任何帮助。。

Sub OpenFileUsingPowershell()

Dim Powershell_Command, Your_Code As Variant
Dim File_Path, File_Path_with_Spaces As String
File_Path_with_Spaces = "D:Test PathTest File.xlsx"    'use file path you have copied from clipboard
File_Path = Replace(File_Path_with_Spaces, " ", Chr(39) & " " & Chr(39))
Your_Code = "Add-Type -AssemblyName System.Windows.Forms ; $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{" & Chr(13) & _
"InitialDirectory = [Environment]::GetFolderPath('Desktop')" & Chr(13) & _
"Filter = 'SpreadSheet (*.xlsx)|*.xlsx'" & Chr(13) & _
"FileName=" & File_Path & "}" & Chr(13) & "$null = $FileBrowser.ShowDialog()"
Powershell_Command = Shell("POWERSHELL.exe " & Your_Code, 0)   'See Note below.

End Sub

注意:在上面的代码中,尝试使用Powershell_Command = Shell("POWERSHELL.exe -noexit " & Code, 1)而不是Powershell_Command = Shell("POWERSHELL.exe " & Your_Code, 0),看看发生了什么!

最新更新