【问题标题】:How to name file based on data in bookmarks如何根据书签中的数据命名文件
【发布时间】:2020-12-30 17:34:16
【问题描述】:

我正在尝试使用基于模板的名称保存 Microsoft Word 2010 文档。

我有一个带有书签名称的模板表单:
ProcNo(程序编号的书签)
RevNo(修订号的书签)

我想打开“另存为”窗口,并插入以下格式的文件名:“ProcNo” Rev “RevNo” - 附件 4 Traveler

其中“ProcNo”是从 Bookmark for Procedure Number 提取的文本,“RevNo”是从 Bookmark for Revision Number 提取的文本

然后我会在“另存为”窗口中选择文件目标。

我尝试使用宏记录器并单击“文件”、“另存为”,然后单击“浏览”以打开“另存为”窗口并将文档名称键入为“ABC”以了解代码的外观;但有以下问题:

我不知道打开“另存为”窗口的代码。

我不知道在包含上述文件命名格式的“另存为”窗口中插入文件名的代码。

宏记录器给了我以下信息:

Sub SaveAs()
'
' SaveAs Macro
'
'
    ChangeFileOpenDirectory "C:\Users\bzimme1\Documents\"
    ActiveDocument.SaveAs2 FileName:="ABC.docx", FileFormat:= _
        wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
        :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
        :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=15
End Sub

【问题讨论】:

标签: vba ms-word filenames


【解决方案1】:

试试:

Sub Demo()
Dim sFlNm As String
With ActiveDocument
  sFlNm = .Bookmarks("ProcNo").Range.Text & " Rev " & .Bookmarks("RevNo").Range.Text & " – Attachment 4 Traveler"
End With
With Dialogs(wdDialogFileSaveAs)
  .Name = sFlNm
  .Show
End With
End Sub

【讨论】:

  • 就是这样!它工作得很好。非常感谢!
【解决方案2】:

这是我使用的。它设置为:

  1. 从“标题”内置文档属性中获取信息
  2. 从文档中的第一个内容控件获取和附加信息以用于文件名
  3. 在末尾追加日期
  4. 保留当前默认保存文件夹以供恢复
  5. 设置为保存在特定文件夹中而不更改其他文档的默认保存路径
  6. 显示“另存为”对话框以及对话框中的信息

您可以使用类似的语句从您的书签中获取信息。请参阅 Ibby 在 Word MVP 网站上的 Working With Bookmarks

Sub FileSaveAs()
    ' Run as substitute for FileSave to add date to default document names
    ' Charles Kenyon 2017, 2019, 2020
    ' Appends content control info and date to Title Document property when saving the first time
    On Error Resume Next
   
    If Len(ActiveDocument.Path) > 0 Then
      ' The document has already been saved at least once.
      ' Just save and exit the macro.
      ActiveDocument.Save
      Exit Sub
    End If
    '
    '
    Dim strName As String, dlgSave As Dialog
    Dim strPath As String   'Holder for current path
    Const strTEMPPATH As String = "full path where" ' where document will be stored
    Let strPath = Application.Options.DefaultFilePath(wdDocumentsPath)
    Set dlgSave = Dialogs(wdDialogFileSaveAs)
    strName = ActiveDocument.BuiltInDocumentProperties("Title").Value 'get name in title
'    InputBox (strName)
    strName = strName & " " & ActiveDocument.ContentControls(1).Range.Text 'add name from first content control
'    InputBox (strName)
    strName = strName & " " & Format((Year(Now() + 1) Mod 100), "20##") & "-" & _
        Format((Month(Now() + 1) Mod 100), "0#") & "-" & _
        Format((Day(Now()) Mod 100), "0#") 'add date
    With dlgSave
        .Name = strTEMPPATH & strName
        .Show
    End With
    '   Reset save path
    Let Application.Options.DefaultFilePath(wdDocumentsPath) = strPath
    '   empty object
    Set dlgSave = Nothing
    OnError GoTo -1
End Sub

【讨论】:

    猜你喜欢
    • 2020-05-31
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2022-09-25
    相关资源
    最近更新 更多