ActiveX Document



What is ActiveX Document?
ActiveX Document is similar to a form but it runs in Internet Explorer. An ActiveX document can contain almost everything that a form can contain - standard controls, and ActiveX controls. It can also be used to navigate to another document, to show a form and can also contain a menu.

An ActiveX document is a single .VBD (Visual Basic Document) file.  ActiveX document also contains either .EXE or .DLL file (depending upon how you designed the application) containing the code.  If it is .DLL it becomes an in-process application otherwise (if it is .EXE) it becomes an out-of-process server.

When you compile an ActiveX Document application, it creates one .DLL or  .EXE  (as the case may be) for the entire application and one .VBD file for each ActiveX Document.

Each ActiveX document is a UserDocument in the application. Just like a Form a UserDocument also has a designer that allows you to place controls on the UserDocument.

An ActiveX Document can contain properties, read and write properties from and to PropertyBag using WriteProperties and ReadProperties events.  In this respect it is similar to an ActiveX Control.

Sample Application
Let us develop a sample application to understand the following feature of ActiveX Document.

¨       Navigating from one document to another
¨       Displaying a form from ActiveX Document
¨       Placing an ActiveX Control on ActiveX Document

To create sample project:

1.      Create a new project and select ActiveX Document .DLL as the type of the project.
2.      Visual Basic creates an ActiveX Document project with one UserDocument object.
3.      Invoke Project Explorer and double click on UserDocument1 to invoke UserDocument Designer.
4.      Place a label and two commands buttons as shown in figure 30.3.
5.      And change the following properties of the controls.

Control
Property

Value

Label1
Caption
This is first page in ActiveX Document Application.
Command1
Name
CmdNavigate

Caption
&Navigate To Second Page
Command2
Name
CmdAbout

Caption
&Show About Project

6.      Change the Name of the ActiveX document to “FirstDoc”
7.      Write the following code for command buttons.

Private Sub cmdnavigate_Click()
   UserDocument.Hyperlink.NavigateTo App.Path & "\seconddoc.vbd"
End Sub

Private Sub cmdshow_Click()
   frmaboutaxdoc.Show vbModal
End Sub
Listing  30.1: Code for First ActiveX Document - FirstDoc.

Note: UserDocument SecondDoc and form frmAboutAxdoc will be created later.

Adding a new user document
We need to have one more userdocument in the project. The following procedure is used to create the second document.

1.      Select Project ->Add User Document and select User Document in Add User Document dialog.
A new user document is added to project.
2.      Change the Name of the user document to SecondDoc.
Placing an ActiveX control on a user document is in no way different from placing an ActiveX control on a form.
3.      To add MonthView ActiveX Control to the project, use Project -> Component and select Microsoft Windows Common Controls-2 6.0.
4.      Place MonthView on ActiveX document.
5.      Place a command button below monthview control.
6.      Change the following properties of command button
 Name               cmdGoBack
Caption                        &Go Back To First Document
 7.      Write the following code.
 Private Sub cmdback_Click()
    UserDocument.Hyperlink.GoBack
End Sub
Listing 30.2: Code for Back command button on second document.
Adding a form to the ActiveX Document project
Adding a form is also same as adding a form to Standard Exe project.  Our form, in this case, is an About dialog box. Here is the step-by-step procedure.

1.      Select Project -> Add Form.
2.      Change the Name of the form to frmAboutAxdoc
3.      Place four labels and a command button and arrange them as shown in figure 30.2 .
4.      Change the following properties

Object
Property

Meaning

Label1
Caption
This is a sample ActiveX Document project to demonstrate  the following features of ActiveX Document Project:
Label2
Name
Lblfeatures

Caption
“”

Borderstyle
1-fixed single
Label3
Caption
Developed by Author
Label4
Caption
P.Srikanth

Fontbold
True
Command1
Name
Cmdclose

Caption
Close
Form
Caption
About Sample ActiveX Document Application

BorderStyle
1-fixed single

Controlbox
False

5.      Write the following code.

Private Sub cmdclose_Click()
  Unload Me
End Sub

Private Sub Form_Load()
Dim s As String
Dim nl As String
  nl = vbCr & vbLf
  s = "Navigating from one document to another"
  s = s & nl & nl
  s = s & "Invoking a form from a document"
  s = s & nl & nl
  s = s & "Placing an ActiveX Control on a doucument"
  lblfeatures.Caption = s
End Sub
Listing 30.3: Code for events in frmAboutForm.

That is all that you have to do to create an ActiveX Document application with two user documents and a form.

Save all components of the project under the following names.

¨         Firstdoc.dob
¨         Seconddoc.dob
¨         Frmaboutaxdoc.frm
¨         Axdoc.vbp

Creating .VBD and .DLL
Whenever you run ActiveX Document application from VBIDE, Visual Basic creates .VBD files for userdocuments in the directory where Visual Basic is installed. But if you create .DLL file then .VBD files are created in the directory in which .DLL file is placed.

Select File -> Make AxDoc .DLL to create .DLL file for the entire project and .VBD file for each userdocument object. The primary filename will be the name of userdocument. So the complete names are – firstdoc.vbd and second.vbd.

Test Run
Run the ActiveX Document application after creating .DLL and .VBD files and test the features of the application.

1.      Invoke Internet Explorer
2.      Select File-> Open and select firstdoc.vbd from the directory in which you created it.
3.      You should see the first page displayed in Internet Explorer.
4.      Click on Navigate To Second Page to move to second page. You must see MonthView ActiveX Control.
5.      Click on Go Back To First Document command button. You should be in first document.
6.      In first document, click on Show About Project. You should see About Dialog
7.      Click on Close button to dismiss About form.
8.      Terminate Internet Explorer to terminate the application.
 
Post a Comment

ActiveX Document



What is ActiveX Document?
ActiveX Document is similar to a form but it runs in Internet Explorer. An ActiveX document can contain almost everything that a form can contain - standard controls, and ActiveX controls. It can also be used to navigate to another document, to show a form and can also contain a menu.

An ActiveX document is a single .VBD (Visual Basic Document) file.  ActiveX document also contains either .EXE or .DLL file (depending upon how you designed the application) containing the code.  If it is .DLL it becomes an in-process application otherwise (if it is .EXE) it becomes an out-of-process server.

When you compile an ActiveX Document application, it creates one .DLL or  .EXE  (as the case may be) for the entire application and one .VBD file for each ActiveX Document.

Each ActiveX document is a UserDocument in the application. Just like a Form a UserDocument also has a designer that allows you to place controls on the UserDocument.

An ActiveX Document can contain properties, read and write properties from and to PropertyBag using WriteProperties and ReadProperties events.  In this respect it is similar to an ActiveX Control.

Sample Application
Let us develop a sample application to understand the following feature of ActiveX Document.

¨       Navigating from one document to another
¨       Displaying a form from ActiveX Document
¨       Placing an ActiveX Control on ActiveX Document

To create sample project:

1.      Create a new project and select ActiveX Document .DLL as the type of the project.
2.      Visual Basic creates an ActiveX Document project with one UserDocument object.
3.      Invoke Project Explorer and double click on UserDocument1 to invoke UserDocument Designer.
4.      Place a label and two commands buttons as shown in figure 30.3.
5.      And change the following properties of the controls.

Control
Property

Value

Label1
Caption
This is first page in ActiveX Document Application.
Command1
Name
CmdNavigate

Caption
&Navigate To Second Page
Command2
Name
CmdAbout

Caption
&Show About Project

6.      Change the Name of the ActiveX document to “FirstDoc”
7.      Write the following code for command buttons.

Private Sub cmdnavigate_Click()
   UserDocument.Hyperlink.NavigateTo App.Path & "\seconddoc.vbd"
End Sub

Private Sub cmdshow_Click()
   frmaboutaxdoc.Show vbModal
End Sub
Listing  30.1: Code for First ActiveX Document - FirstDoc.

Note: UserDocument SecondDoc and form frmAboutAxdoc will be created later.

Adding a new user document
We need to have one more userdocument in the project. The following procedure is used to create the second document.

1.      Select Project ->Add User Document and select User Document in Add User Document dialog.
A new user document is added to project.
2.      Change the Name of the user document to SecondDoc.
Placing an ActiveX control on a user document is in no way different from placing an ActiveX control on a form.
3.      To add MonthView ActiveX Control to the project, use Project -> Component and select Microsoft Windows Common Controls-2 6.0.
4.      Place MonthView on ActiveX document.
5.      Place a command button below monthview control.
6.      Change the following properties of command button
 Name               cmdGoBack
Caption                        &Go Back To First Document
 7.      Write the following code.
 Private Sub cmdback_Click()
    UserDocument.Hyperlink.GoBack
End Sub
Listing 30.2: Code for Back command button on second document.
Adding a form to the ActiveX Document project
Adding a form is also same as adding a form to Standard Exe project.  Our form, in this case, is an About dialog box. Here is the step-by-step procedure.

1.      Select Project -> Add Form.
2.      Change the Name of the form to frmAboutAxdoc
3.      Place four labels and a command button and arrange them as shown in figure 30.2 .
4.      Change the following properties

Object
Property

Meaning

Label1
Caption
This is a sample ActiveX Document project to demonstrate  the following features of ActiveX Document Project:
Label2
Name
Lblfeatures

Caption
“”

Borderstyle
1-fixed single
Label3
Caption
Developed by Author
Label4
Caption
P.Srikanth

Fontbold
True
Command1
Name
Cmdclose

Caption
Close
Form
Caption
About Sample ActiveX Document Application

BorderStyle
1-fixed single

Controlbox
False

5.      Write the following code.

Private Sub cmdclose_Click()
  Unload Me
End Sub

Private Sub Form_Load()
Dim s As String
Dim nl As String
  nl = vbCr & vbLf
  s = "Navigating from one document to another"
  s = s & nl & nl
  s = s & "Invoking a form from a document"
  s = s & nl & nl
  s = s & "Placing an ActiveX Control on a doucument"
  lblfeatures.Caption = s
End Sub
Listing 30.3: Code for events in frmAboutForm.

That is all that you have to do to create an ActiveX Document application with two user documents and a form.

Save all components of the project under the following names.

¨         Firstdoc.dob
¨         Seconddoc.dob
¨         Frmaboutaxdoc.frm
¨         Axdoc.vbp

Creating .VBD and .DLL
Whenever you run ActiveX Document application from VBIDE, Visual Basic creates .VBD files for userdocuments in the directory where Visual Basic is installed. But if you create .DLL file then .VBD files are created in the directory in which .DLL file is placed.

Select File -> Make AxDoc .DLL to create .DLL file for the entire project and .VBD file for each userdocument object. The primary filename will be the name of userdocument. So the complete names are – firstdoc.vbd and second.vbd.

Test Run
Run the ActiveX Document application after creating .DLL and .VBD files and test the features of the application.

1.      Invoke Internet Explorer
2.      Select File-> Open and select firstdoc.vbd from the directory in which you created it.
3.      You should see the first page displayed in Internet Explorer.
4.      Click on Navigate To Second Page to move to second page. You must see MonthView ActiveX Control.
5.      Click on Go Back To First Document command button. You should be in first document.
6.      In first document, click on Show About Project. You should see About Dialog
7.      Click on Close button to dismiss About form.
8.      Terminate Internet Explorer to terminate the application.
 
Post a Comment