VBA打开资源管理器并选择,而不是打开选定的文件名

时间:2023-01-17 08:44:47

I want a macro to open windows explorer and just select a file but don’t want it to open the file. I’ve got a list of document names in excel split into some variables. I also included some hyperlinks in it, so you can directly open the selected file. But now I want a macro that selects the corresponding file in explorer and just selects it. All files are in a predefined location, but all filenames are different, off course. Example; D:\username\Documents\workplans\document.001.1.xls D:\username\Documents\workplans\document.002.2.xls D:\username\Documents\workplans\document.003.3.xls

我想要一个宏来打开Windows资源管理器,只需选择一个文件,但不希望它打开该文件。我在excel中有一个文档名列表,分成了一些变量。我还在其中包含了一些超链接,因此您可以直接打开所选文件。但现在我想要一个宏来选择资源管理器中的相应文件并选择它。所有文件都在预定义的位置,但所有文件名都不同,当然。例; D:\ username \ Documents \ workplans \ document.001.1.xls D:\ username \ Documents \ workplans \ document.002.2.xls D:\ username \ Documents \ workplans \ document.003.3.xls

I want to select the corresponding file name in excel, and start the macro to select it in explorer. So for example I select cell D3 and start the macro so it opens explorer and go’s to the following address and selects the file; D:\username\Documents\workplans\document.002.2.xls

我想在excel中选择相应的文件名,然后启动宏以在资源管理器中选择它。因此,例如,我选择单元格D3并启动宏,以便打开资源管理器并转到以下地址并选择文件; d:\用户名\文档\工作计划\ document.002.2.xls

      A                 B             C             D                 E
 1. var 1             var 2          var 3      doc. Name          Hyperlink
 2. document            1             1     document.001.1.xls  document.001.1
 3. document            2             2     document.002.2.xls  document.002.2
 4. document            3             3     document.003.3.xls  document.003.3

If I use the following code directly to the link it works like how I want it to be, but the file name is variable.

如果我直接使用以下代码链接它的工作方式就像我想要的那样,但文件名是可变的。

Sub open_explorer()
Shell "C:\Windows\explorer.exe /select,D:\username\Documents\workplans\document.002.2.xls", vbMaximizedFocus
End Sub

I adjusted the code, but it won’t work. I think the problem is in the (& range (activeCell.select)). How do I get this to work?

我调整了代码,但它不起作用。我认为问题出在(&range(activeCell.select))中。我如何让它工作?

Sub open_explorer()
Shell "C:\Windows\explorer.exe /select, D:\username\Documents\workplans\ &Range ActiveCell.Select", vbMaximizedFocus
End Sub

2 个解决方案

#1


8  

Give this a try:

尝试一下:

Sub open_explorer()
Shell "C:\Windows\explorer.exe /select, ""D:\username\Documents\workplans\" _
    & ActiveCell.Value & """", vbMaximizedFocus
End Sub

Note that quotes are required around any path that has spaces or other special characters. The doubled quotes in the string above, both before D: and after ActiveCell.Value (concatenated to the end of the string) puts a double quote char before and after the path.

请注意,任何包含空格或其他特殊字符的路径都需要引号。上面的字符串中的加倍引号,在D:之前和ActiveCell.Value之后(连接到字符串的结尾)在路径之前和之后放置双引号char。

#2


0  

The above only worked for me if I add an extension to the filename such as ".xls".

如果我在文件名中添加一个扩展名,例如“.xls”,上面只对我有用。

Sub open_explorer()
Shell "C:\Windows\explorer.exe /select, D:\username\Documents\workplans\" & ActiveCell.Value & ".xls", vbMaximizedFocus
End Sub

#1


8  

Give this a try:

尝试一下:

Sub open_explorer()
Shell "C:\Windows\explorer.exe /select, ""D:\username\Documents\workplans\" _
    & ActiveCell.Value & """", vbMaximizedFocus
End Sub

Note that quotes are required around any path that has spaces or other special characters. The doubled quotes in the string above, both before D: and after ActiveCell.Value (concatenated to the end of the string) puts a double quote char before and after the path.

请注意,任何包含空格或其他特殊字符的路径都需要引号。上面的字符串中的加倍引号,在D:之前和ActiveCell.Value之后(连接到字符串的结尾)在路径之前和之后放置双引号char。

#2


0  

The above only worked for me if I add an extension to the filename such as ".xls".

如果我在文件名中添加一个扩展名,例如“.xls”,上面只对我有用。

Sub open_explorer()
Shell "C:\Windows\explorer.exe /select, D:\username\Documents\workplans\" & ActiveCell.Value & ".xls", vbMaximizedFocus
End Sub