temporary copy of current word document

P

pbreah

hello everyone,

I'm trying to make a "backup" copy of the document before the code
(macro) makes changes in the current document. The macro I made
replaces many things and reformats the document. With this "backup"
copy I can revert back to the original if the user doesn't like the end
result of the macro. In other words an "undo" for all the changes the
macro made.

I thought of going with this approach in saving a backup copy, but if
anyone knows an easier way, I'll be happy to implement it.

My approach was:

Macro-Reformat
save "backup file"
run code that changes the document
End Macro-Reformat

Macro-Undo
retrieve backup document
assign backup document to the current document
delete backup document
End Macro-Undo

Any suggestions or code to accomplish this?

Thanks
 
J

Jezebel

The method you describe is reasonable: save a backup copy, and revert to it
if necessary. You haven't said what your problem is with this (if any).

An alternative is to use Word's own undo/redo method: at the start of your
macro you do a 'marker' action (such as defining a bookmark); then to
reverse your macro you call the undo method repeatedly until the marker
action is reversed (eg until the bookmark is not defined).
 
P

pbreah

Sounds good, but do you have sample code on how to do this on one of
the options (the file backup or the bookmark)? This is the second
time I write code for a macro in word, I don't have much expeience on
this.

Thanks...
 
J

Jezebel

If you're a complete novice, the second method is likely to be too tricky.
The file save method is just that: save the file, open the file. Do your
best and post again if you get into difficulties.
 
P

pbreah

For me right now both are tricky. I tried to use:

Dim backup as Word.Document

But I don't know how to assign a name to the file and save the
ActiveDocument in it. After I save the file, how do I access the
contents of the backup file and assign it to the current document (the
undo macro).

I have long hours trying to get this working, but nothing yet. :-(

Thank you
 
J

Jezebel

The file IS the document.

Const pFName as string = "C:\.......\Backup.doc"

1. Save the current file
ActiveDocument.SaveAs FileName:=pFName


2. Close the current file and revert
ActiveDocument.Close SaveChanges:=FALSE
Documents.Open FileName:=pFName
 
P

pbreah

Thanks Jezabel

I'm going to try this code. But I don't want to use the backup copy of
the file. I just want the backup copy as a temp storage. If the user
chooses to undo, the undo macro would read this file and copy it back
to the original.

It could be very confusing for the user to see his/her file name
renamed like that.

Thank you...
 
R

Russ

Pbreah,
Slightly tweaking Jezebel's code you can:

Dim Fname as String

'First before your changes are made get full pathname of the currently open
'file that was previously saved before at least once.
Fname = ActiveDocument.Fullname


'Later close the currently changed file without saving, if wanted, and
'reopen original file, if needed.

ActiveDocument.Close SaveChanges:=FALSE
Documents.Open FileName:=FName
 
P

pbreah

Thank you Russ,

This is a better choice for keeping the file name and path. If you
would approach this differently using some other method, I would be
happy to know also.

I appreciate all responses so far...

Pbreah
 
P

pbreah

One problem I'm seeing is that this is not going to work if the user
starts a new document and the file was not saved. The undo would close
without saving and would erase your document.

There are two macros:

Macro 1 ----> To save the document (or bookmark it) and reformat

Macro 2 -----> Undo the Reformat.

Going with the file save approach, there is a need to keep the file
name and path variable accesible to both macros.

Any new ideas or code are welcome

Pbreah
 
R

Russ

Pbreah,
See new message within previous text below.
One problem I'm seeing is that this is not going to work if the user
starts a new document and the file was not saved. The undo would close
without saving and would erase your document.

There are two macros:

Macro 1 ----> To save the document (or bookmark it) and reformat

Macro 2 -----> Undo the Reformat.

Going with the file save approach, there is a need to keep the file
name and path variable accesible to both macros.
As long as the first macro is done before the second, no change is needed
because the first macro saves the file the required at least once before any
changes presumably.

But, to keep the file-name-and-path variable accessible to all macros
declare that "Fname" variable as "Public" in the first macro in which case
the first macro must be done before the others in the Project or put the
declaration in the Declarations section of the Project in the Code Window of
the editor.

Search for "Declaring Variables" in Visual Basic Editor Help Window to learn
how to give a variable global scope.
Any new ideas or code are welcome

Pbreah
 
P

pbreah

Thanks Russ, I will. I think with this the program should be working.
If anything I'll post again. I have to say there are a lot good people
here such as yourself in these groups.

Thank you for everything... :)

Pbreah
Pbreah,
See new message within previous text below.
One problem I'm seeing is that this is not going to work if the user
starts a new document and the file was not saved. The undo would close
without saving and would erase your document.

There are two macros:

Macro 1 ----> To save the document (or bookmark it) and reformat

Macro 2 -----> Undo the Reformat.

Going with the file save approach, there is a need to keep the file
name and path variable accesible to both macros.
As long as the first macro is done before the second, no change is needed
because the first macro saves the file the required at least once before any
changes presumably.

But, to keep the file-name-and-path variable accessible to all macros
declare that "Fname" variable as "Public" in the first macro in which case
the first macro must be done before the others in the Project or put the
declaration in the Declarations section of the Project in the Code Window of
the editor.

Search for "Declaring Variables" in Visual Basic Editor Help Window to learn
how to give a variable global scope.
 
R

Russ

My pleasure.
I agree that there are a lot of good folks in these groups.
I say thanks to them also. I learn a lot by reading these forums.
Thanks Russ, I will. I think with this the program should be working.
If anything I'll post again. I have to say there are a lot good people
here such as yourself in these groups.

Thank you for everything... :)

Pbreah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top