2017-11-17 8 views
0

ダイアログボックスから偽のファイルを選択しています。残りのコードはうまくいきますが、ファイルを選択しないと、VBAから次のエラーメッセージが表示されます。1004 False.xlsxが見つかりませんでした。そのエラーメッセージがポップアップしないようにコードを管理する方法を知っていますか?ダイアログボックスから誤った選択を処理する

Sub OpeningExcelFile2() 
    Dim Finfo As String 
    Dim FilterIndex As Integer 
    Dim Title As String 
    Dim Filename As Variant 
    Dim wb As Workbook 
    Dim objWdApp As Object 
    Dim objWdDoc As Object 



    'Setup the list of file filters 
    Finfo = "Excel Files (*.xlsx),*xlsx," & _ 
      "Macro-Enable Worksheet (*.xlsm),*xlsm," & _ 
      "Word Files (*.docx),*.docx," & _ 
      "All Files (*.*),*.*" 
      MultiSelect = True 


    'Display *.* by default 
    FilterIndex = 4 

    'Set the dialog box caption 
    Title = "Select a File to Open" 

    'Get the Filename 
    Filename = Application.GetOpenFilename(Finfo, _ 
     FilterIndex, Title) 

    'Handle return info from dialog box 

    If Filename = False Then 
      MsgBox "No file was selected." 
    Else 
     MsgBox "You selected " & Filename 
    End If 

    If InStr(1, Filename, ".docx", vbTextCompare) > 0 Then 
     Set objWdApp = CreateObject("Word.Application") 
     objWdApp.Visible = True 
     Set objWdDoc = objWdApp.Documents.Open(Filename) '\\ Open Word Document 

    Else 
     Set wb = Workbooks.Open(Filename) '\\ Open Excel Spreadsheet 

    'how to bring application excel to the front like word in this code? 

    End If 


End Sub 

答えて

0

ファイルが選択されていない場合は、サブルーチン終了:

If Filename = False Then 
    MsgBox "No file was selected." 
    Exit Sub 
Else 
    MsgBox "You selected " & Filename 
End If 
+0

単に最高でした。どうもありがとうございました。コードは今本当に素晴らしいです! – Sergio

関連する問題