Prevent Word template from being saved over; Word doc is generated from Excel
I am using VBA in Excel to generate a shipping label in Word from a Word template. One time, after the Word doc was successfully opened and populated with my data, I accidentally saved over my original template when I closed it.
I want to prevent the user from being able to do this. If they close the Word doc without saving it first, I want them to be prompted to SaveAs, not to save over the existing template, and I want that SaveAs prompt to default to a .doc or .docx file, not a .dotx template file. If they hit the save button or Ctrl+S instead of just closing the document, I want the same SaveAs prompt to appear instead of overwriting my template.
For reference, I'll include the code that opens up my Word template. I declare my objects using Object instead of Word.Application to prevent missing reference errors for users who don't have references to the Word object library set up on their computers. This will be used on a few different computers, and they don't all have the same version of Office, so if you can think of anything else I can do to maintain compatibility I would love to hear it.
Since this involves the saving of a Word document generated from an Excel workbook, I'm not sure how to write event handling for this. Any help would be really appreciated. Thank you.
Dim wdApp As Object
Dim objRange As Object
Dim objDoc As Object
Set wdApp = CreateObject("Word.Application")
Set objDoc = wdApp.Documents.Open("[template file location]")
With wdApp
.Visible = True
.Activate
'Code that places strings at bookmark locations
Set objRange = objDoc.Bookmarks("bookmarkName").Range
objRange.InsertAfter (stringName)
End With
excel vba excel-vba ms-word
add a comment |
I am using VBA in Excel to generate a shipping label in Word from a Word template. One time, after the Word doc was successfully opened and populated with my data, I accidentally saved over my original template when I closed it.
I want to prevent the user from being able to do this. If they close the Word doc without saving it first, I want them to be prompted to SaveAs, not to save over the existing template, and I want that SaveAs prompt to default to a .doc or .docx file, not a .dotx template file. If they hit the save button or Ctrl+S instead of just closing the document, I want the same SaveAs prompt to appear instead of overwriting my template.
For reference, I'll include the code that opens up my Word template. I declare my objects using Object instead of Word.Application to prevent missing reference errors for users who don't have references to the Word object library set up on their computers. This will be used on a few different computers, and they don't all have the same version of Office, so if you can think of anything else I can do to maintain compatibility I would love to hear it.
Since this involves the saving of a Word document generated from an Excel workbook, I'm not sure how to write event handling for this. Any help would be really appreciated. Thank you.
Dim wdApp As Object
Dim objRange As Object
Dim objDoc As Object
Set wdApp = CreateObject("Word.Application")
Set objDoc = wdApp.Documents.Open("[template file location]")
With wdApp
.Visible = True
.Activate
'Code that places strings at bookmark locations
Set objRange = objDoc.Bookmarks("bookmarkName").Range
objRange.InsertAfter (stringName)
End With
excel vba excel-vba ms-word
Copy it first to a default location and then open it. That way, the original file will not be overwritten.
– L42
Jan 2 at 2:45
Will that save a new copy of the template every time it is opened?
– ARodrigo
Jan 2 at 2:55
Possible duplicate of Copying contents of Word doc to newly created Word doc from excel VBA
– Cindy Meister
Jan 2 at 6:53
@ARodrigo Yes, it will save a new one. But that will preserve your original file. Also, you can edit the template and update it anytime as it will not be locked when someone uses it.
– L42
Jan 3 at 5:35
add a comment |
I am using VBA in Excel to generate a shipping label in Word from a Word template. One time, after the Word doc was successfully opened and populated with my data, I accidentally saved over my original template when I closed it.
I want to prevent the user from being able to do this. If they close the Word doc without saving it first, I want them to be prompted to SaveAs, not to save over the existing template, and I want that SaveAs prompt to default to a .doc or .docx file, not a .dotx template file. If they hit the save button or Ctrl+S instead of just closing the document, I want the same SaveAs prompt to appear instead of overwriting my template.
For reference, I'll include the code that opens up my Word template. I declare my objects using Object instead of Word.Application to prevent missing reference errors for users who don't have references to the Word object library set up on their computers. This will be used on a few different computers, and they don't all have the same version of Office, so if you can think of anything else I can do to maintain compatibility I would love to hear it.
Since this involves the saving of a Word document generated from an Excel workbook, I'm not sure how to write event handling for this. Any help would be really appreciated. Thank you.
Dim wdApp As Object
Dim objRange As Object
Dim objDoc As Object
Set wdApp = CreateObject("Word.Application")
Set objDoc = wdApp.Documents.Open("[template file location]")
With wdApp
.Visible = True
.Activate
'Code that places strings at bookmark locations
Set objRange = objDoc.Bookmarks("bookmarkName").Range
objRange.InsertAfter (stringName)
End With
excel vba excel-vba ms-word
I am using VBA in Excel to generate a shipping label in Word from a Word template. One time, after the Word doc was successfully opened and populated with my data, I accidentally saved over my original template when I closed it.
I want to prevent the user from being able to do this. If they close the Word doc without saving it first, I want them to be prompted to SaveAs, not to save over the existing template, and I want that SaveAs prompt to default to a .doc or .docx file, not a .dotx template file. If they hit the save button or Ctrl+S instead of just closing the document, I want the same SaveAs prompt to appear instead of overwriting my template.
For reference, I'll include the code that opens up my Word template. I declare my objects using Object instead of Word.Application to prevent missing reference errors for users who don't have references to the Word object library set up on their computers. This will be used on a few different computers, and they don't all have the same version of Office, so if you can think of anything else I can do to maintain compatibility I would love to hear it.
Since this involves the saving of a Word document generated from an Excel workbook, I'm not sure how to write event handling for this. Any help would be really appreciated. Thank you.
Dim wdApp As Object
Dim objRange As Object
Dim objDoc As Object
Set wdApp = CreateObject("Word.Application")
Set objDoc = wdApp.Documents.Open("[template file location]")
With wdApp
.Visible = True
.Activate
'Code that places strings at bookmark locations
Set objRange = objDoc.Bookmarks("bookmarkName").Range
objRange.InsertAfter (stringName)
End With
excel vba excel-vba ms-word
excel vba excel-vba ms-word
edited Jan 2 at 12:45
J.schmidt
649120
649120
asked Jan 2 at 2:41
ARodrigoARodrigo
1
1
Copy it first to a default location and then open it. That way, the original file will not be overwritten.
– L42
Jan 2 at 2:45
Will that save a new copy of the template every time it is opened?
– ARodrigo
Jan 2 at 2:55
Possible duplicate of Copying contents of Word doc to newly created Word doc from excel VBA
– Cindy Meister
Jan 2 at 6:53
@ARodrigo Yes, it will save a new one. But that will preserve your original file. Also, you can edit the template and update it anytime as it will not be locked when someone uses it.
– L42
Jan 3 at 5:35
add a comment |
Copy it first to a default location and then open it. That way, the original file will not be overwritten.
– L42
Jan 2 at 2:45
Will that save a new copy of the template every time it is opened?
– ARodrigo
Jan 2 at 2:55
Possible duplicate of Copying contents of Word doc to newly created Word doc from excel VBA
– Cindy Meister
Jan 2 at 6:53
@ARodrigo Yes, it will save a new one. But that will preserve your original file. Also, you can edit the template and update it anytime as it will not be locked when someone uses it.
– L42
Jan 3 at 5:35
Copy it first to a default location and then open it. That way, the original file will not be overwritten.
– L42
Jan 2 at 2:45
Copy it first to a default location and then open it. That way, the original file will not be overwritten.
– L42
Jan 2 at 2:45
Will that save a new copy of the template every time it is opened?
– ARodrigo
Jan 2 at 2:55
Will that save a new copy of the template every time it is opened?
– ARodrigo
Jan 2 at 2:55
Possible duplicate of Copying contents of Word doc to newly created Word doc from excel VBA
– Cindy Meister
Jan 2 at 6:53
Possible duplicate of Copying contents of Word doc to newly created Word doc from excel VBA
– Cindy Meister
Jan 2 at 6:53
@ARodrigo Yes, it will save a new one. But that will preserve your original file. Also, you can edit the template and update it anytime as it will not be locked when someone uses it.
– L42
Jan 3 at 5:35
@ARodrigo Yes, it will save a new one. But that will preserve your original file. Also, you can edit the template and update it anytime as it will not be locked when someone uses it.
– L42
Jan 3 at 5:35
add a comment |
1 Answer
1
active
oldest
votes
Don't use Set objDoc = wdApp.Documents.Open("[template file location]"). The correct syntax is to use
Set objDoc = wdApp.Documents.Add("[template file location]")
This will add a document based on the specified template. There are more arguments you can pass, including the DocumentType which would specify whether the new document is of docx format.
Thank you for finding the other thread! And thank @Variatus for letting me know that I can specify the extension of the added document. That solved my issue. If I don't specify the extension though, will it automatically choose the default for the user? Like if they're using Office 2003 will it default to .doc?
– ARodrigo
Jan 2 at 13:18
This is what MSDN says: Can be one of the following WdNewDocumentType constants: wdNewBlankDocument , wdNewEmailMessage , wdNewFrameset , or wdNewWebPage. The default constant is wdNewBlankDocument. Actually, the question of docx or dotm would arise only at the time of saving. The newly added document is unspecified in this regard.
– Variatus
Jan 3 at 1:38
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%2f54000643%2fprevent-word-template-from-being-saved-over-word-doc-is-generated-from-excel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Don't use Set objDoc = wdApp.Documents.Open("[template file location]"). The correct syntax is to use
Set objDoc = wdApp.Documents.Add("[template file location]")
This will add a document based on the specified template. There are more arguments you can pass, including the DocumentType which would specify whether the new document is of docx format.
Thank you for finding the other thread! And thank @Variatus for letting me know that I can specify the extension of the added document. That solved my issue. If I don't specify the extension though, will it automatically choose the default for the user? Like if they're using Office 2003 will it default to .doc?
– ARodrigo
Jan 2 at 13:18
This is what MSDN says: Can be one of the following WdNewDocumentType constants: wdNewBlankDocument , wdNewEmailMessage , wdNewFrameset , or wdNewWebPage. The default constant is wdNewBlankDocument. Actually, the question of docx or dotm would arise only at the time of saving. The newly added document is unspecified in this regard.
– Variatus
Jan 3 at 1:38
add a comment |
Don't use Set objDoc = wdApp.Documents.Open("[template file location]"). The correct syntax is to use
Set objDoc = wdApp.Documents.Add("[template file location]")
This will add a document based on the specified template. There are more arguments you can pass, including the DocumentType which would specify whether the new document is of docx format.
Thank you for finding the other thread! And thank @Variatus for letting me know that I can specify the extension of the added document. That solved my issue. If I don't specify the extension though, will it automatically choose the default for the user? Like if they're using Office 2003 will it default to .doc?
– ARodrigo
Jan 2 at 13:18
This is what MSDN says: Can be one of the following WdNewDocumentType constants: wdNewBlankDocument , wdNewEmailMessage , wdNewFrameset , or wdNewWebPage. The default constant is wdNewBlankDocument. Actually, the question of docx or dotm would arise only at the time of saving. The newly added document is unspecified in this regard.
– Variatus
Jan 3 at 1:38
add a comment |
Don't use Set objDoc = wdApp.Documents.Open("[template file location]"). The correct syntax is to use
Set objDoc = wdApp.Documents.Add("[template file location]")
This will add a document based on the specified template. There are more arguments you can pass, including the DocumentType which would specify whether the new document is of docx format.
Don't use Set objDoc = wdApp.Documents.Open("[template file location]"). The correct syntax is to use
Set objDoc = wdApp.Documents.Add("[template file location]")
This will add a document based on the specified template. There are more arguments you can pass, including the DocumentType which would specify whether the new document is of docx format.
answered Jan 2 at 4:45
VariatusVariatus
5,7251524
5,7251524
Thank you for finding the other thread! And thank @Variatus for letting me know that I can specify the extension of the added document. That solved my issue. If I don't specify the extension though, will it automatically choose the default for the user? Like if they're using Office 2003 will it default to .doc?
– ARodrigo
Jan 2 at 13:18
This is what MSDN says: Can be one of the following WdNewDocumentType constants: wdNewBlankDocument , wdNewEmailMessage , wdNewFrameset , or wdNewWebPage. The default constant is wdNewBlankDocument. Actually, the question of docx or dotm would arise only at the time of saving. The newly added document is unspecified in this regard.
– Variatus
Jan 3 at 1:38
add a comment |
Thank you for finding the other thread! And thank @Variatus for letting me know that I can specify the extension of the added document. That solved my issue. If I don't specify the extension though, will it automatically choose the default for the user? Like if they're using Office 2003 will it default to .doc?
– ARodrigo
Jan 2 at 13:18
This is what MSDN says: Can be one of the following WdNewDocumentType constants: wdNewBlankDocument , wdNewEmailMessage , wdNewFrameset , or wdNewWebPage. The default constant is wdNewBlankDocument. Actually, the question of docx or dotm would arise only at the time of saving. The newly added document is unspecified in this regard.
– Variatus
Jan 3 at 1:38
Thank you for finding the other thread! And thank @Variatus for letting me know that I can specify the extension of the added document. That solved my issue. If I don't specify the extension though, will it automatically choose the default for the user? Like if they're using Office 2003 will it default to .doc?
– ARodrigo
Jan 2 at 13:18
Thank you for finding the other thread! And thank @Variatus for letting me know that I can specify the extension of the added document. That solved my issue. If I don't specify the extension though, will it automatically choose the default for the user? Like if they're using Office 2003 will it default to .doc?
– ARodrigo
Jan 2 at 13:18
This is what MSDN says: Can be one of the following WdNewDocumentType constants: wdNewBlankDocument , wdNewEmailMessage , wdNewFrameset , or wdNewWebPage. The default constant is wdNewBlankDocument. Actually, the question of docx or dotm would arise only at the time of saving. The newly added document is unspecified in this regard.
– Variatus
Jan 3 at 1:38
This is what MSDN says: Can be one of the following WdNewDocumentType constants: wdNewBlankDocument , wdNewEmailMessage , wdNewFrameset , or wdNewWebPage. The default constant is wdNewBlankDocument. Actually, the question of docx or dotm would arise only at the time of saving. The newly added document is unspecified in this regard.
– Variatus
Jan 3 at 1:38
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%2f54000643%2fprevent-word-template-from-being-saved-over-word-doc-is-generated-from-excel%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
Copy it first to a default location and then open it. That way, the original file will not be overwritten.
– L42
Jan 2 at 2:45
Will that save a new copy of the template every time it is opened?
– ARodrigo
Jan 2 at 2:55
Possible duplicate of Copying contents of Word doc to newly created Word doc from excel VBA
– Cindy Meister
Jan 2 at 6:53
@ARodrigo Yes, it will save a new one. But that will preserve your original file. Also, you can edit the template and update it anytime as it will not be locked when someone uses it.
– L42
Jan 3 at 5:35