![]() |
|
||||||||||||||||||||||||
|
|
Create neat excel spreadsheets and charts from your Fortran programs (Example 6.3)
This is an example in which we create a Fortran Automation controller for Excel. The main Fortran application opens a new Excel spreadsheet and set the values of a few cells to random values. Then the Fortran applications creates a new chart that plots the random values as XY coordinates. After this is done, the spreadsheet is made visible, so the user can see the results. Important points that you should notice in this example include:
Here is a screen shot of the output of the program:
And here is the complete example:
program Example62
!Example shows how to use f90VBA to automate Excel
!Copyright (C) 1999-2000, Canaima Software, Inc.
!Illustrates:
! - Creating an new object
! - Getting an interface to an active object
! - Setting/Reviewing property values
! - Executing object methods
! - Requesting properties/Executing methods in
! an object hierarchy
! - Handling collections
use f90VBDefs
use f90VBVariants
use f90VBAutomation
implicit none
!Variants containing main objects
type(VARIANT)::Excel, WorkBook, Chart, Range
!Variants used to store temporal objects and collections
type(VARIANT)::VarTmp
integer(HRESULT_KIND)::iRet
integer::i
real::k
type(VARIANT)::IsVisible
!Initialize Ole
iRet = OleInitialize()
!If excel is already running, then get an interface
!to the running instance
Excel = GetActiveOleObject('Excel.Application',iRet)
if (iRet.ne.S_OK) then
!no instances of excel are running, create one
Excel = CreateOleObject('Excel.Application', iRet)
if (iRet.ne.S_OK) then
print *,'Cannot instantiate the Excel object. Excel might not be'
print *,'installed or properly registered in your system'
goto 1000
endif
endif
!Add a new workbook by calling Workbooks.Add method
WorkBook=ExecMethod(Excel,'Workbooks.Add')
!Get a range object from the current workbook
VarTmp=VariantCreate(VT_BSTR,'A1:B20')
Range=PropertyGet(Excel,'Range',VarTmp)
!Clear BString stored in VarTmp
call VariantClear(VarTmp)
!Set the range formulas to some random values
VarTmp=VariantCreate(VT_BSTR,'=10*RAND()')
call PropertyPut(Range,'Formula',VarTmp)
!Clear BString stored in VarTmp
call VariantClear(VarTmp)
!Add a new Chart by calling Charts.Add method
Chart=ExecMethod(WorkBook,'Charts.Add')
!Set the type of the chart to scatterplot (-4169)
call PropertyPut(Chart,'ChartType', VariantCreate(VT_I4,-4169))
!Set the range object to be the data for the chart
VarTmp=ExecMethod(Chart,'SetSourceData',Range, VariantCreate(VT_I4,2))
!Set the title for the chart
call PropertyPut(Chart,'HasTitle',VariantCreate(VT_BOOL,.true.))
VarTmp = VariantCreate(VT_BSTR,'f90VB is Easy!')
call PropertyPut(Chart,'ChartTitle.Text',VarTmp,iRet=iRet)
!We don't want to see a legend in this case, so remove it
call PropertyPut(Chart,'HasLegend',VariantCreate(VT_BOOL,.false.))
!Make the excel object visible
call PropertyPut(Excel,'Visible',VariantCreate(VT_BOOL,.true.))
!release all the currently held interfaces
call Release(Chart)
call Release(Workbook)
!close excel (uncomment next line for closing)
!VarTmp= ExecMethod(Excel,'Quit',iRet=iRet)
call Release(Excel)
1000 continue
call OLEUninitialize()
stop
end
|
||||||||||||||||||||||||
|
Copyright
© 1998-2000 Canaima Software
For questions regarding this site, send an e-mail to webmaster@canaimasoft.com |
|||||||||||||||||||||||||