Excel VBA: Getting the Path to an Active Workbook

I’ve described a PowerPoint macro and a Word macro to get the path to an active file.
Today, I’ll show you a similar macro on Microsoft Excel.

Macro Example

The following Excel VBA macro shows a message box with the path to an active workbook. (The path does not include the file name.)

Sub GetActiveWorkbookPath()

  MsgBox ActiveWorkbook.Path

End Sub

Description

ActiveWorkbook property

  ActiveWorkbook...

The Application.ActiveWorkbook property returns an active Workbook object.

obj-Excel-Application-Activeworkbook

The ActiveWorkbook property is a member of the Excel.Global class, so we can omit the Application property.

obj-Excel-Global-Activeworkbook

Workbook.Path property

  ActiveWorkbook.Path

The Workbook.Path property (ActiveWorkbook.Path) returns a path to the workbook.

obj-Excel-Workbook-Path

MsgBox function

The code: ActiveWorkbook.Path is set to the parameter of a MsgBox function.

Sub GetActiveWorkbookPath()

  MsgBox ActiveWorkbook.Path

End Sub

So, a message box shows the path to an active workbook.

Properties

http://www.relief.jp/itnote/archives/003427.php

Apply To

  • Excel 2013
  • Excel 2010
  • Excel 2007