PDA

View Full Version : Solved: Clearing Text Entered at Bookmarks



nickirvine
06-10-2008, 06:00 AM
I have created a form in VB for word that allows you to enter details into a form and then they are placed on a letter template in word where I have placed bookmarks.


I then want the text entered at the bookmarks to be deleted so I can use the form again and again. Can someone tell me how to delete the text created at the bookmarks. Or to reset the letter to clear everything entered.

I'm using the code:


Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
With ActiveDocument

.Bookmarks("title").Range.Text = title.Value
.Bookmarks("Address").Range.Text = Address.Value
.Bookmarks("NINO").Range.Text = NINO.Value
.Bookmarks("NINO2").Range.Text = NINO.Value
.Bookmarks("EndDate").Range.Text = chbenddate.Value
.Bookmarks("Processor").Range.Text = Processor.Value
.Bookmarks("fullname").Range.Text = fullname.Value
.Bookmarks("fullname2").Range.Text = fullname.Value
End With
Application.ScreenUpdating = False
Unload Me

ActiveDocument.PrintOut Background:=False


chblettergen.Show

End Sub

I have used this as on another thread to try and clear the fields. But it doenst work



With ActiveDocument
.Bookmarks("title").Range.Text = ""
.Bookmarks("Address").Range.Text = ""
.Bookmarks("NINO").Range.Text = ""
.Bookmarks("NINO2").Range.Text = ""
.Bookmarks("Processor").Range.Text = ""
.Bookmarks("EndDate").Range.Text = ""
.Bookmarks("fullname").Range.Text = ""
.Bookmarks("fullname2").Range.Text = ""

End With
Any help is much appreciated. Thanks, Nick

Edit: VBA tags added to code

lucas
06-10-2008, 06:48 AM
Nick,
Have you considered saving the file as a template. If you do that then you clone a new instance of it each time and the original template is left intact to use again and again. You will have to put your code to show your userform in document new.

nickirvine
06-10-2008, 06:59 AM
Hi Steve,

How do you mean? I already have the file as a template and have built the form within the template, so when the template is opened the form opens. I thought about getting vb to close the file and reopen it but because it will be on shared computer and lots of people will be using it, it will cause problems.

I just want a simple reset code or reload code or something. I've done loads of php/mysql coding just never vb so im a bit of newbie.

I'm sure there must be a way around it, even if it is long winded. Perhaps even getting vb to regenerate the whole letter? or to count the number of characters added and then delete that number of characters from point a-b?

Nick

lucas
06-10-2008, 07:02 AM
Nick, When you post code you can select it and hit the vba button to get it to be formatted as I have done in your first post. If a template is not a good idea for you could you post your document so we don't have to recreate it please.

lucas
06-10-2008, 07:07 AM
What if you recall the userform and can clear it or change the text in the bookmarks from the userform....move the userform a little to see the second bookmark.....see attached.

Again if this is not close to what you need then it would help if you posted an example of your problem so we don't have to build it from scratch.

nickirvine
06-10-2008, 07:07 AM
Hi,

Sorry i missed the vba button.

I can't post the whole document. Due to restrictions at my work. This is the whole code:


Private Sub UserForm_Initialize()
With title
.AddItem "Mrs"
.AddItem "Mr"
.AddItem "Miss"
.AddItem "Ms"
.AddItem "Dr"
.AddItem "Rev"
End With

chbenddate.Value = "1st September 2008"

End Sub

Private Sub CommandButton3_Click()
Unload Me
ActiveDocument.Close SaveChanges:=False
End Sub

Private Sub CommandButton2_Click()
fullname.Value = Null
Address.Value = Null
NINO.Value = Null
chbenddate.Value = "1st September 2008"
End Sub

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
With ActiveDocument

.Bookmarks("title").Range.Text = title.Value
.Bookmarks("title2").Range.Text = title.Value
.Bookmarks("Address").Range.Text = Address.Value
.Bookmarks("NINO").Range.Text = NINO.Value
.Bookmarks("NINO2").Range.Text = NINO.Value
.Bookmarks("EndDate").Range.Text = chbenddate.Value
.Bookmarks("Processor").Range.Text = Processor.Value
.Bookmarks("fullname").Range.Text = fullname.Value
.Bookmarks("fullname2").Range.Text = fullname.Value
.Bookmarks("fullname3").Range.Text = fullname.Value
End With
Application.ScreenUpdating = False
Unload Me

ActiveDocument.PrintOut Background:=False

chblettergen.Show


End Sub


This runs as the form chblettergen. That is running off a file with this code in it:

Private Sub Document_New()
chblettergen.Show
End Sub

So basically that just opens the form - which is the top bit of code. In the actual letter it has bookmarks that you see above "title" , "NINO" , "address" etc.

I hope that makes sense and makes up for me not being able to attach the file. Thanks for your help.

nickirvine
06-10-2008, 07:14 AM
That does seem to work thanks. how have you entered the fields on the document? they dont appear to be bookmarks, have you entered them as something else?

Is there anyway you could adapt that code for me a little...just so that you click ok it prints it and then clears the fields?

lucas
06-10-2008, 07:24 AM
Nick, If you go to insert-bookmark you will see that they are indeed bookmarks. The difference in the two methods is that my code actually puts the text into the bookmark and yours adds it after the bookmark.

Give me a minute on your request....

nickirvine
06-10-2008, 07:26 AM
absolutley brilliant yeah i see.

awaiting your next post. thanks loads for your help. I'll be donating to your site!

lucas
06-10-2008, 07:48 AM
I only tested this once because I didn't want to waste a lot of ink but it seems to work.....let me know.

nickirvine
06-10-2008, 07:52 AM
Works, absoulutly brilliant. thanks for your help. been trying to get it sorted all day. When I get home I'll show my appreciation through paypal!

Thanks again

lucas
06-10-2008, 07:55 AM
Glad to help Nick. Be sure to mark your thread solved using the thread tools at the top of the page.

fumei
06-10-2008, 09:08 AM
Nick, I would agree with Steve's original suggestion.

Template are not meant to "opened". In fact, by users, template should NEVER be "opened".

A template is meant to be cloned. That is, a copy is made.

Therefore, each copy is fresh, new. The original (the template itself, the .dot file) is not touched. It remains intact. Therefore, there is no need to clear anything.

Each new document starts fresh. This is the whole point of templates.

"How do you mean? I already have the file as a template and have built the form within the template, so when the template is opened the form opens."

You can have it so that when a new document is cloned from the template, your userform comes up. I do this all the time.

You use the Document_New event. You clone a new document (File > New) and choose the template. A new document is created based on the template. A copy.

The userform comes up automatically, and you go from there.

The template is not changed, so another new document will be, again, a fresh new copy.

lucas
06-10-2008, 09:15 AM
Seems to be a concept that few understand fully Gerry.....Maybe he will re-read this and reconsider the solution.....I should have persued it further on reviewing the thread.

fumei
06-10-2008, 09:33 AM
That is why I thought I would mention it again. I read the thread and thought I would re-iterate your original comment.

Actually, if the original request is dealt with: "I then want the text entered at the bookmarks to be deleted "

That is, ALL text at ALL Bookmarks deleted (or rather changed to "" as the code does), then using your Sub

Sub ClearEverything()
Dim oBM As Bookmark
For Each oBM In ActiveDocument.Bookmarks
Call wb(oBM.Name, "")
Next
End Sub

will call the replacing Sub for each bookmark, passing the bookmark name, and the empty string.


NOTE: If using this, I would definitely place the replacing Sub - wb...and odd name that BTW - in a standard module, NOT the userform module.

CreganTur
06-10-2008, 11:12 AM
Seems to be a concept that few understand fully Gerry.


That is why I thought I would mention it again

Have you guys considered writting a Template FAQ? I know that a lot of people would benefit from that (myself included), since it is an issue that most people don't fully understand. I know the site would greatly benefit from this, and it means less typing for both of you whenever someone has a template related issue.

Just a thought.

fumei
06-10-2008, 11:43 AM
Not a bad idea, although I have my doubts about it being useful in a general way. Individuals who want to know how to use Word better/well (such as yourself) may gain from such a FAQ. So, yes, it could be worth it.

Unfortunately, and maybe I am tired and cynical, the majority of people do not appear to really want to know how to use Word better/well.

I have been trying for 25 years to get even my colleagues - and I work in the Office products training field!!! - to use templates properly.

I have designed and built very effective templates for our own organization; have taught full week in-house courses on Word; have taught one day specialized courses on using templates; built on-line tutorials on our intranet covering both Word itself, and templates specifically...

and on and on

For 25 years I have been trying.

My guess is that less than 1% of our organization use templates properly. I see thousands of documents every year, and it is very very rare that I see anything other than Normal.dot as the template.

Crap. I just read the above. Yeah, I sound tired and cynical. Hmmm. I need a vacation.

But hey...I should put my money where my mouth is, and you all know how much I shoot my mouth off regarding templates...so, what the heck. I will give it a shot.

CreganTur
06-10-2008, 11:47 AM
...so, what the heck. I will give it a shot.

Awesome! Can't wait to see it:thumb

lucas
06-10-2008, 03:56 PM
Gerry, I think this is an excellent idea. We can use it to reference instead of repeating ourselves.....may I suggest you submit it as an article? Look under Site Links at the top of the page if it interests you.

nickirvine
06-11-2008, 02:56 AM
Thanks for your advice guys I agree. I will not make it a template and will change it. I was advised elsewhere to make it a template.

I have one quick final coding question, as I can't get the code to work as I have repeated variables. I.e you type in name in one field on the form and it is repeated three times on the letter. I need to repeat, the title, the field called NINO, and the full name.

However I cant get it to repeat. I have tried adapting the code to now avail. I guess it is due to the loop at the top.

Here is the code I have so far:

Option Explicit
Dim bname As String
Private Sub wb(bname, ByVal inhalt As String)

Dim r As Range
Set r = ActiveDocument.Bookmarks(bname).Range
r.Text = inhalt
ActiveDocument.Bookmarks.Add bname, r

End Sub
Private Sub UserForm_Initialize()
With title
.AddItem "Mrs"
.AddItem "Mr"
.AddItem "Miss"
.AddItem "Ms"
.AddItem "Dr"
.AddItem "Rev"
End With

title.SetFocus
title.Text = ActiveDocument.Bookmarks("title").Range.Text
fullname.Text = ActiveDocument.Bookmarks("fullname").Range.Text
Address.Text = ActiveDocument.Bookmarks("Address").Range.Text
NINO.Text = ActiveDocument.Bookmarks("NINO").Range.Text
NINO2.Text = NINO.Text
NINO2.Text = ActiveDocument.Bookmarks("NINO2").Range.Text
chbenddate.Text = ActiveDocument.Bookmarks("EndDate").Range.Text
Processor.Text = ActiveDocument.Bookmarks("Processor").Range.Text
fullname.Text = ActiveDocument.Bookmarks("fullname").Range.Text
fullname.Text = ActiveDocument.Bookmarks("fullname2").Range.Text
fullname.Text = ActiveDocument.Bookmarks("fullname3").Range.Text

chbenddate.Value = "1st September 2008"
End Sub



Private Sub CommandButton3_Click()
Unload Me
ActiveDocument.Close SaveChanges:=False
End Sub

Private Sub CommandButton2_Click()
fullname.Value = Null
Address.Value = Null
NINO.Value = Null
chbenddate.Value = "1st September 2008"
End Sub

Private Sub CommandButton1_Click()
ActiveDocument.PrintOut Background:=False
title.Value = ""
Address.Value = ""
NINO.Value = ""
chbenddate.Value = "1st September 2008"
fullname.Value = ""
Unload Me
chblettergen.Show
End Sub
Private Sub title_Change()
wb "title", title
End Sub
Private Sub title2_Change()
wb "title2", title
End Sub
Private Sub Address_Change()
wb "Address", Address
End Sub
Private Sub NINO_Change()
wb "NINO", NINO
End Sub
Private Sub NINO2_Change()
wb "NINO2", NINO2
End Sub
Private Sub chbenddate_Change()
wb "EndDate", chbenddate
End Sub
Private Sub Processor_Change()
wb "Processor", Processor
End Sub
Private Sub fullname_Change()
wb "fullname", fullname
End Sub
Private Sub fullname2_Change()
wb "fullname2", fullname
End Sub
Private Sub fullname3_Change()
wb "fullname3", fullname
End Sub


I'm sure im missing something really simple. Thanks again for all your help on this.

nickirvine
06-11-2008, 05:15 AM
ive sorted it. thanks again for all your help guys.

grahamd55
01-29-2016, 08:21 AM
That is why I thought I would mention it again. I read the thread and thought I would re-iterate your original comment.

Actually, if the original request is dealt with: "I then want the text entered at the bookmarks to be deleted "

That is, ALL text at ALL Bookmarks deleted (or rather changed to "" as the code does), then using your Sub

Sub ClearEverything()
Dim oBM As Bookmark
For Each oBM In ActiveDocument.Bookmarks
Call wb(oBM.Name, "")
Next
End Sub

will call the replacing Sub for each bookmark, passing the bookmark name, and the empty string.


NOTE: If using this, I would definitely place the replacing Sub - wb...and odd name that BTW - in a standard module, NOT the userform module.

Fumei, I am trying to do the same thing as Nick here. I have tried to implement the code you have posted here mixed with Nicks "wb" subroutine. It seems to work but I get caught in an infinite loop with my first Bookmark. It doesn't ever move to the next bookmark rather. It gets stuck in the For loop with oBM.Name being my first Bookmark. Any Suggestions?

gmayor
01-30-2016, 01:17 AM
This is a very old thread. The following will clear your bookmarks.
Option Explicit

Sub ClearBookmarks()
Dim i As Long
Dim strName As String
For i = 1 To ActiveDocument.Bookmarks.Count
strName = ActiveDocument.Bookmarks(i).Name
FillBM strName, ""
Next i
lbl_Exit:
Exit Sub
End Sub

Private Sub FillBM(strBMName As String, strValue As String)
'Graham Mayor
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strBMName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub
Checkout FillBM and other useful VBA functions on my web site.

grahamd55
02-01-2016, 06:44 AM
This is a very old thread. The following will clear your bookmarks.
Option Explicit

Sub ClearBookmarks()
Dim i As Long
Dim strName As String
For i = 1 To ActiveDocument.Bookmarks.Count
strName = ActiveDocument.Bookmarks(i).Name
FillBM strName, ""
Next i
lbl_Exit:
Exit Sub
End Sub

Private Sub FillBM(strBMName As String, strValue As String)
'Graham Mayor
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strBMName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub
Checkout FillBM and other useful VBA functions on my web site.

Graham, Thank you! this code worked exactly as I expected. Although I had to add a space between the quotes of this line. FillBM strName, "" Without the extra space it reduced the Bookmark from a double bracket to a single bracket which made the code not work properly.

gmaxey
02-01-2016, 04:38 PM
If you are trying to write the same userform data to two different bookmarks then:


Private Sub title_Change()
wb "title", title
wb "title2", title
End Sub


That is a crude way repeat data though.