Get value of TextBox in Word by using VBA macro, but in Word template
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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?
vba ms-word word-vba word-template
add a comment |
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?
vba ms-word word-vba word-template
Where is the textbox?
– Siddharth Rout
Jul 7 '12 at 11:48
You should edit your question to share your exact steps, I don't understand what is not working for you. Also, your code is incomplete and anyone who offers help will need to fill in the blanks, which could lead to confusion.
– JohnZaj
Jul 7 '12 at 14:38
Ok, new try of explanation... there are two cases: - (1) editing the template, if you save it, it is again .dotm. In this case all works fine with the script. - (2) open the template as "end user" (and not as template-author). If the user clicks the Save-button, the document is saved as .doc and not .dotm. In this case, the script does NOT work! btw: where the code is incomplete?? confused
– Chris
Jul 9 '12 at 7:02
@SiddharthRout: The TextBox is just located on the page (root level). The VBA script is saved in "This document" source code (and not in a module).
– Chris
Jul 9 '12 at 7:06
Did you solve this? If not, I can steer you in the right direction...
– Cindy Meister
Jan 4 '16 at 10:36
add a comment |
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?
vba ms-word word-vba word-template
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?
vba ms-word word-vba word-template
vba ms-word word-vba word-template
edited Jul 6 '12 at 13:48
Gaffi
3,45263969
3,45263969
asked Jul 6 '12 at 13:44
ChrisChris
4522518
4522518
Where is the textbox?
– Siddharth Rout
Jul 7 '12 at 11:48
You should edit your question to share your exact steps, I don't understand what is not working for you. Also, your code is incomplete and anyone who offers help will need to fill in the blanks, which could lead to confusion.
– JohnZaj
Jul 7 '12 at 14:38
Ok, new try of explanation... there are two cases: - (1) editing the template, if you save it, it is again .dotm. In this case all works fine with the script. - (2) open the template as "end user" (and not as template-author). If the user clicks the Save-button, the document is saved as .doc and not .dotm. In this case, the script does NOT work! btw: where the code is incomplete?? confused
– Chris
Jul 9 '12 at 7:02
@SiddharthRout: The TextBox is just located on the page (root level). The VBA script is saved in "This document" source code (and not in a module).
– Chris
Jul 9 '12 at 7:06
Did you solve this? If not, I can steer you in the right direction...
– Cindy Meister
Jan 4 '16 at 10:36
add a comment |
Where is the textbox?
– Siddharth Rout
Jul 7 '12 at 11:48
You should edit your question to share your exact steps, I don't understand what is not working for you. Also, your code is incomplete and anyone who offers help will need to fill in the blanks, which could lead to confusion.
– JohnZaj
Jul 7 '12 at 14:38
Ok, new try of explanation... there are two cases: - (1) editing the template, if you save it, it is again .dotm. In this case all works fine with the script. - (2) open the template as "end user" (and not as template-author). If the user clicks the Save-button, the document is saved as .doc and not .dotm. In this case, the script does NOT work! btw: where the code is incomplete?? confused
– Chris
Jul 9 '12 at 7:02
@SiddharthRout: The TextBox is just located on the page (root level). The VBA script is saved in "This document" source code (and not in a module).
– Chris
Jul 9 '12 at 7:06
Did you solve this? If not, I can steer you in the right direction...
– Cindy Meister
Jan 4 '16 at 10:36
Where is the textbox?
– Siddharth Rout
Jul 7 '12 at 11:48
Where is the textbox?
– Siddharth Rout
Jul 7 '12 at 11:48
You should edit your question to share your exact steps, I don't understand what is not working for you. Also, your code is incomplete and anyone who offers help will need to fill in the blanks, which could lead to confusion.
– JohnZaj
Jul 7 '12 at 14:38
You should edit your question to share your exact steps, I don't understand what is not working for you. Also, your code is incomplete and anyone who offers help will need to fill in the blanks, which could lead to confusion.
– JohnZaj
Jul 7 '12 at 14:38
Ok, new try of explanation... there are two cases: - (1) editing the template, if you save it, it is again .dotm. In this case all works fine with the script. - (2) open the template as "end user" (and not as template-author). If the user clicks the Save-button, the document is saved as .doc and not .dotm. In this case, the script does NOT work! btw: where the code is incomplete?? confused
– Chris
Jul 9 '12 at 7:02
Ok, new try of explanation... there are two cases: - (1) editing the template, if you save it, it is again .dotm. In this case all works fine with the script. - (2) open the template as "end user" (and not as template-author). If the user clicks the Save-button, the document is saved as .doc and not .dotm. In this case, the script does NOT work! btw: where the code is incomplete?? confused
– Chris
Jul 9 '12 at 7:02
@SiddharthRout: The TextBox is just located on the page (root level). The VBA script is saved in "This document" source code (and not in a module).
– Chris
Jul 9 '12 at 7:06
@SiddharthRout: The TextBox is just located on the page (root level). The VBA script is saved in "This document" source code (and not in a module).
– Chris
Jul 9 '12 at 7:06
Did you solve this? If not, I can steer you in the right direction...
– Cindy Meister
Jan 4 '16 at 10:36
Did you solve this? If not, I can steer you in the right direction...
– Cindy Meister
Jan 4 '16 at 10:36
add a comment |
2 Answers
2
active
oldest
votes
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.
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.
Thanks for reply! Problem is, that I do not have an additional Save button. The "FileSaveAs()" sub function shall be (automatically) called, after the user clicks on Save(As) button/menu entry.
– Chris
Jul 9 '12 at 9:29
So FileSaveAs is not intercepting the user doing a File | Save As? I don't understand the problem so I need o recreate it.
– JohnZaj
Jul 12 '12 at 15:20
No, my Sub is called when the user clicks File | Save (as). So the call of the function works fine! But the TextField is located in the document and not in an extra user form. Also have a look to my comment in the question. It is very strange, why all works in case 1, but not in case 2. :-/
– Chris
Jul 13 '12 at 10:24
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11363427%2fget-value-of-textbox-in-word-by-using-vba-macro-but-in-word-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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.
Thanks for reply! Problem is, that I do not have an additional Save button. The "FileSaveAs()" sub function shall be (automatically) called, after the user clicks on Save(As) button/menu entry.
– Chris
Jul 9 '12 at 9:29
So FileSaveAs is not intercepting the user doing a File | Save As? I don't understand the problem so I need o recreate it.
– JohnZaj
Jul 12 '12 at 15:20
No, my Sub is called when the user clicks File | Save (as). So the call of the function works fine! But the TextField is located in the document and not in an extra user form. Also have a look to my comment in the question. It is very strange, why all works in case 1, but not in case 2. :-/
– Chris
Jul 13 '12 at 10:24
add a comment |
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.
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.
Thanks for reply! Problem is, that I do not have an additional Save button. The "FileSaveAs()" sub function shall be (automatically) called, after the user clicks on Save(As) button/menu entry.
– Chris
Jul 9 '12 at 9:29
So FileSaveAs is not intercepting the user doing a File | Save As? I don't understand the problem so I need o recreate it.
– JohnZaj
Jul 12 '12 at 15:20
No, my Sub is called when the user clicks File | Save (as). So the call of the function works fine! But the TextField is located in the document and not in an extra user form. Also have a look to my comment in the question. It is very strange, why all works in case 1, but not in case 2. :-/
– Chris
Jul 13 '12 at 10:24
add a comment |
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.
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.
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.
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.
answered Jul 7 '12 at 14:47


JohnZajJohnZaj
1,88652945
1,88652945
Thanks for reply! Problem is, that I do not have an additional Save button. The "FileSaveAs()" sub function shall be (automatically) called, after the user clicks on Save(As) button/menu entry.
– Chris
Jul 9 '12 at 9:29
So FileSaveAs is not intercepting the user doing a File | Save As? I don't understand the problem so I need o recreate it.
– JohnZaj
Jul 12 '12 at 15:20
No, my Sub is called when the user clicks File | Save (as). So the call of the function works fine! But the TextField is located in the document and not in an extra user form. Also have a look to my comment in the question. It is very strange, why all works in case 1, but not in case 2. :-/
– Chris
Jul 13 '12 at 10:24
add a comment |
Thanks for reply! Problem is, that I do not have an additional Save button. The "FileSaveAs()" sub function shall be (automatically) called, after the user clicks on Save(As) button/menu entry.
– Chris
Jul 9 '12 at 9:29
So FileSaveAs is not intercepting the user doing a File | Save As? I don't understand the problem so I need o recreate it.
– JohnZaj
Jul 12 '12 at 15:20
No, my Sub is called when the user clicks File | Save (as). So the call of the function works fine! But the TextField is located in the document and not in an extra user form. Also have a look to my comment in the question. It is very strange, why all works in case 1, but not in case 2. :-/
– Chris
Jul 13 '12 at 10:24
Thanks for reply! Problem is, that I do not have an additional Save button. The "FileSaveAs()" sub function shall be (automatically) called, after the user clicks on Save(As) button/menu entry.
– Chris
Jul 9 '12 at 9:29
Thanks for reply! Problem is, that I do not have an additional Save button. The "FileSaveAs()" sub function shall be (automatically) called, after the user clicks on Save(As) button/menu entry.
– Chris
Jul 9 '12 at 9:29
So FileSaveAs is not intercepting the user doing a File | Save As? I don't understand the problem so I need o recreate it.
– JohnZaj
Jul 12 '12 at 15:20
So FileSaveAs is not intercepting the user doing a File | Save As? I don't understand the problem so I need o recreate it.
– JohnZaj
Jul 12 '12 at 15:20
No, my Sub is called when the user clicks File | Save (as). So the call of the function works fine! But the TextField is located in the document and not in an extra user form. Also have a look to my comment in the question. It is very strange, why all works in case 1, but not in case 2. :-/
– Chris
Jul 13 '12 at 10:24
No, my Sub is called when the user clicks File | Save (as). So the call of the function works fine! But the TextField is located in the document and not in an extra user form. Also have a look to my comment in the question. It is very strange, why all works in case 1, but not in case 2. :-/
– Chris
Jul 13 '12 at 10:24
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 3 at 12:51


macropodmacropod
3,0592311
3,0592311
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11363427%2fget-value-of-textbox-in-word-by-using-vba-macro-but-in-word-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where is the textbox?
– Siddharth Rout
Jul 7 '12 at 11:48
You should edit your question to share your exact steps, I don't understand what is not working for you. Also, your code is incomplete and anyone who offers help will need to fill in the blanks, which could lead to confusion.
– JohnZaj
Jul 7 '12 at 14:38
Ok, new try of explanation... there are two cases: - (1) editing the template, if you save it, it is again .dotm. In this case all works fine with the script. - (2) open the template as "end user" (and not as template-author). If the user clicks the Save-button, the document is saved as .doc and not .dotm. In this case, the script does NOT work! btw: where the code is incomplete?? confused
– Chris
Jul 9 '12 at 7:02
@SiddharthRout: The TextBox is just located on the page (root level). The VBA script is saved in "This document" source code (and not in a module).
– Chris
Jul 9 '12 at 7:06
Did you solve this? If not, I can steer you in the right direction...
– Cindy Meister
Jan 4 '16 at 10:36