简体   繁体   English

用括号将这种微妙的VBA代码更改意味着什么?

[英]What does this subtle VBA code change with brackets mean?

The following code opens the default application for a filename: 以下代码打开文件名的默认应用程序:

Public Function OpenDefaultApplication(filename As String)
    Dim sh As Object
    Set sh = CreateObject("Shell.Application")
    sh.Open (filename)
    Set sh = Nothing
End Function

However, removing the brackets from the line opening the file means the file is not opened: 但是,从打开文件的行中除去括号意味着该文件未打开:

    sh.Open filename    ' does not work!

However, if I remove the code from a function, the code works successfully without brackets. 但是,如果我从函数中删除代码,则代码可以成功运行而无需使用方括号。

Dim sh As Object
Set sh = CreateObject("Shell.Application")
sh.Open filename ' works if not inside a function
Set sh = Nothing

What is this subtle different and why do the brackets become necessary inside a function? 这有什么细微的区别?为什么在函数中必须使用括号?

sh.Open filename

is actually the correct syntax. 实际上是正确的语法。
Or alternatively 或者

Call sh.Open(filename)

The original statement 原始声明

sh.Open (filename)

evaluates filename and then passes it to sh.Open . 计算 filename ,然后将其传递给sh.Open
If you tried this syntax with a method that has two parameters: 如果您使用具有两个参数的方法尝试了此语法:

sh.SomeMethod (filename, parameter2)

you'd get a syntax error. 您会收到语法错误。

So the question is: 所以问题是:
What do you pass as filename to the first function, and what is filename in the second code example? 您将什么作为filename传递给第一个函数,第二个代码示例中的filename是什么?

EDIT 编辑
For more clarification, add this to the beginning of OpenDefaultApplication : 为了进一步说明,请将其添加到OpenDefaultApplication的开头:

Debug.Print filename
Debug.Print (filename)

and see the result in the Immediate Window (Ctrl+G). 并在立即窗口(Ctrl + G)中查看结果。

Well, that was interesting. 好吧,那很有趣。

You've hit a very odd behavior of the Shell.Open method. 您已经遇到了Shell.Open方法的非常奇怪的行为。

Shell.Open(ByVal vDir As Variant) As Integer

Note that the parameter is defined as Variant, not String. 请注意,该参数定义为Variant,而不是String。
That's because you can also do eg 那是因为你也可以做

sh.Open 36

to open the Windows folder. 打开Windows文件夹。

Apparently because of this, sh.Open filename doesn't work (though it's the correct syntax), since filename is passed as Reference. 显然是因为这个原因, sh.Open filename不起作用(尽管它是正确的语法),因为文件名作为引用传递。

sh.Open (filename) works, but is a rather ugly hack. sh.Open (filename)可以工作,但是非常难看。

Better solutions: 更好的解决方案:

1) Declare filename as Variant, if you insist on using Shell.Open: 1)如果坚持使用Shell.Open,则将filename声明为Variant。

Public Sub OpenDefaultApplication(filename As Variant)
    Dim sh As Object
    Set sh = CreateObject("Shell.Application")
    sh.Open filename
End Sub

2) Use the correct method to open files, Shell.ShellExecute 2)使用正确的方法打开文件Shell.ShellExecute

Public Sub OpenDefaultApplication(filename As String)
    Dim sh As Object
    Set sh = CreateObject("Shell.Application")
    sh.ShellExecute filename
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM