PDA

View Full Version : WORD VBA Clear bookbarks within Document with "Clear" button



Niclasfa
08-10-2015, 09:09 PM
I have this userform that adds data within the document.
The problem I am having is deleting the data using the 'Clear' button and then reading new data.

the reason this is needed is basically for mail purposes. So once a quote is filled out with the personal information it would be good to clear that data and re-add new instead of opening up a new document every time.

This is also a template in case anyone needed to know.

~
~

gmayor
08-10-2015, 09:43 PM
You cannot clear the bookmarks with your process as they will no longer exist. Change the Clear Button title to Cancel and use the following code in the form



Option Explicit

Private Sub CommandButton1_Click()
Me.Tag = 1
Me.Hide
End Sub

Private Sub CommandButton2_Click()
Me.Tag = 0
Me.Hide
End Sub


In a separate new ordinary module enter the following


Option Explicit
Sub RunProcess()
Dim oFrm As New UserForm1
With oFrm
.Show
If .Tag = 0 Then GoTo lbl_Exit
FillBM "FullName", .TextBox1.Text
FillBM "MovingFrom", .TextBox2.Text
FillBM "MovingTo", .TextBox3.Text
FillBM "CostIncludingGST", .TextBox4.Text
FillBM "ReferenceNo", .TextBox5.Text
End With
lbl_Exit:
Unload oFrm
Set oFrm = Nothing
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


You can run the RunProcess macro as many times as you wish, though if you are re-writing data in the same document there is scope for overwriting wanted data.

Niclasfa
08-11-2015, 04:09 PM
Ok so I tried to make the changes you suggested but got an error.

I changed the Title of the button from 'Clear' to 'Cancel'
The error reads as follows.
~
I think I have done everything you suggested but just to make sure I have attached some screen shots.
~
~

Niclasfa
08-11-2015, 04:12 PM
14135

gmayor
08-11-2015, 09:11 PM
Paste the code and use the # command to insert code as the screen shots are difficult to read. However the error means that you have two macros with the same name in the userform.
Remove all the code from the userform except the code I posted:


Option Explicit

Private Sub CommandButton1_Click()
Me.Tag = 1
Me.Hide
End Sub

Private Sub CommandButton2_Click()
Me.Tag = 0
Me.Hide
End Sub

Niclasfa
08-12-2015, 06:54 PM
Ok so I have removed the code I had before and entered the data you advised.

I now have in the UserForm Code

Option Explicit

Private Sub CommandButton1_Click()
Me.Tag = 1
Me.Hide
End Sub

Private Sub CommandButton2_Click()
Me.Tag = 0
Me.Hide
End Sub

And in the module you mentioned:

Option Explicit
Sub RunProcess()
Dim oFrm As New UserForm1
With oFrm
.Show
If .Tag = 0 Then GoTo lbl_Exit
FillBM "FullName", .TextBox1.Text
FillBM "MovingFrom", .TextBox2.Text
FillBM "MovingTo", .TextBox3.Text
FillBM "CostIncludingGST", .TextBox4.Text
FillBM "ReferenceNo", .TextBox5.Text
End With
lbl_Exit:
Unload oFrm
Set oFrm = Nothing
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



But now the bookmarks won't populate at all with the data I put into it.

gmayor
08-12-2015, 11:16 PM
Do the named bookmarks exist in the document?
If so attach or link the document so we can see what you are doing.

Niclasfa
08-17-2015, 04:02 AM
All I need is some VBA code to delete the data entered into the bookmarks so that they may be populated again with new data.

the purpose of this is so that once data is entered and send via mail instead of opening up a new document. We would like to be able to clear the data entered and re enter new data using the user form.

I have attached the test document I am currently working on in the hopes to get this to work.

Feel free to delete and add the proper coding.

gmayor
08-17-2015, 05:00 AM
See attached template. Create a new document from the template. The rest is self evident.

Niclasfa
08-17-2015, 06:07 PM
Word cannot express what a great job you did on that. If I could hug you I would....

Thanks again.