简体   繁体   English

来自书签的 MS Word 文件名 VBA

[英]MS Word filename from bookmarks with VBA

Assuming I have:假设我有:

  • a word template including macro: custom_template.dotm ;包含宏的单词模板: custom_template.dotm
  • two bookmarks in this template: ' first_name " and " last_name ".此模板中的两个书签:“ first_name ”和“ last_name ”。

I would like that on "Save" event, in the dialog box, the application proposes to user, instead of "document1", and only if relative bookmarks exist, the filename " Document of first_name second_name.docx ".我希望在“保存”事件中,在对话框中,应用程序向用户建议,而不是“document1”,并且仅当存在相关书签时,文件名“ Document of first_name second_name.docx ”。

Can anybody explain me how to achieve this with VBA?谁能解释一下如何使用 VBA 实现这一目标?

Thanks.谢谢。

=== UPDATE === ===更新===

Now I've this code, working well when I execute it.现在我有了这段代码,当我执行它时运行良好。 I would like it runs automatically when user clicks on "save document".我希望它在用户单击“保存文档”时自动运行。

Sub Demo()
Dim sFlNm As String
With ActiveDocument
  sFlNm = "Document of " & .Bookmarks("first_name").Range.Text & " " & .Bookmarks("last_name").Range.Text
End With
With Dialogs(wdDialogFileSaveAs)
  .Name = sFlNm
  .Show
End With
End Sub

For a macro to run at the client's end, you would have to send a macro-enabled template or document.要在客户端运行宏,您必须发送启用宏的模板或文档。 Many people who are running anti-virus software will get a warning of a possible Word virus.许多运行防病毒软件的人会收到可能存在 Word 病毒的警告。 Then the user will have to manually enable the macros.然后用户将不得不手动启用宏。 Are they going to bother?他们会打扰吗?

Making it run automatically with a Save command may have unintended consequences.使用保存命令使其自动运行可能会产生意想不到的后果。 You'll have to check whether the bookmarks have actually been filled, and using the Save command while you're revising the document can save it with a new file name.您必须检查书签是否已实际填写,并且在修改文档时使用“保存”命令可以使用新文件名保存它。 But you asked, so here's how: rename the macro as FileSave .但是您问了,所以方法如下:将宏重命名为FileSave Then when you choose Ctrl + S or File>Save in Word, the dialog will automatically pop up:然后当你选择Ctrl + SFile>Save in Word 时,会自动弹出对话框:

Sub FileSave()
    Dim sFlNm As String
    With ActiveDocument
      sFlNm = "Document of " & .Bookmarks("first_name").Range.Text & " " & .Bookmarks("last_name").Range.Text
    End With
    With Dialogs(wdDialogFileSaveAs)
      .Name = sFlNm
      .Show
    End With
End Sub

In Word, to create a macro that runs automatically when you choose a Word command, follow these steps:在 Word 中,若要创建在您选择 Word 命令时自动运行的宏,请执行以下步骤:

  1. Choose Developer>Macros .选择开发人员>宏
  2. Change the Macros in dropdown to Word commands .将下拉列表中的更改为Word 命令
  3. Choose the command name you want to re-purpose.选择要重新使用的命令名称。
  4. Change the Macros in dropdown back to the macro-enabled document or template that you're developing.将下拉列表中的更改回您正在开发的启用宏的文档或模板。
  5. Click on the Create button.单击创建按钮。 A new macro is created in the VBE with the correct command name.使用正确的命令名称在 VBE 中创建了一个新宏。 Fill in the macro with whatever you want the macro to do.用您希望宏执行的任何操作填充宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM