History tracking

L

LEU

I have a document that when a user opens the form to update it puts his/her
name in the name textbox automatically. This works great for a new document.
If it already exists and the user opens it up it will remove the existing
name in the document and put their name in when the form closes. Is there
someway to keep a running record of the old names in a hidden field for
history?
 
J

Jezebel

Write an AutoOpen macro that takes the value in the TextBox and appends it
to a list that you store in a document variable.
 
L

LEU

Hi Jezebel,

I have no idea on how to do that. I know how to creat a variable like below,
but not how to append it to a list that I store in a document.

Dim VarA As Word.Range
With ActiveDocument
..Variables("varA").Value = Text1.Text
End With
 
J

Jezebel

An easy method is to use the Join() and Split() functions --

Dim pValue as string
Dim pHistory() as string

'Retrieve the existing list, if any
on error resume next
pValue = ActiveDocument.Variables("varA")
on error goto 0

'If existing list, extend it
if len(pValue) then
pHistory = split(pValue, "|")
redim preserve pHistory(0, uBound(pHistory)+1)

'No existing list, so create a new one
Else
Redim pHistory(0)
end if

'Add the new item
pHistory(uBound(pHistory)) = Text1.Text

'Save it
ActiveDocument.Variables("varA") = Join(pHistory, "|")
 
L

LEU

Thank you so much for all your help.....

Then to display the list of names I would need to create a new form with a
listbox that I would open from a command button. How would I make the listbox
= varA to show all the names?
 
L

LEU

Hi Jesebel,

I have it all set up and when I run it through the first time it works. But
when I run it a second time I get the following error “Subscript out of
range†at the following spot in the macro:

ReDim Preserve pHistory(0, UBound(pHistory) + 1)


Here is the hole macro:

Private Sub CmdOK_Click()
Dim MyDate
Set oFF = ActiveDocument.FormFields
Set oRng = oBMs("RName").Range
oRng.Text = Text1.Text
oBMs.Add "RText1", oRng
MyDate = Date
Set oRng = oBMs("RDate").Range
oRng.Text = MyDate
oBMs.Add "RDate", oRng
Set oRng = oBMs("RRisk").Range
oRng.Text = Cmb1.Value
oBMs.Add "RRisk", oRng
Set oRng = oBMs("RBases").Range
oRng.Text = Text3.Text
oBMs.Add "RRisk", oRng
ActiveDocument.Saved = True

Dim pValue As String
Dim pHistory() As String

'Retrieve the existing list, if any
On Error Resume Next
pValue = ActiveDocument.Variables("varA")
On Error GoTo 0

'If existing list, extend it
If Len(pValue) Then
pHistory = Split(pValue, "|")
ReDim Preserve pHistory(0, UBound(pHistory) + 1)

'No existing list, so create a new one
Else
ReDim pHistory(0)
End If

'Add the new item
pHistory(UBound(pHistory)) = Text1.Text & " " & Text2.Text

'Save it
ActiveDocument.Variables("varA") = Join(pHistory, "|")

Me.Hide
End Sub
 

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