vba - 如何使用 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
让他们可以继续编写文档。
解决方案
无需使用 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
推荐阅读
- progressive-web-apps - 安装的 PWA 在 DNS 更新后继续连接到旧服务器
- html - 如何使部分透明的导航栏后面的内容消失?
- reactjs - 简单的 react-redux 连接函数中的错误
- javascript - 本地机器与 travis.ci 为 `Date.toISOString()` 返回不同的结果
- javascript - 计算有或没有 string.slice 的字符串中的字符
- ruby-on-rails - 在 Rails 教程中,如何在没有'require_relative "application_record"' 的情况下从 ApplicationRecord 继承 Article?
- java - 确保特定用户只能看到自己的用户详细信息 - 使用 Spring
- python - 在 Python 中创建类抽象
- go - 什么会导致 go 在 runtime.pthread_cond_signal 上花费这么多时间
- node.js - 模式关系在填充后返回未定义