http://www.chrispearson.org/pages/programming/VB/ActiveX/activex04.asp
09h18
Wednesday, 8. October 2008

A PROJECT

This project shows how an ActiveX Control can be created in VB to meet a requirement that can't easily be replicated using server-side scripting or client-side script.

The scenario involves a requirement to print a certifiable document in a pre-determined layout from a web site: An achievement certificate or a licence certificate are typical of documents of this type. (See the project's flowchart)

VB is used to create a control which prints the certficate to the local, default printer. The VB Internet control is used in the ActiveX to download a certificate number from a web site which is then included on the page of the printed document.

To keep the coding simple I have minimised the error checking and handling in this ActiveX so it doesn't always fail elegantly . . .

 

Start VB and choose a new ActiveX Control project.

Change the control's name from UserControl1 to PrintControl and save the project as CertificatePrint.vbp

Add a command button to the control, naming it cmdPrint and set its caption to Print

We'll place the button and size the control later, so just leave the control where it is for now.

cmdPrint's properties

Add an internet transfer control
Add the signature graphic

Next, add a Microsoft Internet Transfer control to the UserControl workspace. You may need to right-click the toolbox and use the Components option to add this control to the toolbox. (See VB's help if you have any trouble doing this)

This control will be used to download the unique certificate number. This control is invisible at runtime and can't be sized at design time, so just leave it as it is.

Since no certificate is complete without a signature, we'll add a picture box, call it picSignature and set its Picture property to a suitable graphic (Included in the downloadable support files, signature.jpg)

The picture box needs to be sized to match the signature graphic and its border and background set to match the white document background.

We now need to write code to print the certificate, so double-click the command button, cmdPrint to open the code window and add the code to it's onClick event, as shown below: Picture box properties

Private Sub cmdPrint_Click()
'
' Print a formatted, authenticated certificate to the default printer
'
Const twipFactor = 1440

Printer.ScaleMode = vbTwips
Printer.Orientation = 1
Printer.FontTransparent = True
Printer.FillStyle = vbFSTransparent
Printer.ForeColor = &O0&

'
sWidth = 8.3
sHeight = 12
'
Printer.Width = twipFactor * sWidth
Printer.Height = twipFactor * sHeight
'
Printer.Font = "Arial"
Printer.FontSize = 14
Printer.FontBold = True
Printer.CurrentY = twipFactor * 0.5
Printer.CurrentX = twipFactor * 0.5
Printer.Print Format(Now(), "hh:mm:ss")
'
' Page Header
'
Printer.Font = "Arial"
Printer.FontSize = 36
Printer.FontBold = True
Printer.CurrentY = twipFactor * 1
hWidth = Printer.TextWidth("THIS IS TO CERTIFY") / 2
Printer.CurrentX = Printer.ScaleWidth / 2 - hWidth
Printer.Print "THIS IS TO CERTIFY"

Printer.Font = "Arial"
Printer.FontSize = 18
Printer.FontBold = True
Printer.CurrentY = twipFactor * 1.7
hWidth = Printer.TextWidth("THE AUTHENTICITY OF") / 2
Printer.CurrentX = Printer.ScaleWidth / 2 - hWidth
Printer.Print "THE AUTHENTICITY OF"

Printer.Font = "Arial"
Printer.FontSize = 36
Printer.FontBold = True
Printer.CurrentY = twipFactor * 2.2
hWidth = Printer.TextWidth("THIS CERTICATE") / 2
Printer.CurrentX = Printer.ScaleWidth / 2 - hWidth
Printer.Print "THIS CERTICATE"
'
' Signature
'
Printer.CurrentX = twipFactor * 3.5
Printer.CurrentY = twipFactor * 7
Printer.Font = "Arial"
Printer.FontSize = 14
Printer.FontBold = True
Printer.ForeColor = &O0&
Printer.Print "Signed this " & Day(Now()) & " day of " & _
MonthName(Month(Now())) & _
" " & Year(Now())

sX1 = twipFactor * 4.5
sY1 = twipFactor * 8
Printer.PaintPicture picSignature, sX1, sY1

Printer.CurrentX = twipFactor * 4.5
Printer.CurrentY = twipFactor * 9
Printer.Font = "Arial"
Printer.FontSize = 14
Printer.FontBold = True
Printer.Print "Mortimer Mouse"

Printer.EndDoc
'
Exit Sub

End Sub

At this stage you can test the control by clicking on the Start icon (the little blue arrow) and, when the test page has loaded, clicking on the print button (or whatever you can see of the print button that isn't obscured by the picture box!) So long as the print seems to be working (looking something like this) we can move on to getting the certificate number using the Internet Transfer Control. Note that the current time is included on the output, top left. This is just to ensure that, during any debugging, you can tell which copy is which.
Getting the certificate number    

Because we have the entire contents of the VB toolbox at our disposal, getting a file from a remote server is as easy as it was placing the Microsoft Internet Transfer Control on our ActiveX Control. What we will do with this control is first ask the user for a security code which is also the name of a file on the web server: We will use an Input Box to get the filename.

Next, we'll use the Internet Transfer Control to get the file containing a certificate number from a URL hardcoded into the ActiveX, so the user need never know what the URL is or how the update works. Checking validity consists two steps:

The secure code entered by the user must match the name of a file on the server (If it's a Unix server, that will be case-sensitive, too)

The returned certificate number is validated by the ActiveX control (In this case we simply check if it's six characters long)

This is the code:

Dim v as Variant
'
Inet1.AccessType = icUseDefault
v = Inet1.OpenURL _
   ("www.chrispearson.org/pages/programming/vb/activex/certprint/" & _
   InputBox("Please enter your security code", , "hoot") & _
   ".dat")
'
If Len(v) <> 6 Then
   v = "INVALID!"
End If
'
MsgBox ">>>" & v & "<<<"

 

Notice that we have included the InputBox in the parameters of OpenURL. We replace the contents of variable v with the text INVALID! if the returned certificate number fails our validity check.

At this stage we also use a MessageBox to stop the program and show us the result - Use this a couple of times to ensure you are making a connection and returning an expected result. Once the transfer is working properly don't forget to remove the MsgBox ">>>" & v & "<<<" code.

We need to ensure that the certificate is only signed if it is valid, so change the original code, adding a condition around the line that prints the signature:

sX1 = twipFactor * 4.5
sY1 = twipFactor * 8

If v <> "INVALID!!" Then
   Printer.PaintPicture picSignature, sX1, sY1
End If
 
And a certificate number needs to be added just before the existing Printer.EndDoc:  
Printer.CurrentX = twipFactor * 4.5
Printer.CurrentY = twipFactor * 10
Printer.Font = "Arial"
Printer.FontSize = 14
Printer.FontBold = True
Printer.Print "Certificate number " & v

Printer.EndDoc

 
Also, remove the time stamp from the top left corner of the form and tiday up the control, shrinking it around the command button.
Move the print command button to the top left Move the command button to the top left . . .
Then shrink the control around the button - The other controls will now not be visible but they're still there! Resize the control to match the size of the print command button
 

 

The project files, along with three certificate numbers in .dat files, are available to download in certprint_files.zip.

This archive contains all the VB project files and a compiled .ocx.

And, finally, it might not be the most sparkling bit of software in the universe but it does show how you can use ActiveX to do something you can't do in JavaScript and which does (at a push) have a real-world application.

Bear in mind that this project contains nothing in the way of error handling: If anything untoward happens the ActiveX dies without grace! The next steps would be a few judicious OnError GoTos and maybe a Select Case to handle INet1's return codes.

Why not give it a go? Use the secure code: hippo or the default value, hoot

Happy coding!

 
Previous page

Previous page:
Page 3 - Controls on a page and security issues

   

xxx,xxx

copyright ©2000 - 2008 Chris Pearson