Dim varFile As Variant
Set varFile = CreateObject("Scripting.FileSystemObject")
If varFile.FileExists(strPath) = True Then
'file is exists
End If
Set varFile = CreateObject("Scripting.FileSystemObject")
If varFile.FileExists(strPath) = True Then
'file is exists
End If
我来回答你:
On Error GoTo errhandler是出错后,就跳到标号为errhandler那行去执行,
就象basic里的标号一样。
c:\Microsoft office\word.exe那样我还有办法.
你可以这样
a=strpath
b=split(a,"\")
newpath=""
for n=0 to ubound(a)
if instr(1,b(n)," ")>0 then '目录名有空格
b(n)=left(b(n),6) & "~1" '把 Microsoft office改成 Micros~1 .
end if
if n=ubound(a) then '是最后的文件名
newpath=newpath & b(n)
else
newpath= newpath & b(n) & "\"
end if
next这个 newpath 就是可执行的全路径了.
也就是把 C:\microsoft office\word.exe 换成 c:\micros~1\word.exe
CommonDialog1.Flags=cdlOFNPathMustExist or cdlOFNNoLongNames
'判断文件是否存在
If Dir$(CommonDialog1.FileName) <> "" Then
Shell ("E:\Microsoft Office\Office\winword.exe " & CommonDialog1.FileName)
CommonDialog1.FileName = ""
End If
改为
CommonDialog1.Flags=cdlOFNFileMustExist or cdlOFNNoLongNames
好一点。可查阅MSDN“CommonDialog 控件”说明。
改为
CommonDialog1.Flags=cdlOFNFileMustExist or cdlOFNNoLongNames
好一点。可查阅MSDN“CommonDialog 控件”说明。
没有空格,也不影响使用
例如 filename = """"+filename+""""
或filename=chr(34)+filename+chr(34)
With CommonDialog1
.CancelError = True
On Error GoTo errhandler
Flags = &H200000 Or &H1000 Or &H4
.Filter = "word文档(*.doc)|*.doc|所有文件(*.*)|*.*"
.ShowOpen
End WithIf CommonDialog1.FileName <> "" Then
Shell ("E:\Microsoft Office\Office\winword.exe " & """" CommonDialog1.FileName & """")
CommonDialog1.FileName = ""
End If
errhandler:
End Sub
Private Sub Command1_Click()
With CommonDialog1
.CancelError = True
On Error GoTo errhandler
.Flags = &H200000 Or &H1000 Or &H4
.Filter = "word文档(*.doc)|*.doc|所有文件(*.*)|*.*"
.ShowOpen
End WithIf CommonDialog1.FileName <> "" Then
Shell ("E:\Microsoft Office\Office\winword.exe " & """" CommonDialog1.FileName & """")
CommonDialog1.FileName = ""
End If
errhandler:
End Sub
Shell ("E:\Microsoft Office\Office\winword.exe " & """" CommonDialog1.FileName & """")改为
Shell "E:\Microsoft Office\Office\winword.exe " & """" CommonDialog1.FileName & """",vbNormalFocus应该可以了,前面没看清你的代码,对不起。在不知道WORD安装路径的情况下可以用以下代码:
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 Long
Const SW_SHOWNORMAL = 1Private Sub Command1_Click()
With CommonDialog1
.CancelError = True
.Flags = &H4 Or &H200000 Or &H1000
.Filter = "word文档(*.doc)|*.doc|所有文件(*.*)|*.*"
On Error GoTo ErrHandle
.ShowOpen
End WithShellExecute Me.hwnd, vbNullString, CommonDialog1.FileName, vbNullString, "C:\", SW_SHOWNORMALErrHandle:
CommonDialog1.FileName = ""
End Sub这段代码经过测试,绝对没问题。