Dim varFile As Variant
    Set varFile = CreateObject("Scripting.FileSystemObject")
    If varFile.FileExists(strPath) = True Then
        'file is exists
    End If

解决方案 »

  1.   

    小轩轩:
    我来回答你:
    On Error GoTo errhandler是出错后,就跳到标号为errhandler那行去执行,
    就象basic里的标号一样。
      

  2.   

    去掉空格用trim(pathname),去掉了前面和后面的空格
      

  3.   

    唉.我也正愁~!如果你的那个目录是唯一的.也就是说 如果那个目录下只有  Microsoft office 这样的一个目录.而不是  Microsoft office 和  Microsoft visual studio.  这样的几个差不多的目录.   
    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
      

  4.   

    '强制文件必须存在,并使文件名不允许出现空格
    CommonDialog1.Flags=cdlOFNPathMustExist or cdlOFNNoLongNames
    '判断文件是否存在
    If Dir$(CommonDialog1.FileName) <> "" Then
     Shell ("E:\Microsoft Office\Office\winword.exe " & CommonDialog1.FileName)
      CommonDialog1.FileName = ""
    End If
      

  5.   

    CommonDialog1.Flags=cdlOFNPathMustExist or cdlOFNNoLongNames
    改为
    CommonDialog1.Flags=cdlOFNFileMustExist or cdlOFNNoLongNames
    好一点。可查阅MSDN“CommonDialog 控件”说明。
      

  6.   

    CommonDialog1.Flags=cdlOFNPathMustExist or cdlOFNNoLongNames
    改为
    CommonDialog1.Flags=cdlOFNFileMustExist or cdlOFNNoLongNames
    好一点。可查阅MSDN“CommonDialog 控件”说明。
      

  7.   

    如果有空格,加双引号就可以了
    没有空格,也不影响使用
    例如  filename = """"+filename+""""
      

  8.   

    可能是我说的有点不清楚。第二个问题所说的空格不是WORD的可执行文件的路径中有没有空格,而是用WORD打开的文档的路径中存在空格,而且空格是在每个路径的中间不是两端。如果去掉WORD就找不到文件的正确位置,如果不去掉,VB就不让打开,大家看看怎么办?第一个问题是想利用判断文件是否存在还控制软件是不是可以打开它。前面的解决方法没有看懂!谢谢大家了!
      

  9.   

    加引号!filename = """"+filename+""""
      

  10.   

    加引号!filename = """"+filename+""""
      

  11.   

    加引号!filename = """"+filename+""""
      

  12.   

    同意Chice_wxg(VS.NET = 光驱烤肉,我也入网了) 
    或filename=chr(34)+filename+chr(34)
      

  13.   

    好事做到底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
      

  14.   

    前面漏上个点,对不起。
    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
      

  15.   

    对于SHELL的路径,可以试试用个字符串如:STR1来保存所有的东东然后再shell str1,1
      

  16.   

    对于SHELL的路径,可以试试用个字符串如:STR1来保存所有的东东然后再shell str1,1
      

  17.   

    谢谢你们对我的支持,可能还是我说不不够明确,我所说的空格不是在别的位置,而是在“commondialog1.filename”中包含的。你可以做个窗体,加个命令按钮,把上面的代码复制到commandbotton的单击事件中试试就知道了。还有这里E:\Microsoft  Office\Office\winword.exe 要改成你自己的WORD路径。比如有个文件存放在My Documets目录下,用上面的程序就打不开。大家可以试试。
      

  18.   

    我给你的代码中将
    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这段代码经过测试,绝对没问题。