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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23539852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:03:09  来源:igfitidea点击:

VBA to open explorer and select, not open, a selected file name

excel-vbaselectexplorervbaexcel

提问by user3572958

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:\用户名\文档\工作计划\文档.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?

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

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

回答by GlennFromIowa

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 quotesin the string above, both before D:and after ActiveCell.Value(concatenated to the end of the string) puts a double quotechar before and after the path.

请注意,任何包含空格或其他特殊字符的路径都需要引号。的一倍引号字符串中的上方,两个前D:和后ActiveCell.Value(级联到字符串的末尾)把一个双引号之前和路径之后炭。

回答by id11

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