Comment get current working directory using vba?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment get current working directory using vba?

Message par ForumBot »

I am using MS Excel 2010 and trying to get the current directory using the below code,


    path = ActiveWorkbook.Path


But ActiveWorkbook.Path returns blank.

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Comment get current working directory using vba?

Message par ayi »

When one opens an Excel document D:\db\tmp\test1.xlsm:





CurDir() returns C:\Users\[username]\Documents





ActiveWorkbook.Path returns D:\db\tmp


So CurDir() has a system default and can be changed.


ActiveWorkbook.Path does not change for the same saved Workbook.


For example, CurDir() changes when you do “File/Save As” command, and select a random directory in the File/Directory selection dialog. Then click on Cancel to skip saving. But CurDir() has already changed to the last selected directory.


[ADD]


Resume VBA for different applications


Access D:\db\tmp\test1.accdb, like duckboy81 commented:




  • CurDir() => C:\Users[username]\Documents




  • Application.CurrentProject.Path => D:\db\tmp




Excel D:\db\tmp\test1.xlsm:




  • CurDir() => C:\Users[username]\Documents




  • ActiveWorkbook.Path => D:\db\tmp




  • Application.DefaultFilePath => C:\Users[username]\Documents




Outlook:




  • CurDir() => C:\WINDOWS\System32




  • Application.Session.Stores(1).Filepath => D:\programdata\Outlook\myOutlookDocX.pst




PowerPoint D:\db\tmp\test1.ppt:




  • CurDir() => C:\Users[username]\Documents




  • ActivePresentation.Path => D:\db\tmp




Word D:\db\tmp\test1.docx:




  • CurDir() => C:\Users[username]\Documents




  • Application.ActiveDocument.Path => D:\db\tmp




  • Application.ActiveDocument.FullName => D:\db\tmp\test1.docx




  • Application.StartupPath => C:\users[username]\appdata\roaming\microsoft\word\startup



Répondre

Revenir à « Excel & VBA »