Now: Tutorial for Web and Software Design > Programming > VB > Programming Content
> Visual Basic Explorer - OLE - Object Linking and Embedding Tutorial [Bookmark it]
Visual Basic Explorer - OLE - Object Linking and Embedding Tutorial

OLE Automation 1.0


Name: Using OLE in Visual Basic applications.
Author: Richard Quinn
E-Mail: Richard.Quinn@IBM.NET
Date: July 22, 1997

Description: So, you've heard of OLE, you know what it is, but how do you use it? The simplest and quickest way to use OLE is by making use of the OLE container control, provided as standard by Visual Basic. With no programming, you can draw one of these controls onto your visual basic form, set the SourceDoc property to point to the file of your choice and there you are! You have created an OLE client application. Your program can play host to OLE server applications (such as Microsoft Excel) allowing the user access to professional level computing power. But, seeing as this definitely hasn't quenched your thirsty lust for knowledge, the following guide will explain how to programatically control what appears in your OLE container. You will also see how the Common Dialog is integrated into the rest of your project.
Controls needed: Common Dialog, Command Button and OLE Container.
Note: You will need Excel to complete this tutorial.
Level: Beginner


You will need to start a new project containing a single form, a Common Dialog control, a command button (named cbOpenFile) control and an OLE Container control (named objOLEContainer) Paste the following code into the declarations section of your form.




Private Sub Form_Load()

'disable error messages for this procedure

On Error Resume Next

	

	'centre the form

	'Me refers to this form

	'Move is more efficient than setting left and top properties

	'we use the \ operator to make integer divisions,

	'accuracy is not important but speed is.

	

	Me.Move (Screen.Width - Me.ScaleWidth) \ 2, _

(Screen.Height - Me.ScaleHeight) \ 2



        'move the command button to the middle bottom of the form

	cbOpenFile.Move (Me.ScaleWidth - cbOpenFile.Width) \ 2, _

					(Me.ScaleHeight - cbOpenFile.Height)

				

	'move the OLE container to cover the rest of the form

	objOLEContainer.Move Me.ScaleLeft, _

				Me.ScaleTop, _

				Me.ScaleWidth, _

				Me.ScaleHeight - cbOpenFile.Height

	

	'we want the contents of the container to be automatically

	'and proportionally resized

	objOLEContainer.SizeMode = vbOLESizeZoom

	





End Sub



Private Sub cbOpenFile_Click()



On Error Resume Next

		

		'tell the common dialog that we want to know if the user presses

		'cancel

		cdgDialog.CancelError = True

		

		'we want the common dialog to filter in only Excel files

		'filters are very useful when using the common dialog control

		cdgDialog.Filter = "Excel WorkBooks (*.xls)|*.xls|Comma Separated Value Files (*.csv)|*.csv"

		

		'tell the common dialog to show itself in the FileOpen mode

		cdgDialog.ShowOpen

		

		'Code stops executing and waits for a result from

		'the common dialog box

		

		'Did you press cancel?

		If Err = cdlCancel Then

			'Inform the user that this was a bad idea

			MsgBox "OOps! You pressed cancel!", vbOKOnly, "Error"

			'and leave the sub

			Exit Sub

		End If

		

		'Did the user choose a respectable file name?

		If cdgDialog.FileName = vbNullString Then

			'Inform the user that this was a bad idea

			MsgBox "OOps! You didn't choose a file!", vbOKOnly, "Error"

			'and leave the sub

			Exit Sub

		End If

		

		'everything appears OK, so we can tell the OLE container

		'to create an *embedded* link using the file chosen by the user

		

		objOLEContainer.CreateLink cdgDialog.FileName

	

End Sub

Private Sub objOLEContainer_Click()

On Error Resume Next

	

	'Tell the OLE container object to empty *linked* files

	objOLEContainer.Delete

	

	'see what errors happened during this process

	Select Case Err



	Case Is = 0 'no errors - Yippee!



	Case Else 'other errors

		'tell the user what error happened

		'Error(Err) returns the error message for the error number held by "Err"

		MsgBox "Error number " & Err & " occurred. This means" & vbCrLf & _

			Error(Err)



	End Select

	

	

End Sub



Run the project and see how, very simply, you have given the user access to show whichever Excel file he (she) chooses and edit it directly from within your program. Clicking on the OLE control will remove its contents.


[Bookmark][Print] [Close][To Top]
  • Prev Article-Programming:

  • Next Article-Programming:
  • Related Materias
    Passing an Array to and fr
    Using the For匛ach Stateme
    Executing a VB Program wit
    Sorting Viewer Tutorial - 
    Crystal Reports Tutorial -
    Creating a Toolbar - Visua
    Random Numbers - Randomize
    File Dialog Box - Tutorial
    Using Office applications 
    VB Tutorials - MultiSelect
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial