0

I have a word document with some vba code. I want to return the path of the directory in which the word document is saved.

This is the code I used:

Dim PathCurrentDocument As Variant
PathCurrentDocument = ActiveDocument.path
Debug.Print PathCurrentDocument

The desired output is:

"C:\Users\firstname.lastname\OneDrive - company name\VBA\4_update_documents_with_vba_inside_word\document_templates_to_modify"

What I am getting, is the web link, which looks like this:

https://my-company.sharepoint.com/personal/my_name/Documents/VBA/4_update_documents_with_vba_inside_word/Document_A_Template_with_macro.docm

The microsoft documentation only says the property returns the disk or web path to the document. It does not say how to choose which one to return.

Maciej Los
  • 8,468
  • 1
  • 20
  • 35
Leon K.
  • 1
  • 2
  • Document.FullName also returns the web path – Leon K. Jul 09 '20 at 08:51
  • Path (FullName) returns the real path. Why (or how) it should return the path in which document is not saved? If you want to save the document in another location, you have to set specific directory. – Maciej Los Jul 09 '20 at 08:56
  • It does return the real path where the document is saved, just not in the correct format. I want the path as a disk path, but it gets returned as a web path. – Leon K. Jul 09 '20 at 09:40
  • Just because the path isn't what you would like it to be doesn't mean that it isn't correct. You will always get either the path that the document is opened from, or the path it has been saved to. If a document is opened from a local disk or network drive you will get a disk path. If it was opened from OneDrive or SharePoint you will get a web path. That's how Word works. – Timothy Rylatt Jul 09 '20 at 12:58
  • Now I understand it. It's a web path because the file is stored in OneDrive. Thank you Timothy. – Leon K. Jul 09 '20 at 14:01
  • You can convert the web link to the local path using [this solution](https://stackoverflow.com/a/73577057/12287457). – GWD Sep 05 '22 at 23:54

2 Answers2

0

Depending on where the document is coming from the Document.Path property returns the disk or Web path to the document.

Use the Document.SaveAs2 method if you want to get a local file path. Basically, you need to save the file on the hard drive anywhere.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

For Office 365:

ActiveDocument.FullName

AlainD
  • 5,413
  • 6
  • 45
  • 99