Copy/Paste file path in already open file dialog window (3rd party) with VBA

谁说胖子不能爱 提交于 2021-02-08 10:16:48

问题


My native language isn't English and I am relativly new to VBA, so I apologize in advance. My problem is the following: A part of my VBA code trys to copy and paste a file path from the clipboard that was defined in a string variable before. I do have a solution but that consists of using "Sendkeys" - not quite content with that one even if it works

Is there an alternative way to Sendkeys ? Like for example to find and activate the already open file dialog, specifically the input field within, and set the string variable directly into that field ? (I hope the attached picture makes it clearer what I imagine)

Screenshot


回答1:


see if following VB code, making use of Powershell, can be of any help to you..

Sub OpenFileUsingPowershell()
 
Dim Powershell_Command, Your_Code As Variant
Dim File_Path, File_Path_with_Spaces As String

File_Path_with_Spaces = "D:\Test Path\Test 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

Note : In the above code try using Powershell_Command = Shell("POWERSHELL.exe -noexit " & Code, 1) instead of Powershell_Command = Shell("POWERSHELL.exe " & Your_Code, 0) just to see what's happening !



来源:https://stackoverflow.com/questions/63318815/copy-paste-file-path-in-already-open-file-dialog-window-3rd-party-with-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!