Listing 7-2
Using the Clipboard to Copy Data
Private Sub btnCopy_Click()
‘ Create a Clipboard storage object.
Dim ClipData As DataObject
Set ClipData = New DataObject
‘ Place the text in lblOutput in the Clipboard.
ClipData.SetText lblOutput.Caption
‘ Place the object on the Clipboard.
ClipData.PutInClipboard
End Sub
Private Sub btnQuit_Click()
‘End the program.
End
End Sub
Private Sub btnTest_Click()
‘ Create a string to hold the data.
Dim Output As String
‘ Create a document property holder.
Dim DocProp As DocumentProperty
‘ Handle properties that the application doesn’t
support.
On
Error Resume Next
‘ Start creating the output string.
For Each DocProp In _
ActiveWorkbook.BuiltinDocumentProperties
Output = Output + DocProp.Name + “ = “ + _
CStr(DocProp.Value) + vbCrLf
Next
‘ Display the output onscreen.
lblOutput.Caption = Output
‘ Enable the Copy Data button.
btnCopy.Enabled = TrueEnd
Sub
VBA puts your code in alphabetical order when you work with forms, which
changes the appearance of the code, but not the actual flow.
This technique
makes it easier to find a particular
Sub
. However, it can also make it more
difficult to follow the expected flow of the program. The code begins with
btnTest_Click
because
btnCopy
is disabled when you start the program.
The “Writing Your First Sub” section of Chapter 3 shows how to retrieve just
one value from the
BuiltinDocumentProperties
collection. This example
164
Part II: Learning the Ropes
12_046500 ch07.qxp 12/5/06 5:35 PM Page 164
shows how to use the
For Each...Next
statement to retrieve all the values
available for an application.
Notice that
btnTest_Click
uses the
On Error Resume Next
statement.
This program shows one of the few times where that statement is the correct
one to use. Not every application supports every built-in property. If you try
to retrieve the value of a built-in property that the application doesn’t sup-
port, VBA generates an error. Because there’s nothing that you should do
to
correct the error, resuming with the next statement is the correct error-
handling strategy.
After the code retrieves all the built-in document properties, it places the
information in the
lblOutput.Caption
property, which displays the infor-
mation onscreen.
The code also enables
btnCopy
by setting its
Enabled
property to true.
The
btnCopy_Click
sub-procedure introduces the
DataObject
type. You
might need to interact with the Clipboard when writing an application. This
object is what you use to perform the interaction. The
DataObject
methods
help you retrieve information from the
Clipboard, place information on the
Clipboard, and format Clipboard data.
Working with the Clipboard is normally a two-step process. To place data on
the Clipboard, the code must place it in the
DataObject
first by using the
SetText
method. The next step uses the
PutInClipboard
method to
move the data from the
DataObject
to the Clipboard. To get data from
the Clipboard, follow the opposite procedure. Use the
GetFromClipboard
method to place the text
on the Clipboard into the
DataObject
and then use
the
GetText
method to move the data from the
DataObject
to a variable.
Every program that you create should include some means of ending. A
dialog-box–based program, like the one in this section, normally includes a
Quit or an Exit button. A configuration dialog box normally relies on the OK
or Cancel button to perform cleanup and then exit. The
btnQuit_Click
sub-procedure for this example performs a simple task — it uses the
End
statement to end the program.
Saying
yes or no with check
boxes and toggle buttons
The
CheckBox
and
ToggleButton
controls are Boolean — controls with a
true or false value. The implication of each control is different. A
CheckBox
control normally indicates yes or no. On the other hand, a
ToggleButton
control indicates an on or off condition. You can use the controls as you see
fit, but this is the normal way to work with them.
165
Do'stlaringiz bilan baham: