Get current Word document's save directory - vba

I have a Word document that is already saved and I need to get its existing save directory and/or full file path (i.e. C:\Users\Public\Documents\testDoc.docx).
How can this be acquired from a macro/VBA?

ActiveDocument.FullName
The above gives the full path to the document, including the file name.
ActiveDocument.Path
The above gives the directory where your document is saved.
ActiveDocument.Name
The above gives the name of the document, without path information.

Related

Word VBA Documents.Open deleting files in folder when falsely passing that folder's path instead of file path

I encountered the following issue:
When accidentally passing a folder path to the Documents.Open function in VBA Word I get the runtime error 5174 as one would expect.
However all files with names that begin with an underscore get deleted in that moment from that folder.
To replicate:
Assume folder C:/Test/
In said folder have two files:
test.txt
_test.txt
In Word VBA execute the command:
Documents.Open("C:/Test/")
(As part of a subroutine or in the immediate window.)
Result: Runtime Error 5174 and _test.txt is now missing.
Note: Passing a nonexisting file like "C:/Test/abc.txt" or a wrong folder path like "C:/Test" (without the last slash) will not have this effect and won't cause the runtime error.
I have only tested this on one system on a network drive under windows 10 with Microsoft Professional Plus 2019. It's entirely possible that it's an issue with the file system. Does anyone have any suggestions as to why is this happening? I now included the workaround to check if I'm passing a folder, but it's still unnerving.
The Documents.Open method opens the specified document and adds it to the Documents collection. It is designed to take the file path, not a folder. If you want to allow users to pick file(s) you may consider using the file open dialog. The FileOpenDialog triggered by your code which opens a folder for picking files allows specifying file formats that should be supported and visible via the dialog window.
Set dlgSaveAs = Application.FileDialog(msoFileDialogFilePicker)
dlgSaveAs.Filter = "Text Files (.txt)|*.txt|Word Documents (.docx)|*.docx|Word Template (.dotx)|*.dotx|All Files (*.*)|*.*"
dlgSaveAs.ValidateNames = true
Res = dlgSaveAs.Show

How can I get the disk path of a word document using VBA? Document.Path returns the web path instead

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.
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.

VBA code (if needed) to save new Word document in same directory as other Word doc

Many users, myself included, begin editing a Word document, create a new Word doc, and wish to save the second doc in the same directory as the first doc. This is the default behavior in an editor such as emacs. Word 2013, alas, makes me pick the directory ("Folder") where I wish to save the new file, forcing me to select among various network folders and then select multiple subdirectories before I reach the one containing my first document. I used to have a simple VBA script that would find the directory of the first file and then save the new file in that directory, or at least it would set the directory of the first file as the default directory for saving files. Does anyone have code to do what I'm looking for? Thanks!
This will the working directory to the directory of the first file you were working on:
ChDir(Application.Documents(Application.Documents.Count).Path)
If you want to save the most recent opened document to the same location as the first file you were working on, then try the following
Sub SaveWithFirstFile()
NewFileName = "filename1.docx
Application.Documents(1).SaveAs2 FileName:=Application.Documents(Application.Documents.Count).Path & "\\" & NewFileName
End Sub
Change "filename1.docx" to what you want it to

How do I specify the file path in vba code in powerpoint 2011 for mac?

I want to access an image file in my VBA code for PowerPoint 2011 for Mac.
I have used both "/" and ":" as separator but it didn't work.
It works only when I save both .pptm file and the image file in the same folder and specify the relative path for the image file as below:
Dim strPath As String
strPath = "image.png"
But if I try saving the file at a different location than the .pptm file and provide the full path for the StrPath as:
strPath = "Users:Me:Desktop:image.png"
it throws a runtime error saying "the specified file wasn't found".
I have kept in mind the case sensitivity in case of Mac but nothing seems to be working for me.
Can anyone please help me in finding the possible workaround so that I can save the .pptm file and the image file at different locations on Mac?
Try:
Presentations.Open "Macintosh HD:Users:Me:Desktop:filename.ext"
Substitute appropriate strings for the name of the HD, your user name and the file.

SaveAs file name with new format in current folder

I have setup the following macro in a Word Template to save my file to the place where the template is current saved, but in a sub-folder with a new name and file format. Each time I run the macro I get a "Command failed" Error.
Here is my code:
pathName = ActiveDocument.Path & "\Periodic Count\CaseManager_CSV.txt"
ActiveDocument.SaveAs fileName:=pathName, FileFormat:=wdFormatText
I think the reason of your problem stem from unsaved document which doesn't have a path. As a result your pathName variable value is only like this: "\Periodic Count\CaseManager_CSV.txt" which is incorrect.
As you mentioned you want to save in the subfolder of the folder where your template is saved. Possibly you should modify pathName variable in this way:
pathName = ActiveDocument.AttachedTemplate.Path & "\Periodic Count\CaseManager_CSV.txt"