W
Wicked Wizard
I've got some code attached to a button that takes data from a user form and
puts it in a letter at some bookmarks, very simple, thus:
Private Sub cmdOK_Click()
'utilise the data on the form when the user hits OK
'hide the form, leaving it in memory to access the data
frmGetData.Hide
'take the data from the boxes on the form and place it at the bookmarks in
the document
If ActiveDocument.Bookmarks.Exists("Addressee") Then
ActiveDocument.Bookmarks("Addressee").Range.Text = txtAddressee.Text
End If
If ActiveDocument.Bookmarks.Exists("Company") Then
ActiveDocument.Bookmarks("Company").Range.Text = txtCompany.Text
End If
If ActiveDocument.Bookmarks.Exists("LineOne") Then
ActiveDocument.Bookmarks("LineOne").Range.Text = txtLineOne.Text
End If
If ActiveDocument.Bookmarks.Exists("LineTwo") Then
ActiveDocument.Bookmarks("LineTwo").Range.Text = txtLineTwo.Text
End If
If ActiveDocument.Bookmarks.Exists("LineThree") Then
ActiveDocument.Bookmarks("LineThree").Range.Text = txtLineThree.Text
End If
If ActiveDocument.Bookmarks.Exists("LineFour") Then
ActiveDocument.Bookmarks("LineFour").Range.Text = txtLineFour.Text
End If
If ActiveDocument.Bookmarks.Exists("Salutation") Then
ActiveDocument.Bookmarks("Salutation").Range.Text = txtSalutation.Text
End If
If ActiveDocument.Bookmarks.Exists("Subject") Then
ActiveDocument.Bookmarks("Subject").Range.Text = txtSubject.Text
End If
'move the cursor to the start of the letter to begin typing
Selection.GoTo what:=wdGoToBookmark, Name:="StartText"
'get the form out of memory
Unload frmGetData
End Sub
Works fine, and it's only a small form. But I can see that I might want to
collect lots of data, whereupon this becomes rather tedious. So I thought,
if I were any good with VBA (and I am only starting - an aged canine slowly
learning a new trick) I would loop through the text boxes on the form, get
the data and stick it in each bookmark by looping through them too. It's
getting to the stage where I need to go for my afternoon lie down, so I
thought I'd ask for some help and see if this can be done as I envisage.
Can any of you young things put me right please, while I reach for my zimmer
frame?
puts it in a letter at some bookmarks, very simple, thus:
Private Sub cmdOK_Click()
'utilise the data on the form when the user hits OK
'hide the form, leaving it in memory to access the data
frmGetData.Hide
'take the data from the boxes on the form and place it at the bookmarks in
the document
If ActiveDocument.Bookmarks.Exists("Addressee") Then
ActiveDocument.Bookmarks("Addressee").Range.Text = txtAddressee.Text
End If
If ActiveDocument.Bookmarks.Exists("Company") Then
ActiveDocument.Bookmarks("Company").Range.Text = txtCompany.Text
End If
If ActiveDocument.Bookmarks.Exists("LineOne") Then
ActiveDocument.Bookmarks("LineOne").Range.Text = txtLineOne.Text
End If
If ActiveDocument.Bookmarks.Exists("LineTwo") Then
ActiveDocument.Bookmarks("LineTwo").Range.Text = txtLineTwo.Text
End If
If ActiveDocument.Bookmarks.Exists("LineThree") Then
ActiveDocument.Bookmarks("LineThree").Range.Text = txtLineThree.Text
End If
If ActiveDocument.Bookmarks.Exists("LineFour") Then
ActiveDocument.Bookmarks("LineFour").Range.Text = txtLineFour.Text
End If
If ActiveDocument.Bookmarks.Exists("Salutation") Then
ActiveDocument.Bookmarks("Salutation").Range.Text = txtSalutation.Text
End If
If ActiveDocument.Bookmarks.Exists("Subject") Then
ActiveDocument.Bookmarks("Subject").Range.Text = txtSubject.Text
End If
'move the cursor to the start of the letter to begin typing
Selection.GoTo what:=wdGoToBookmark, Name:="StartText"
'get the form out of memory
Unload frmGetData
End Sub
Works fine, and it's only a small form. But I can see that I might want to
collect lots of data, whereupon this becomes rather tedious. So I thought,
if I were any good with VBA (and I am only starting - an aged canine slowly
learning a new trick) I would loop through the text boxes on the form, get
the data and stick it in each bookmark by looping through them too. It's
getting to the stage where I need to go for my afternoon lie down, so I
thought I'd ask for some help and see if this can be done as I envisage.
Can any of you young things put me right please, while I reach for my zimmer
frame?