2016-10-31 2 views
0

経由でファイルをエクセル開くのアドレスを取得しますmsgBoxは何も表示しません。しかし、ファイルを保存した後、MsgBoxは正しいアドレスを表示します。何故ですか?!ファイルを開いた直後にファイルのアドレスを取得するにはどうすればよいですか?はちょうど私は、ファイルをエクセルやワークブックに簡単なマクロを書き、単純な空を作成したVBA

答えて

1

新しく作成されたワークブックにはパスがまだありません。

あなたはそれを保存すると、それは次のようにあなたが

はあなたのサブを変更することができます得ることができます実際のパスを持っています:

Private Sub Workbook_NewSheet(ByVal Sh As Object) 
    If ThisWorkbook.Path = "" Then '<--| if the workbook doesn't have path, yet 
     With Application.FileDialog(msoFileDialogSaveAs) '<--| have the user choose a path and a name 
      .Title = "Select Folder and name" 
      .InitialFileName = "C:\" 
      If .Show = 0 Then Exit Sub '<--| if the user canceled the dialog box then exit sub 
      ThisWorkbook.SaveAs .SelectedItems(1), xlOpenXMLWorkbookMacroEnabled '<--| otherwise save the workbook to the specified path as a macro enabled excel file 
     End With 
    End If 
    MsgBox ThisWorkbook.Path '<--| finally show the workbook path! 
End Sub 
関連する問題