Get value of TextBox in Word by but in Word template

Chris picture Chris · Jul 6, 2012 · Viewed 16.5k times · Source

Background: I want to use a specific entered text from a TextBox for the default filename in the SaveAs dialog.

I have implemented the following VBA script in my document, a Word 2010 template .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.

So, how can I read out the data of the TextBox in "document mode" of a template?

Answer

macropod picture macropod · Jan 3, 2019

Presumably, the OP's intention was something along the lines of:

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.