I'm stuck on a VBA project I have going on. At the end of the code I have it ask "Adding another user?" And give a yes or no option. If I choose no it just closes out of the word document without saving and that works fine. If I choose yes though I'd like it to remove all the changes done to the document and run the VBA sub again.
The changes I make to the document are all from input boxes in the VBA Code and it never actually saves the document, so I'm able to run
Documents.Open Filename:=ActiveDocument.FullName, Revert:=True
And that gets rid of all the changes quickly without having to save the document. It goes back to the original document quickly with that command but the VBA Code always stops, even if it's set to an AutoOpen sub or it's own subroutine that I call. I've tried a few different ways around it but can't seem to get the code to continue to run. Does someone have a better solution for what I'm trying to do? Maybe a custom undo record would be best? Right now when the yes option is picked to add another user it runs undo 200 times and then calls the sub. It works fine but I'd like this document to be more scalable.
CodePudding user response:
If you put this code into another document, you get the answer yes to your question! Of cause Word keeps the VBA Code running after reopening the other document!
- editedDoc.docx
- TheMacroDoc.docm
vba code in TheMacroDoc.docm:
Sub ReloadEditDoc()
debug.print "The ActiveDocument is : "; ActiveDocument.FullName
Documents.Open FileName:=ActiveDocument.FullName, Revert:=True
Debug.Print "Done!"
MsgBox ("Done")
End Sub
-----------------------
Immediate Window:
The ActiveDocument is : editDoc.docx
Done!
Update 24.02.2022 09:32 UTC: Whoops, I should've been more clear, sorry about that. When yes is selected to add another user I'm wanting to re-open the same document, make all the changes go away, and edit it again ...
this is what I've shown.
without needing another word document.
Could you please explain why you have this limit?
All of the changes to the document are made with find and replace using user input, and then it gets exported to a PDF. After exporting it that's when it asks if you'd like to add more users. I could possibly have the code save a temporary document and edit the temporary document instead? I tried doing that and felt like I got close but couldn't make it work.
Either you work with a temporary document or you have to transfer somehow the actual status to a new start! What is the diffence between the "re-open" situation and just open the document in the first step? What are the next actions you want to perform?
CodePudding user response:
The temporary document variant
Sub ReloadEditDoc()
Dim FileName_Old_Version As String
Debug.Print
Debug.Print "Step 1 - Active: "; ActiveDocument.Name
Debug.Print "Step 1 - Save a copy of your ""EditDoc""!"
ActiveDocument.Save
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
FileName_Old_Version = Replace(ActiveDocument, ".doc", "_OLD.doc")
'if you run this a 2nd time there is already an "OLD" version
On Error Resume Next
ChDir ActiveDocument.Path
fso.CopyFile ActiveDocument, FileName_Old_Version, True
On Error GoTo 0
Debug.Print "Step 1 - Done"
Debug.Print
Debug.Print " ****************************************"
Debug.Print " **"
Debug.Print " ** Do your initial tasks and ..."
Debug.Print " ** ask ""Adding another user?"""
Debug.Print " ** acc. to your question!"
Debug.Print " **"
Debug.Print " ****************************************"
Debug.Print
Debug.Print
Debug.Print "Step 2 - We open the ""OLD"" version."
Debug.Print "Step 2 - Active: "; ActiveDocument.Name
ChDir ActiveDocument.Path
Documents.Open FileName_Old_Version
Debug.Print "Step 2 - The ""OLD"" version is open now."
Debug.Print "Step 2 - Active: "; ActiveDocument.Name & " !!!!"
Debug.Print "Step 2 - Done"
Debug.Print
Debug.Print "Step 3 - Active: "; ActiveDocument.Name
Debug.Print "Step 3 - Here YOU Write the next steps on the OLD version!"
'Your next steps - Step 1
'Your next steps - Step 2
'Your next steps - Step 3
'Your next steps - ...
Debug.Print
Debug.Print "remark - ThisDoc: "; ThisDocument.Name
Debug.Print "remark - The Macro is still ""running"" in edited version of the document!"
Debug.Print " Closing and reverting this document earlier will stop the program!"
'Save the changes on the file "FileName_Old_Version"
Documents(FileName_Old_Version).Save
Documents(FileName_Old_Version).Close
Stop
ThisDocument.Close saveChanges:=False
End Sub
Immediate window:
Step 1 - Active: editDoc.docm
Step 1 - Save a copy of your "EditDoc"!
Step 1 - Done
****************************************
**
** Do your initial tasks and ...
** ask "Adding another user?"
** acc. to your question!
**
****************************************
Step 2 - We open the "OLD" version.
Step 2 - Active: editDoc.docm
Step 2 - The "OLD" version is open now.
Step 2 - Active: editDoc_OLD.docm !!!!
Step 2 - Done
Step 3 - Active: editDoc_OLD.docm
Step 3 - Here are the next steps on the OLD version!
remark -ThisDoc: editDoc.docm
remark - The Macro is still "running" in edited version of the document!
Closing and reverting this document earlier will stop the program!