Word VBA - Save Multiple Files
Hi I have a word file that makes a change to a table using VBA. After every change, I would like to save a new file with these changes. This way I have 10 copies with 10 different tables.
But currently, when I perform the code below, it exits the original, and thereby exiting the loop. Excel has a method SaveCopyAs, which Word doesn't have. Are there any ways to workaround this?
Documents.Add.SaveAs2 FileName:=("C:UsersXXXDownloadsBaby
Shower Table Games_Updated_" & x & ".docm")
vba ms-word word-vba
add a comment |
Hi I have a word file that makes a change to a table using VBA. After every change, I would like to save a new file with these changes. This way I have 10 copies with 10 different tables.
But currently, when I perform the code below, it exits the original, and thereby exiting the loop. Excel has a method SaveCopyAs, which Word doesn't have. Are there any ways to workaround this?
Documents.Add.SaveAs2 FileName:=("C:UsersXXXDownloadsBaby
Shower Table Games_Updated_" & x & ".docm")
vba ms-word word-vba
UsingIf Dir(filepath)
, you could keep appending Copy to the end of the file until there's no file namedfilepath
found.
– Lodi
Nov 21 '18 at 16:18
Or even sum + 1 at a index sufix on the pathfile.
– Lodi
Nov 21 '18 at 16:21
1
You don't need the .add. Just be aware that each time you save the document the document name is updated to match the filename you have just used for saving so if you want to retain your original filename, save the .name in a variable and then do a final .saveas2 using .name to restore your original filename (although the file will be the final edited version)
– Freeflow
Nov 21 '18 at 16:24
I’m sorry Lodi, but I’m not sure I understand this quite yet. Would it be possible for you to clarify how this would save a new file without closing the active document?
– Miguel Ortiz
Nov 21 '18 at 16:24
I edited my question seeing that the old question wasn’t conveying my problem effectively.
– Miguel Ortiz
Nov 21 '18 at 16:28
add a comment |
Hi I have a word file that makes a change to a table using VBA. After every change, I would like to save a new file with these changes. This way I have 10 copies with 10 different tables.
But currently, when I perform the code below, it exits the original, and thereby exiting the loop. Excel has a method SaveCopyAs, which Word doesn't have. Are there any ways to workaround this?
Documents.Add.SaveAs2 FileName:=("C:UsersXXXDownloadsBaby
Shower Table Games_Updated_" & x & ".docm")
vba ms-word word-vba
Hi I have a word file that makes a change to a table using VBA. After every change, I would like to save a new file with these changes. This way I have 10 copies with 10 different tables.
But currently, when I perform the code below, it exits the original, and thereby exiting the loop. Excel has a method SaveCopyAs, which Word doesn't have. Are there any ways to workaround this?
Documents.Add.SaveAs2 FileName:=("C:UsersXXXDownloadsBaby
Shower Table Games_Updated_" & x & ".docm")
vba ms-word word-vba
vba ms-word word-vba
edited Nov 21 '18 at 16:27
Miguel Ortiz
asked Nov 21 '18 at 16:13
Miguel OrtizMiguel Ortiz
477
477
UsingIf Dir(filepath)
, you could keep appending Copy to the end of the file until there's no file namedfilepath
found.
– Lodi
Nov 21 '18 at 16:18
Or even sum + 1 at a index sufix on the pathfile.
– Lodi
Nov 21 '18 at 16:21
1
You don't need the .add. Just be aware that each time you save the document the document name is updated to match the filename you have just used for saving so if you want to retain your original filename, save the .name in a variable and then do a final .saveas2 using .name to restore your original filename (although the file will be the final edited version)
– Freeflow
Nov 21 '18 at 16:24
I’m sorry Lodi, but I’m not sure I understand this quite yet. Would it be possible for you to clarify how this would save a new file without closing the active document?
– Miguel Ortiz
Nov 21 '18 at 16:24
I edited my question seeing that the old question wasn’t conveying my problem effectively.
– Miguel Ortiz
Nov 21 '18 at 16:28
add a comment |
UsingIf Dir(filepath)
, you could keep appending Copy to the end of the file until there's no file namedfilepath
found.
– Lodi
Nov 21 '18 at 16:18
Or even sum + 1 at a index sufix on the pathfile.
– Lodi
Nov 21 '18 at 16:21
1
You don't need the .add. Just be aware that each time you save the document the document name is updated to match the filename you have just used for saving so if you want to retain your original filename, save the .name in a variable and then do a final .saveas2 using .name to restore your original filename (although the file will be the final edited version)
– Freeflow
Nov 21 '18 at 16:24
I’m sorry Lodi, but I’m not sure I understand this quite yet. Would it be possible for you to clarify how this would save a new file without closing the active document?
– Miguel Ortiz
Nov 21 '18 at 16:24
I edited my question seeing that the old question wasn’t conveying my problem effectively.
– Miguel Ortiz
Nov 21 '18 at 16:28
Using
If Dir(filepath)
, you could keep appending Copy to the end of the file until there's no file named filepath
found.– Lodi
Nov 21 '18 at 16:18
Using
If Dir(filepath)
, you could keep appending Copy to the end of the file until there's no file named filepath
found.– Lodi
Nov 21 '18 at 16:18
Or even sum + 1 at a index sufix on the pathfile.
– Lodi
Nov 21 '18 at 16:21
Or even sum + 1 at a index sufix on the pathfile.
– Lodi
Nov 21 '18 at 16:21
1
1
You don't need the .add. Just be aware that each time you save the document the document name is updated to match the filename you have just used for saving so if you want to retain your original filename, save the .name in a variable and then do a final .saveas2 using .name to restore your original filename (although the file will be the final edited version)
– Freeflow
Nov 21 '18 at 16:24
You don't need the .add. Just be aware that each time you save the document the document name is updated to match the filename you have just used for saving so if you want to retain your original filename, save the .name in a variable and then do a final .saveas2 using .name to restore your original filename (although the file will be the final edited version)
– Freeflow
Nov 21 '18 at 16:24
I’m sorry Lodi, but I’m not sure I understand this quite yet. Would it be possible for you to clarify how this would save a new file without closing the active document?
– Miguel Ortiz
Nov 21 '18 at 16:24
I’m sorry Lodi, but I’m not sure I understand this quite yet. Would it be possible for you to clarify how this would save a new file without closing the active document?
– Miguel Ortiz
Nov 21 '18 at 16:24
I edited my question seeing that the old question wasn’t conveying my problem effectively.
– Miguel Ortiz
Nov 21 '18 at 16:28
I edited my question seeing that the old question wasn’t conveying my problem effectively.
– Miguel Ortiz
Nov 21 '18 at 16:28
add a comment |
2 Answers
2
active
oldest
votes
The code below demonstrates what you need to know.
Sub test()
Dim my_doc As Word.Document
Dim my_index As Long
Dim my_initial_name As String
Set my_doc = ActiveDocument
my_initial_name = my_doc.FullName
For my_index = 1 To 10
'<OTHER ACTIONS BEFORE SAVING>
my_doc.SaveAs2 FileName:="C:UsersXXXDownloadsBaby Shower Table Games_Updated_" & CStr(my_index) & ".docm"
Next
my_doc.SaveAs2 FileName:=my_initial_name
End Sub
After the first my_doc.SaveAs2, the vba code stops running, and Word hangs when executing the second my_doc.SaveAs2. This appears to be doing the same thing as my code + Word hanging up.
– Miguel Ortiz
Nov 21 '18 at 17:34
Works fine on my pc. You WILL need to edit the file path to one that actually exists on your PC.
– Freeflow
Nov 21 '18 at 18:04
Thanks @Freeflow I really appreciate your help. My error was elsewhere that was causing the file to be stuck in an infinite loop, but your code does the trick to save files like I wanted.
– Miguel Ortiz
Nov 22 '18 at 0:16
add a comment |
Maybe you shoud use FileCopy
instead .SaveAs
.
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%2f53416223%2fword-vba-save-multiple-files%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
The code below demonstrates what you need to know.
Sub test()
Dim my_doc As Word.Document
Dim my_index As Long
Dim my_initial_name As String
Set my_doc = ActiveDocument
my_initial_name = my_doc.FullName
For my_index = 1 To 10
'<OTHER ACTIONS BEFORE SAVING>
my_doc.SaveAs2 FileName:="C:UsersXXXDownloadsBaby Shower Table Games_Updated_" & CStr(my_index) & ".docm"
Next
my_doc.SaveAs2 FileName:=my_initial_name
End Sub
After the first my_doc.SaveAs2, the vba code stops running, and Word hangs when executing the second my_doc.SaveAs2. This appears to be doing the same thing as my code + Word hanging up.
– Miguel Ortiz
Nov 21 '18 at 17:34
Works fine on my pc. You WILL need to edit the file path to one that actually exists on your PC.
– Freeflow
Nov 21 '18 at 18:04
Thanks @Freeflow I really appreciate your help. My error was elsewhere that was causing the file to be stuck in an infinite loop, but your code does the trick to save files like I wanted.
– Miguel Ortiz
Nov 22 '18 at 0:16
add a comment |
The code below demonstrates what you need to know.
Sub test()
Dim my_doc As Word.Document
Dim my_index As Long
Dim my_initial_name As String
Set my_doc = ActiveDocument
my_initial_name = my_doc.FullName
For my_index = 1 To 10
'<OTHER ACTIONS BEFORE SAVING>
my_doc.SaveAs2 FileName:="C:UsersXXXDownloadsBaby Shower Table Games_Updated_" & CStr(my_index) & ".docm"
Next
my_doc.SaveAs2 FileName:=my_initial_name
End Sub
After the first my_doc.SaveAs2, the vba code stops running, and Word hangs when executing the second my_doc.SaveAs2. This appears to be doing the same thing as my code + Word hanging up.
– Miguel Ortiz
Nov 21 '18 at 17:34
Works fine on my pc. You WILL need to edit the file path to one that actually exists on your PC.
– Freeflow
Nov 21 '18 at 18:04
Thanks @Freeflow I really appreciate your help. My error was elsewhere that was causing the file to be stuck in an infinite loop, but your code does the trick to save files like I wanted.
– Miguel Ortiz
Nov 22 '18 at 0:16
add a comment |
The code below demonstrates what you need to know.
Sub test()
Dim my_doc As Word.Document
Dim my_index As Long
Dim my_initial_name As String
Set my_doc = ActiveDocument
my_initial_name = my_doc.FullName
For my_index = 1 To 10
'<OTHER ACTIONS BEFORE SAVING>
my_doc.SaveAs2 FileName:="C:UsersXXXDownloadsBaby Shower Table Games_Updated_" & CStr(my_index) & ".docm"
Next
my_doc.SaveAs2 FileName:=my_initial_name
End Sub
The code below demonstrates what you need to know.
Sub test()
Dim my_doc As Word.Document
Dim my_index As Long
Dim my_initial_name As String
Set my_doc = ActiveDocument
my_initial_name = my_doc.FullName
For my_index = 1 To 10
'<OTHER ACTIONS BEFORE SAVING>
my_doc.SaveAs2 FileName:="C:UsersXXXDownloadsBaby Shower Table Games_Updated_" & CStr(my_index) & ".docm"
Next
my_doc.SaveAs2 FileName:=my_initial_name
End Sub
answered Nov 21 '18 at 17:20


FreeflowFreeflow
551128
551128
After the first my_doc.SaveAs2, the vba code stops running, and Word hangs when executing the second my_doc.SaveAs2. This appears to be doing the same thing as my code + Word hanging up.
– Miguel Ortiz
Nov 21 '18 at 17:34
Works fine on my pc. You WILL need to edit the file path to one that actually exists on your PC.
– Freeflow
Nov 21 '18 at 18:04
Thanks @Freeflow I really appreciate your help. My error was elsewhere that was causing the file to be stuck in an infinite loop, but your code does the trick to save files like I wanted.
– Miguel Ortiz
Nov 22 '18 at 0:16
add a comment |
After the first my_doc.SaveAs2, the vba code stops running, and Word hangs when executing the second my_doc.SaveAs2. This appears to be doing the same thing as my code + Word hanging up.
– Miguel Ortiz
Nov 21 '18 at 17:34
Works fine on my pc. You WILL need to edit the file path to one that actually exists on your PC.
– Freeflow
Nov 21 '18 at 18:04
Thanks @Freeflow I really appreciate your help. My error was elsewhere that was causing the file to be stuck in an infinite loop, but your code does the trick to save files like I wanted.
– Miguel Ortiz
Nov 22 '18 at 0:16
After the first my_doc.SaveAs2, the vba code stops running, and Word hangs when executing the second my_doc.SaveAs2. This appears to be doing the same thing as my code + Word hanging up.
– Miguel Ortiz
Nov 21 '18 at 17:34
After the first my_doc.SaveAs2, the vba code stops running, and Word hangs when executing the second my_doc.SaveAs2. This appears to be doing the same thing as my code + Word hanging up.
– Miguel Ortiz
Nov 21 '18 at 17:34
Works fine on my pc. You WILL need to edit the file path to one that actually exists on your PC.
– Freeflow
Nov 21 '18 at 18:04
Works fine on my pc. You WILL need to edit the file path to one that actually exists on your PC.
– Freeflow
Nov 21 '18 at 18:04
Thanks @Freeflow I really appreciate your help. My error was elsewhere that was causing the file to be stuck in an infinite loop, but your code does the trick to save files like I wanted.
– Miguel Ortiz
Nov 22 '18 at 0:16
Thanks @Freeflow I really appreciate your help. My error was elsewhere that was causing the file to be stuck in an infinite loop, but your code does the trick to save files like I wanted.
– Miguel Ortiz
Nov 22 '18 at 0:16
add a comment |
Maybe you shoud use FileCopy
instead .SaveAs
.
add a comment |
Maybe you shoud use FileCopy
instead .SaveAs
.
add a comment |
Maybe you shoud use FileCopy
instead .SaveAs
.
Maybe you shoud use FileCopy
instead .SaveAs
.
answered Nov 29 '18 at 19:48
dekudeku
155
155
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%2f53416223%2fword-vba-save-multiple-files%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
Using
If Dir(filepath)
, you could keep appending Copy to the end of the file until there's no file namedfilepath
found.– Lodi
Nov 21 '18 at 16:18
Or even sum + 1 at a index sufix on the pathfile.
– Lodi
Nov 21 '18 at 16:21
1
You don't need the .add. Just be aware that each time you save the document the document name is updated to match the filename you have just used for saving so if you want to retain your original filename, save the .name in a variable and then do a final .saveas2 using .name to restore your original filename (although the file will be the final edited version)
– Freeflow
Nov 21 '18 at 16:24
I’m sorry Lodi, but I’m not sure I understand this quite yet. Would it be possible for you to clarify how this would save a new file without closing the active document?
– Miguel Ortiz
Nov 21 '18 at 16:24
I edited my question seeing that the old question wasn’t conveying my problem effectively.
– Miguel Ortiz
Nov 21 '18 at 16:28