将WordBasic获取文件目录代码转换为适配Word 365的现代VBA
filenamein... Replacement for Word 365 Hey there, I’ve run into this exact issue when migrating legacy Word macros to 365—WordBasic’s old functions like filenamepath or filenamein are totally deprecated now, so we need to swap them out with modern VBA alternatives that work reliably in 365.
Let’s break down what your original code was trying to do: it popped up the Save As dialog with a pre-filled filename, then grabbed the directory the user chose. Here are two solid replacement approaches that do the same thing without WordBasic:
Option 1: Use the Modern FileDialog Object (Recommended)
This is the cleanest, most future-proof method since it’s built directly into VBA for Office 365. It gives you more control over the dialog’s behavior too.
Sub GetSaveDirectory() Dim saveDialog As FileDialog Dim saveFullPath As String Dim targetDirectory As String ' Initialize the Save As dialog Set saveDialog = Application.FileDialog(msoFileDialogSaveAs) With saveDialog ' Set your pre-defined filename .InitialFileName = Jobnumber & " " & Jobname & " FDS V0_00.docx" ' Optional: Restrict to .docx files (adjust FilterIndex if needed) .Filter = "Word Documents (*.docx)|*.docx" .FilterIndex = 1 ' Show the dialog—check if user clicked "OK" If .Show = -1 Then ' Grab the full path including filename saveFullPath = .SelectedItems(1) ' Extract just the directory (strip off the filename) targetDirectory = Left(saveFullPath, InStrRev(saveFullPath, "\")) ' Use the directory however you need Debug.Print "Selected save directory: " & targetDirectory ' MsgBox "Save directory: " & targetDirectory ' Uncomment for a popup Else ' User canceled the dialog Debug.Print "Save operation canceled by user" End If End With ' Clean up the dialog object Set saveDialog = Nothing End Sub
Option 2: Stick with Dialogs(wdDialogFileSaveAs) (Legacy Dialog, Updated Code)
If you prefer to keep using the classic Save As dialog instead of the modern file picker, you can still do that—just replace the WordBasic call with VBA’s native string handling:
Sub GetSaveDirectoryFromLegacyDialog() Dim saveDialog As Dialog Dim fullSavePath As String Dim targetDirectory As String Set saveDialog = Dialogs(wdDialogFileSaveAs) With saveDialog ' Set your pre-filled filename .Name = Jobnumber & " " & Jobname & " FDS V0_00.docx" ' Show dialog and check for user confirmation If .Show = True Then ' Get the full path the user selected fullSavePath = .Name ' Extract the directory part targetDirectory = Left(fullSavePath, InStrRev(fullSavePath, "\")) ' Use the directory as needed Debug.Print "Selected directory: " & targetDirectory Else Debug.Print "User canceled save" End If End With Set saveDialog = Nothing End Sub
Key Notes:
- Make sure
JobnumberandJobnameare properly defined and assigned values before running this code—otherwise you’ll get an "undefined variable" error. - The
InStrRevfunction finds the last backslash in the full path, which lets us split off the filename cleanly to get just the directory. - Word 365 phased out most WordBasic functions because they were tied to ancient Word architecture—sticking to VBA-native methods ensures your macro will work across all modern Office versions.
内容的提问来源于stack exchange,提问作者Alex Gale

