首页 > 解决方案 > 如何使用 Word 中的命令按钮创建另存为弹出窗口?

问题描述

我有允许用户输入数据(姓名、地址、公司名称、城市、州)的代码,这些数据运行良好,当用户点击提交时,它会将其填充到 word 文档中。

接下来,如果可能的话,我会尝试在单击提交后立即弹出保存,但不知道下一步该做什么。我尝试了多个示例,但是所有示例都给了我预期的编译错误,我需要帮助!

这是我的工作代码:

Private Sub CommandButton1_Click()
    Dim firstnamelastname As Range
    Set firstnamelastname = ActiveDocument.Bookmarks("firstnamelastname").Range
    firstnamelastname.Text = Me.TextBox1.Value
    Dim Companyname As Range
    Set Companyname = ActiveDocument.Bookmarks("Companyname").Range
    Companyname.Text = Me.TextBox2
    Dim Address As Range
    Set Address = ActiveDocument.Bookmarks("address").Range
    Address.Text = Me.TextBox3
    Dim citystatezip As Range
    Set citystatezip = ActiveDocument.Bookmarks("Citystatezip").Range
    citystatezip.Text = Me.TextBox4
    Me.Repaint
    userform1.hide

但是当我添加任何东西来保存时,它就不起作用了。

我也可以删除 userform1.hide 代码并添加另一个按钮进行保存;然后userform.hide让他们可以继续编写文档。

标签: vbams-word

解决方案


无需使用 Windows API。

Application.Dialogs属性返回一个Dialogs集合,该集合表示 Word 中的所有内置对话框。要从集合中获取对象,您需要传递WdWordDialog 枚举的实例。例如,以下代码显示了带有预定义值的 SaveAs 对话框:

dim strFullPath as string
dim strRootPath as string
dim strFileName as string

strRootPath = "C:\Users\Eugene\Documents\"
strFileName = "FileName.docx"
strFullPath = strRootPath & strFileName

With appWrd.Dialogs(wdDialogFileSaveAs)
    .Name = strFullPath
    .Format = Word.WdSaveFormat.wdFormatXMLDocument
    .Show
End With

推荐阅读