Word VBA - Save Multiple Files












0















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")











share|improve this question

























  • 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






  • 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
















0















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")











share|improve this question

























  • 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






  • 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














0












0








0








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")











share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 16:27







Miguel Ortiz

















asked Nov 21 '18 at 16:13









Miguel OrtizMiguel Ortiz

477




477













  • 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






  • 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











  • 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












2 Answers
2






active

oldest

votes


















1














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





share|improve this answer
























  • 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





















0














Maybe you shoud use FileCopy instead .SaveAs.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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





    share|improve this answer
























    • 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


















    1














    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





    share|improve this answer
























    • 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
















    1












    1








    1







    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





    share|improve this answer













    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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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





















    • 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















    0














    Maybe you shoud use FileCopy instead .SaveAs.






    share|improve this answer




























      0














      Maybe you shoud use FileCopy instead .SaveAs.






      share|improve this answer


























        0












        0








        0







        Maybe you shoud use FileCopy instead .SaveAs.






        share|improve this answer













        Maybe you shoud use FileCopy instead .SaveAs.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 19:48









        dekudeku

        155




        155






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            Npm cannot find a required file even through it is in the searched directory

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith