我一直使用vb的,vb比较熟悉,现在想开发一个小vba程序,遇到了下面这个问题:
在vb中我想根据给出的路径打开相应的文件,代码如下:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPrivate Sub Command1_Click()
    Dim strFile As String
    strFile =text1.text
    ShellExecute Me.hwnd, "open", strFile, vbNullString, vbNullString, vbNormalFocus现在我想在VBA中实现文件打开功能,仍然使用上述代码但是却报错!提示没有Me.hwnd对象!查询Me.果然没有hwnd对象!请问这是怎么回事?在vba中到底该如何实现打开任意文件功能?请赐教!

解决方案 »

  1.   

    fso?好象不是这个问题!我在VB里用都没问题,到vba里就有问题了。而且hwnd是句柄吧好象
      

  2.   

    shell函数可以打开doc、pdf、txt任意文件吗?好象不可以吧,只能打开exe之类的可执行程序!!!
      

  3.   

    唉呀,不好意思了。我看错了,你把me.hwnd改为0就可以了Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPrivate Sub Command1_Click()
        Dim strFile As String
        strFile =text1.text
        ShellExecute 0, "open", strFile, vbNullString, vbNullString, vbNormalFocus
      

  4.   

    第一个参数是隶属窗口
    第二个参数用vbNullString就相当于双击打开文件了ShellExecute 0, vbNullString, strFile, vbNullString, vbNullString, vbNormalFocus