使用 VBA 宏获取 Word 中 TextBox 的值,但在 Word 模板中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11363427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value of TextBox in Word by using VBA macro, but in Word template
提问by Chris
Background: I want to use a specific entered text from a TextBox for the default filename in the SaveAs dialog.
背景:我想使用文本框中输入的特定文本作为 SaveAs 对话框中的默认文件名。
I have implemented the following VBA script in my document, a Word 2010 template .dotm
我在我的文档中实现了以下 VBA 脚本,一个 Word 2010 模板 .dotm
Sub FileSaveAs()
'for testing
Dim fileName As String
fileName = Me.tb_myTextBox.Value & "_MyFileNameToSave"
MsgBox fileName
'use specific file name in save dialog
With Dialogs(wdDialogFileSaveAs)
.Name = fileName
.Show
End With
End Sub
It works fine, when I run it. I saved the .dotm, closed it and reopened it out from the Windows Explorer (means as "end user"). BUT in this case, means after open the template document as "end user" (so that I can save a new doc out of it and not overwrite the template), the content/value of the TextBox is empty, even if I entered something into it.
当我运行它时,它工作正常。我保存了 .dotm,关闭它并从 Windows 资源管理器中重新打开它(意思是“最终用户”)。但是在这种情况下,意味着在以“最终用户”身份打开模板文档(以便我可以从中保存新文档而不是覆盖模板)后,即使我输入了一些内容,TextBox 的内容/值也是空的进去。
So, how can I read out the data of the TextBox in "document mode" of a template?
那么,如何在模板的“文档模式”下读出TextBox的数据呢?
回答by JohnZaj
how can I read out the data of the TextBox in "document mode" of a template?
如何在模板的“文档模式”下读出文本框的数据?
Not sure what you mean. This works for me:
不明白你的意思。这对我有用:
create a form:
创建一个表单:
Private Sub btn_OK_Click()
Dim fileName As String
fileName = tb_myTextBox.Value & "_MyFileNameToSave"
With Dialogs(wdDialogFileSaveAs)
.name = fileName
.Show
End With
End Sub
create a sub to call this form:
创建一个子来调用这个表单:
Sub FileSaveAs()
UserForm1.Show
End Sub
This is all saved in a template / .dotm.
这都保存在模板/.dotm 中。
Now, create a document off of the template (double click the template to launch document off of it). Alt + F8 and run the macro from the template (you may have to select the template from the "Macros in" drop down). Result: my form comes up, I enter a name for the document, press ok, and the Word Save As dialog appears with the name I gave to the document.
现在,从模板创建一个文档(双击模板以启动文档)。Alt + F8 并从模板运行宏(您可能必须从“宏输入”下拉列表中选择模板)。结果:出现我的表单,我输入文档的名称,按确定,然后出现 Word 另存为对话框,其中包含我为文档指定的名称。
回答by macropod
Presumably, the OP's intention was something along the lines of:
据推测,OP 的意图是这样的:
Sub FileSaveAs()
Dim StrNm As String
With ActiveDocument
StrNm = Split(.Shapes(1).TextFrame.TextRange.Text, vbCr)(0) & "_MyFileNameToSave"
'use specific file name in save dialog
With Dialogs(wdDialogFileSaveAs)
.Name = StrNm
.Show
End With
End With
End Sub
where .Shapes(1) identifies the particular textbox Shape object.
其中 .Shapes(1) 标识特定的文本框 Shape 对象。