Page 1 of 1 1
Topic Options
#151867 - 2005-11-18 10:19 PM RFC: xlLib - Library of MS Excel UDFs
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4400
Loc: New Jersey
Version 1.0 is now posted

Glenn

 Code:
;; xlLib - a library of MS Excel interface functions
;;
;; A collection of UDFs to interface with MS Excel
;; Extensive testing with MS Office XP, limited testing with Office 2K3, 
;; otherwise untested! 
;;
;; This version is released for review, comments, and suggestions.
;;
;; Glenn Barnas
;; Version 1.0	2007/09/26	Updated xlRangeValue, added xlQuit
;; Version 0.8	2005/11/08	First release
;;
;;	xlInstantiateApp()	Create the Excel object reference
;;	xlNewBook()		Create a new workbook in the active object
;;	xlAddSheet()		Create a new worksheet
;;	xlSheetName()		Return the active sheet name, set the specified sheet's name
;;	xlRangeValue()		Read or write a worksheet range
;;	xlRangeClear()		Clear the specified worksheet range
;;	xlRangeFormat()		Apply formatting to the specified worksheet range
;;	xlFile()		Open, Save, or SaveAs file functions
;;	xlQuit()		Destroy the instantiated object

;;
;;======================================================================
;;
;;FUNCTION       xlInstantiateApp()
;;
;;ACTION         Instantiates the MS Excel application
;;
;;AUTHOR         Glenn Barnas
;;
;;SYNTAX         xlInstantiateApp()
;;
;;PARAMETERS     None
;;
;;REMARKS        
;;
;;RETURNS        Pointer to Excel Application Object
;;
;;DEPENDENCIES   Excel
;;
;;TESTED WITH    W2K, WXP, W2K3, Office XP, Office 2K3
;;
;;EXAMPLES       $oXL = xlInstantiateApp()
;
Function xlInstantiateApp()

  $xlInstantiateApp = CreateObject('EXCEL.Application')
  Exit @ERROR

EndFunction



;;
;;======================================================================
;;
;;FUNCTION       xlNewBook()
;;
;;ACTION         Creates a new Excel Workbook
;;
;;AUTHOR         Glenn Barnas
;;
;;SYNTAX         xlNewBook(Object_ID [, Sheets] , [Properties])
;;
;;PARAMETERS     Object_ID  - REQUIRED, Excel Application Object pointer
;;		 Sheets     - OPTIONAL, Number of sheets to create. Default is 1
;;		 Properties - OPTIONAL, Array of workbook document properties
;;
;;REMARKS        Creates a new workbook with the defined number of sheets.
;;		 Each sheet has the default name (Sheet#). The optional array
;;		 has 8 elements, defining the document properties.
;;
;;RETURNS        Workbook Object ID
;;
;;DEPENDENCIES   Excel
;;
;;TESTED WITH    W2K, WXP, W2K3, Office XP, Office 2K3
;;
;;EXAMPLES       $WBo = xlNewBook($oXL, 4, $aryProp)
;
Function xlNewBook($_ID, OPTIONAL $_Sheets, OPTIONAL $_aProp)

  Dim $_, $_Err

  $_Sheets = IIf(Val($_Sheets) = 0, 1, Val($_Sheets))
  $_ID.SheetsInNewWorkbook = $_Sheets

  $xlNewBook = $_ID.Workbooks.Add()		; add a new workbook
  $_Err = @ERROR

  If VarType($_aProp) > 8191
    ; Define optional properties from array
    If $_aProp[0] $xlNewBook.Title	= $_aProp[0] EndIf
    If $_aProp[1] $xlNewBook.Subject	= $_aProp[1] EndIf
    If $_aProp[2] $xlNewBook.Author	= $_aProp[2] EndIf
    If $_aProp[3] $xlNewBook.Manager	= $_aProp[3] EndIf
    If $_aProp[4] $xlNewBook.Company	= $_aProp[4] EndIf
    If $_aProp[5] $xlNewBook.Category	= $_aProp[5] EndIf
    If $_aProp[6] $xlNewBook.Keywords	= $_aProp[6] EndIf
    If $_aProp[7] $xlNewBook.Comments	= $_aProp[7] EndIf
  EndIf

  Exit $_Err

EndFunction



;;
;;======================================================================
;;
;;FUNCTION       xlAddSheet()
;;
;;ACTION         Adds a new worksheet to an Excel spreadsheet
;;
;;AUTHOR         Glenn Barnas
;;
;;SYNTAX         xlAddSheet(Object_ID [, Position] [, Name] [, TabColor])
;;
;;PARAMETERS     Object_ID  - REQUIRED, Excel Application Object pointer
;;		 Position   - OPTIONAL, Position of new sheet
;;				Null     - placed before active sheet
;;				[-]name  - placed before named sheet
;;				+name    - placed after named sheet
;;		 Name       - OPTIONAL, Name of new worksheet
;;		 TabColor   - OPTIONAL, Color of new worksheet tab (Color Index #)
;;
;;REMARKS        
;;
;;RETURNS        Name of the new sheet
;;
;;DEPENDENCIES   Excel
;;
;;TESTED WITH    W2K, WXP, W2K3, Office XP, Office 2K3
;;
;;EXAMPLES       ; Add a new sheet called "may" after the "April" sheet
;;		 xlAddSheet($oXL, '+April', 'May', 33)
;
Function xlAddSheet($_ID, OPTIONAL $_Position, OPTIONAL $_Name, OPTIONAL $_TabColor)

  Dim $_, $_Err

  If Left($_Position, 1) = '+'
    $_Position = SubStr($_Position, 2)		; trim leading '+'
    $_ = $_ID.Worksheets.Add(, $_ID.Worksheets($_Position), 1)
  Else
    If $_Position
      If Left($_Position, 1) = '-'
        $_Position = SubStr($_Position, 2)	; trim leading '-'
      EndIf
      $_ = $_ID.Worksheets.Add($_ID.Worksheets($_Position), , 1)
    Else
      $_ = $_ID.Worksheets.Add
    EndIf
  EndIf

  $_Err = @ERROR				; save the creation status

  If $_Name
    $_ID.ActiveSheet.Name = $_Name		; define the name, if supplied
  EndIf

  If $_TabColor
    $_ID.ActiveSheet.Tab.ColorIndex = $_TabColor
  EndIf

  $xlAddSheet = $_ID.ActiveSheet.Name		; return the name of the new sheet
  Exit $_Err

EndFunction



;;
;;======================================================================
;;
;;FUNCTION       xlSheetName()
;;
;;ACTION         Renames the defined sheet
;;		 Returns the name of the active sheet
;;
;;AUTHOR         Glenn Barnas
;;
;;SYNTAX         xlSheetName(Object_ID [, Old, New])
;;
;;PARAMETERS     Object_ID  - REQUIRED, Excel Application Object pointer
;;		 Old        - OPTIONAL, Old sheet name
;;		 New        - OPTIONAL, New sheet name
;;
;;REMARKS        If old name is not specified, the name of the active sheet is returned.
;;		 If the old name is specified, that sheet is changed to the new name. An
;;		 error is returned if OLD is specified and NEW is not.
;;
;;RETURNS        Namem of active sheet if no name args are present
;;
;;DEPENDENCIES   Excel
;;
;;TESTED WITH    W2K, WXP, W2K3, Office XP, Office 2K3
;;
;;EXAMPLES       $SheetName = xlSheetName($oXL)			; get current sheet name
;;		 xlSheetName($0XL, $SheetName, 'NewSheetName')	; change it!
;
Function xlSheetName($_ID, OPTIONAL $_Old, OPTIONAL $_New)

  ; if Old name not provided, return the active sheet name
  If $_Old = ''
    $xlSheetName = $_ID.ActiveSheet.Name
    Exit @ERROR
  EndIf

  ; complain if New name isn't provided
  If VarType($_New) = 0
    Exit 87
  EndIf

  ; Rename the sheet
  $_ID.Worksheets($_Old).Name = $_New
  Exit @ERROR

EndFunction



;;
;;======================================================================
;;
;;FUNCTION       xlRangeValue()
;;
;;ACTION         Reads / Writes a range of cells
;;
;;AUTHOR         Glenn Barnas
;;
;;SYNTAX         xlRangeValue(Object_ID, Range [, Value] [, Sheet])
;;
;;PARAMETERS     Object_ID  - REQUIRED, Excel Application Object pointer
;;		 Range      - REQUIRED, A standard range specification (A1:C3)
;;		 Value      - OPTIONAL, The value to place into the range
;;		 Sheet      - OPTIONAL, The worksheet name to read/write
;;
;;REMARKS        Defaults to the active sheet if not specified.
;;		 To READ a range value, the Value parameter must not be
;;		 specified! Both values and formulas can be written.
;;
;;RETURNS        Variant containing the data from the defined cell range
;;
;;DEPENDENCIES   Excel
;;
;;TESTED WITH    W2K, WXP, W2K3, Office XP, Office 2K3
;;
;;EXAMPLES       READ:	$Val = xlRangeValue($XL, 'A1', , 'Sheet1')
;;		 WRITE:	xlRangeValue($XL, 'A1', '=Sum(A2:a9)', 'Sheet1')
;
Function xlRangeValue($_ID, $_Range, OPTIONAL $_Value, OPTIONAL $_Sheet)

  Dim $_Rv, $_X, $_Y, $_D

  $_Sheet = IIf($_Sheet = '', $_ID.ActiveSheet.Name, $_Sheet)

  If VarType($_Value) <> 0
    $_ID.WorkSheets($_Sheet).Range($_Range).Value = $_Value
  Else
    $Rv = $_ID.WorkSheets($_Sheet).Range($_Range).Value
    If VarType($Rv) < 8192		; return a straight value
      $xlRangeValue = $Rv
    Else			; return an array
      If UBound($Rv) = 1	; 1 row by x columns
        $xlRangeValue = Split(Join($Rv, Chr(31)), Chr(31))
      Else			; several rows of X columns
        Dim $aTmp[UBound($Rv) - 1]
        $_X = 0 $_Y = UBound($Rv)
        For Each $_ in $Rv
          $aTmp[$_X Mod $_Y] = $aTmp[$_X Mod $_Y] + $_D + $_
          $_X = $_X + 1
          If $_X = $_Y $_D = Chr(31) EndIf
        Next
        For $_ = 0 to UBound($aTmp)
          $aTmp[$_] = Split($aTmp[$_], Chr(31))
        Next
        $xlRangeValue = $aTmp
      EndIf
    EndIf
  EndIf

  Exit @ERROR

EndFunction



;;
;;======================================================================
;;
;;FUNCTION       xlRangeClear()
;;
;;ACTION         Clears a range of cells
;;
;;AUTHOR         Glenn Barnas
;;
;;SYNTAX         xlRangeClear(Object_ID, Range [, Sheet])
;;
;;PARAMETERS     Object_ID  - REQUIRED, Excel Application Object pointer
;;		 Range      - REQUIRED, A standard range specification (A1:C3)
;;		 Sheet      - OPTIONAL, The worksheet name to reference
;;
;;REMARKS        Defaults to the active sheet if not specified.
;;
;;RETURNS        Nothing
;;
;;DEPENDENCIES   Excel
;;
;;TESTED WITH    W2K, WXP, W2K3, Office XP, Office 2K3
;;
;;EXAMPLES       
;
;; 
Function xlRangeClear($_ID, $_Range, OPTIONAL $_Sheet)

  Dim $_

  $_Sheet = IIf($_Sheet = '', $_ID.ActiveSheet.Name, $_Sheet)

  $_ = $_ID.WorkSheets($_Sheet).Range($_Range).Clear

  Exit @ERROR

EndFunction


;;
;;======================================================================
;;
;;FUNCTION       xlRangeFormat()
;;
;;ACTION         Sets format info for a range of cells
;;
;;AUTHOR         Glenn Barnas
;;
;;SYNTAX         xlRangeFormat(Object_ID, Range, Format [, Sheet])
;;
;;PARAMETERS     Object_ID  - REQUIRED, Excel Application Object pointer
;;		 Range      - REQUIRED, A standard range specification (A1:C3)
;;		 Format     - REQUIRED, The format array to set
;;		 Sheet      - OPTIONAL, The worksheet name to read/write
;;
;;REMARKS        Defaults to the active sheet if not specified. 
;;		 Format array is:
;;			0: FontName
;;			1: FontStyle   (Normal, Bold, Italic, Bold Italic)
;;			2: FontColor   (Index)
;;			3: FontSize
;;			4: CellColor   (Index)
;;
;;RETURNS        Variant containing the data from the defined cell range
;;
;;DEPENDENCIES   Excel
;;
;;TESTED WITH    W2K, WXP, W2K3, Office XP, Office 2K3
;;
;;EXAMPLES       xlRangeValue($XL, 'A1', $aFmt, 'Sheet1')
;
Function xlRangeFormat($_ID, $_Range, $_Format, OPTIONAL $_Sheet)

  Dim $_

  $_Sheet = IIf($_Sheet = '', $_ID.ActiveSheet.Name, $_Sheet)

  If $_Format[0]
    $_ID.WorkSheets($_Sheet).Range($_Range).Font.Name = $_Format[0]
  EndIf

  If $_Format[1]
    $_ID.WorkSheets($_Sheet).Range($_Range).Font.FontStyle = $_Format[1]
  EndIf

  If $_Format[2]
    $_ID.WorkSheets($_Sheet).Range($_Range).Font.ColorIndex = $_Format[2]
  EndIf

  If $_Format[3]
    $_ID.WorkSheets($_Sheet).Range($_Range).Font.Size = $_Format[3]
  EndIf

  If $_Format[4]
    $_ID.WorkSheets($_Sheet).Range($_Range).Interior.ColorIndex = $_Format[4]
  EndIf

  Exit

EndFunction


;;
;;======================================================================
;;
;;FUNCTION       xlFile()
;;
;;ACTION         Opens / Saves an Excel file
;;
;;AUTHOR         Glenn Barnas
;;
;;SYNTAX         xlFile(Object_ID, Function, Filespec)
;;
;;PARAMETERS     Object_ID  - REQUIRED, Excel Application Object pointer
;;		 Function   - REQUIRED, 0=Open, 1=Save(as), 2=SaveAs
;;		 File	    - REQUIRED, Filespec of file to open/save
;;
;;REMARKS        If function is "1", it will save to the current file if 
;;		 the file was opened or previously saved, otherwise it will
;;		 save the file using the defined filespec. When function is
;;		 "2", it will always save the file using the defined filespec.
;;
;;RETURNS        Nothing
;;
;;DEPENDENCIES   Excel
;;
;;TESTED WITH    W2K, WXP, W2K3, Office XP, Office 2K3
;;
;;EXAMPLES       
;
Function xlFile($_ID, $_FN, $_File)

  Dim $_

  Select
    Case $_FN = 0			; Open
      If Exist($_File)			; if the file exists, open it
        ;$_ = $_ID.Open($_File, $_Links, $_RO, $_Fmt)
        $_ = $_ID.WorkBooks.Open($_File)
        Exit @ERROR
      Else
        Exit 2				; otherwise complain that is isn't found
      EndIf

    Case $_FN = 1			; Save(as)
      ; If the file was previously saved (or opened), just re-save it,
      ; otherwise do a Save As
      If Not $_ID.ActiveWorkbook.Path
        $_ = $_ID.ActiveWorkbook.SaveAs($_File)
      Else
        $_ = $_ID.ActiveWorkbook.Save
      EndIf
      Exit @ERROR

    Case $_FN = 2			; SaveAs
      $_ = $_ID.ActiveWorkbook.SaveAs($_File)
      Exit @ERROR

EndFunction


;;
;;======================================================================
;;
;;FUNCTION       xlQuit()
;;
;;ACTION         Terminates the Excel connection
;;
;;AUTHOR         Glenn Barnas
;;
;;SYNTAX         xlQuit(Object_ID)
;;
;;PARAMETERS     Object_ID  - REQUIRED, Excel Application Object pointer
;;
;;REMARKS        This should be called to properly exit the Excel application
;;
;;RETURNS        Nothing
;;
;;DEPENDENCIES   Excel
;;
;;TESTED WITH    W2K, WXP, W2K3, Office XP, Office 2K3
;;
;;EXAMPLES       
;
Function xlQuit($_ID)

  $_ID.Quit
  $_ID = 0

  Exit 0

EndFunction



Edited by Glenn Barnas (2007-09-27 05:42 AM)
Edit Reason: Posted v1.0
_________________________
Actually I am a Rocket Scientist! \:D

Top
#151868 - 2005-11-18 11:01 PM Re: RFC: xlLib - Library of MS Excel UDFs
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
WOW - I sure could have used these when I was with the Mouse.

I'll try to give them a go and provide some feedback.

Not as needed at new place as they were with the Mouse, guess here they're not as much into dog and pony show/reports.
 

Top
#151869 - 2005-11-19 04:00 AM Re: RFC: xlLib - Library of MS Excel UDFs
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4400
Loc: New Jersey
This is the result of typical Govt busywork...

We have a product that inventories software - the report is incomprehensible, and hundreds of lines long. Manager wants a summary (in Excel). Assigns it to someone to manually review and summarize!

A Kix script with these UDFs and about 40 lines of code reads an ini file & parses the individual report files, summarizes them, and creates a multi-tabbed workbook with the detail. Now all we do is dump the unwieldly reports to .TXT files each month, run the script, and it generates the .XLS file in about 12 seconds. Of course, we turn it back to management in 4-5 days (and they're grateful for the fast turnaround!).

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#151870 - 2005-11-20 12:09 AM Re: RFC: xlLib - Library of MS Excel UDFs
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Glen,

Looks good, it just reminded med that i sent an Excel collection some years ago.

Mine to was also build to feed the management with info.

The difference is that you made yor functions ready for the UDF-forum

Try looking at this:
Collection of UDF's for Excel

Not much error handling or comments, but it contains lots of enumerated properties of Excel.

-Erik

Top
#151871 - 2005-11-20 01:56 AM Re: RFC: xlLib - Library of MS Excel UDFs
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4400
Loc: New Jersey
Wow! Thanks, Erik! I'll take a look at incorporating some of those capabilities. Some, like search and page/print format, I had not even considered, but if your foundation is already there, I'll build on it.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#151872 - 2006-11-11 02:28 PM Re: RFC: xlLib - Library of MS Excel UDFs
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Glenn and Kholm,
Wow these UDFs are great! Thanks for sharing.
Are these the most current UDFs?
If not, could you point me in the right direction?
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#151873 - 2006-11-11 08:45 PM Re: RFC: xlLib - Library of MS Excel UDFs
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4400
Loc: New Jersey
Mine haven't changed. You can always check my website for the most current versions of stuff I write.

Glenn

Top
#151874 - 2006-11-11 10:05 PM Re: RFC: xlLib - Library of MS Excel UDFs
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
do you mean www.innotechcg.com?
i tried from work and from home and get a message that it can not be displayed:
Error Code: 403 Forbidden
do i need to change a setting on my machine?
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#151875 - 2006-11-13 01:51 AM Re: RFC: xlLib - Library of MS Excel UDFs
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4400
Loc: New Jersey
Well, that rots! I've been doing lots of network upgrades this weekend, so it's possible I've mucked something up. I brought a new VMware test server online, replaced the SAN switch, migrated to the new, virtualized firewall, installed the new wireless access software in the WAPs, and added an iSCSI target to the infrastructure. And that's in between cooking, laundry, and yardwork!

I have to drive up the street and hack into an open wireless network to actually test external web access. It's possible that the settings in the new ISA-2K6 firewall aren't exactly the same as the ISA-2K4 version. Since I'm running on my home cable Internet connection, I'm publishing the web site on port 2080 - it's possible that some firewalls may not permit http traffic on that port. This is not likely the case, since you're having problems from home, too.

I'll try to have the access issues resolved within a day or so.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#151876 - 2006-11-13 02:54 AM Re: RFC: xlLib - Library of MS Excel UDFs
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I thought before I was getting 12202 error. Now I get:

Error Code: 403 Forbidden. ISA Server is configured to block HTTP requests that require authentication. (12250)

Top
#151877 - 2006-11-13 03:23 AM Re: RFC: xlLib - Library of MS Excel UDFs
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4400
Loc: New Jersey
Yeah - it should be working now..

Blew away the config for the listener and publishing rule, recreated it, and now it works. ISA-2K6 has lots of new parameters for web publishing that I didn't take the time to fully understand before I migrated the ISA-2K4 settings over.

I hopped onto the local WIFI to test it - not sure if it's my laptop settings or what, but the javascript news scroller doesn't work - works internally on my desktop.

I guess that was you earlier filling my firewall logs while I was changing the settings, eh?

Glenn

Top
#151878 - 2006-11-13 04:15 AM Re: RFC: xlLib - Library of MS Excel UDFs
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Seems to be working now, incl. the news scroller.
_________________________
There are two types of vessels, submarines and targets.

Top
#151879 - 2006-11-13 04:21 AM Re: RFC: xlLib - Library of MS Excel UDFs
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Ja, it works but the news is hard to read while scrolling.
Top
#151880 - 2006-11-13 04:36 AM Re: RFC: xlLib - Library of MS Excel UDFs
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4400
Loc: New Jersey
Thanks guys! I think it's the setting on my laptop that was preventing the display earlier.

The news scroller is a bit funky - if you click on the whitespace, you can pause, or even drag the news up or down. If you click on the text, it takes you to that section of the web site.

Glenn

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1739 anonymous users online.
Newest Members
min_seow, Audio, Hoschi, Comet, rrosell
17881 Registered Users

Generated in 0.061 seconds in which 0.023 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org