PDA

View Full Version : Solved: Possible to reset/reload a document to blank?



Krullpes-FM
02-21-2006, 04:03 AM
Hi, great forum, very helpful to me so far, but I have a question

I have a Word template which uses two userforms for text entry into bookmarks. The two forms are for entering two different sets of information about the recipient and the sender respectively.

What I would like to be able to do is allow users to reset a userform and for that reset to clear the document of the text input through the form.

Is there some kind of reset code to return the document to its original template format? or would I need to close the document and load a new one up without closing the userform?

I'm not sure if what I would like to do is possible, but any help would be much appreciated.

Rgds

Dave
02-21-2006, 06:42 AM
This will clear a document. I'm not sure re. returning it to its' original template format. HTH. Dave

Dim Wapp As Object
Set Wapp = CreateObject("Word.Application")
Wapp.activedocument.Select
With Wapp.activedocument
.Range(0, .Characters.Count).Delete
End With

TonyJollans
02-21-2006, 07:05 AM
There is no way to say "reinitialise me" but you should be able to mimic it with ...DocumentRef.Undo 999

TonyJollans
02-21-2006, 07:30 AM
This will clear a document. I'm not sure re. returning it to its' original template format. HTH. Dave

Dim Wapp As Object
Set Wapp = CreateObject("Word.Application")
Wapp.activedocument.Select
With Wapp.activedocument
.Range(0, .Characters.Count).Delete
End With


Ignoring the unnecessary code, and without wishing to be too critical, there are a couple of problems with this.

1. It will not delete, for example, header content
2. It will delete document content that was in the original template

Dave
02-21-2006, 07:36 AM
Thanks for the info Tony. Didn't know re. header content and I had my suspicions that the original template may be affected. Have a nice day. Dave

TonyJollans
02-21-2006, 08:23 AM
Hi Dave,

You did say you were unsure about the 'back to template' aspect of it. I have to say, it's a nice idea for a feature but just undoing all updates is the only way I can think of to achieve it.

Krullpes-FM
02-21-2006, 12:04 PM
Absolutely great stuff Tony and Dave, thanks for the help. ActiveDocument.Undo 999 works like a charm!

I tested it on its own with a reset command button, it worked well but I found that the changes did not always undo if I left them. (I presume this is meant to happen?!), anyway I added it to my print command, which will be the final useraction and it works great. The code looks like this:


Private Sub cmdPrint_Click()
With ActiveDocument
.Bookmarks("bkSenderName").Range.Text = txtSenderName.Value
.Bookmarks("bkReceiverName").Range.Text = textReceiverName.Value
'etc etc etc to update all bookmarks

Options.UpdateFieldsAtPrint = True
ActiveDocument.PrintOut
ActiveDocument.Undo 999
End Sub


I was just after the ability to undo the changes caused by the userforms/bookmarks so that the template could be reused without quitting, so sorry for the confusion.

I've been looking into this for a couple of days trying to work it out and lost the ability to express what I was after properly.

Thanks for the help guys.

TonyJollans
02-21-2006, 12:16 PM
Glad you're sorted! And Welcome to VBAX, by the way!

fumei
02-22-2006, 10:55 PM
I have to state that this is an improper use of a template. If in fact you are using an initial .dot file. If you are clearing an opened file back to its original state, you may as well use a DOCUMENT, not a template.

Further, a proper use of a template does not have values, unless of course they are default values. Are you saying...a user opens the file, changes something ( does an input), SAVES the file AS IT IS.....then reopens the file, changes something...you want it to revert back to the LAST saved change?

Again, this is an improper use of templates - if that is what it is.

Further, as there is consistent mis-use of the term "bookmark" I have to ask, are these real bookmarks, or are they formfields? Formfields allow direct user input, bookmarks do not. Because if they ARE formfields, you can revert them to default entries by code. But if they were previously changed from the default then of course they would still revert to default....but then that is, in fact, the point of a template.

fumei
02-22-2006, 10:59 PM
Another thought...if you want to return to the opening values...why not just not save it?

Also I notice the values appear to be coming from textboxes, quite possibly from a userform. Are you saying the textboxes have default values?

Krullpes-FM
02-23-2006, 07:34 AM
Fumei,

To answer you questions:

I set it up as a .dot because as I understand it its necessary to allow the use of a userform when a document is created and because I want multiple users to be able to use it all at once.

The document is a standard wording letter with a number of variables, including the sender and recipient. The variables are marked with bookmarks to allow the userform to insert text in the correct place (I followed the guide on www.fontsuff.com (http://www.fontsuff.com/)). There are no fields in the document.

The user will open a new document based on the template, the userform will run, asking them to input the necessary information, they can then click on print, which inserts the userform values at the correct bookmarks and then, thanks to Tony, runs undo on the changes the code made and to allow the re-use of the document without needing to start it up again so that different recipients and the other variables could be changed as needed and a new letter printed.

The user will not be able to save a copy, as I found that this causes input errors and omissions because users are reusing old documents and missing necessary changes, so I wanted to prevent this.

I hope that makes it clearer for you.

fumei
02-23-2006, 08:20 AM
Yes, that is clearer. And from that I will reiterate....this is NOT a template. This is a document that is being reused.

1. the fact you are preventing a save immediately makes this a document that is being reused.

2. template are used to make NEW documents, while leaving the template alone. That is their primary purpose. If you are not using it to make new documents then you do not need to have it as a .dot file (template).


I set it up as a .dot because as I understand it its necessary to allow the use of a userform when a document is createdThis is incorrect.

3. documents can open with a userform. Not a problem.

4. documents can be set to be Read-Only.

However, you DO have it correct on one thing. If you really are going to have users actioning the document AT THE SAME TIME, but literally the same time, then yes, a template would be better. There are issues with the same file being opened simultaneous by different users. But that is the ONLY reason I can see for using a template file for this purpose.

Further, this is only going to work if you DO disable any Save.

You did not answer the question about if there default values. If there are no default values, and you ARE diaabling Saves, then again...why reset the values? If the values are nothing, and the user can not save, then.....any changes can NOT be saved. They close the file....done.

However, are you talking about a user (A) doing stuff, changing values....and then user (B) coming along and on the same computer with the SAME file open, wanting to input and print different values. if so, sure, you want to clear the values.

But if it is on a different computer then they never CAN get the other values - as the file can not be saved.

Are you saying that this file is for input and print ONLY????? NOTHING the user inputs is ever saved. Again, is it input and print ONLY? If it is that, then OK I can sort of see this. But if it is for anything else....nope, this is an improper use of templates.;