Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

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.




Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.