Example 1: Excel Macros

This collection of Microsoft Excel macros illustrates the functionality of the OLE CCI for NCSA Mosaic for Windows.

Because Excel contains a built-in subset of Visual Basic, the macros are written in this language. In practice (as in our own example), these macros might be assigned to buttons built into an Excel spreadsheet.


Declare the Mosaic CCI object

Dim Mosaic_CCI_Obj As Object

Open the OLE connection to Mosaic

Sub LoadMosaic()
Set Mosaic_CCI_Obj = CreateObject("Mosaic.MosaicCCI")
End Sub

Description: This macro initializes the OLE connection to Mosaic. It must be called before any of the other CCI functions may be used, or else terrible and spooky things might happen.


Close the OLE connection to Mosaic

Sub UnloadMosaic()
Set Mosaic_CCI_Obj = Nothing
End Sub

Description: This macro closes the OLE connection to Mosaic.


Find out the CCI version

Sub GetVersion()
Selection.Value = Mosaic_CCI_Obj.GetVersion()
End Sub

Description: This macro illustrates the use of the GetVersion() method, which determines the version of the CCI that is implemented in the current version of Mosaic. GetVersion() returns a character string, which is stored in the currently-active cell in the spreadsheet.


Tell Mosaic to load the NCSA Home Page

Sub AccessNCSAHomePage()
Mosaic_CCI_Obj.ResolveUrl ("http://www.ncsa.uiuc.edu")
End Sub

Description: This macro illustrates the use of the ResolveUrl() method, which causes Mosaic to fetch and display a given URL. The ResolveUrl() method takes a single argument -- a character string representing a URL. In this case, the URL is hardcoded into the method call. This macro does not change the contents of any cells in the spreadsheet.


Tell Mosaic to load an arbitrary URL

Sub AccessUrl()
Mosaic_CCI_Obj.ResolveUrl (ActiveCell.Value)
End Sub

Description: This is another macro to illustrate the use of the ResolveUrl() method. This macro differs from the previous one in that it gets the argument for ResolveUrl() from the contents of the currently-active cell in the spreadsheet. The spreadsheet cell remains unchanged.


Find out the title and URL of the currently-displayed document

Sub ShowCurrentDocument()
Selection.Value = Mosaic_CCI_Obj.GetCurrentDocTitle
Selection.Offset(0, 1).Value = Mosaic_CCI_Obj.GetCurrentDocUrl
End Sub

Description: This macro illustrates two CCI methods, GetCurrentDocTitle() and GetCurrentDocUrl(). GetCurrentDocTitle() returns the title of the document currently displayed in Mosaic; GetCurrentDocUrl() returns its URL. The macro places these two strings in the currently-active cell and the one adjacent to the right.


Get the session history list from Mosaic

Sub GetHistory()
Index = 1
Total = Mosaic_CCI_Obj.GetHistoryCount()
Item = Mosaic_CCI_Obj.GetHistoryItem(Index)
Do Until Index > Total
    ActiveCell.Value = Item
    ActiveCell.Offset(1, 0).Activate
    Index = Index + 1
    Item = Mosaic_CCI_Obj.GetHistoryItem(Index)
Loop
End Sub

Description: This macro shows how to get the session history information from Mosaic. It illustrates the use of two CCI methods, GetHistoryCount() and GetHistoryItem(). GetHistoryCount() takes no arguments, and returns the number of items in Mosaic's session history list. GetHistoryItem() takes an integer argument N, and returns the Nth item on the list (a URL string). This method is called repeatedly, and the corresponding strings are placed in successive cells of the spreadsheet.


Find out all the links in the currently-displayed document

Sub GetAnchors()
Index = 1
Total = Mosaic_CCI_Obj.GetAnchorCount()
ActiveCell.Value = Total
ActiveCell.Offset(1, 0).Activate
Do Until Index > Total
    DocUrl = Mosaic_CCI_Obj.GetAnchorUrl(Index)
    DocDsc = Mosaic_CCI_Obj.GetAnchorDesc(Index)
    ActiveCell.Value = DocDsc
    ActiveCell.Offset(0, 1).Value = DocUrl
    ActiveCell.Offset(1, 0).Activate
    Index = Index + 1
Loop
End Sub

Description: This macro illustrates three CCI methods that manipulate the hyperlinks (anchors) in the document currently displayed in Mosaic. These three methods are GetAnchorCount(), GetAnchorUrl(), and GetAnchorDesc().

GetAnchorCount() takes no arguments, and returns the number of anchors that are present in the document. GetAnchorUrl() takes an integer argument N, and returns a string representing the URL which is pointed to by the Nth link in the document. GetAnchorDesc() takes the same integer N, and returns the "descriptive text" for the link -- the bit of text that is highlighted in Mosaic's display.

The macro uses these three methods to produce a list of URLs and their associated text. The list uses two columns in the spreadsheet, as the URLs and descriptive text are place side by side.


Tell Mosaic to fetch a URL and save it to a given file

Sub SaveUrlToDisk()
Result = Mosaic_CCI_Obj.DownloadUrl(ActiveCell.Value, ActiveCell.Offset(0, 1).Value)
End Sub

Description: This macro illustrates the DownloadUrl() method of the CCI. This method tells Mosaic to fetch a document and save it to a given file. DownloadUrl() takes two arguments: the URL to be accessed, and the name of the file to be saved. This macro gets the values for those two arguments from the currently-active cell and the one beside it to the right, respectively.


Run a slide show from a list of URLs

Sub SlideShow()
Item = ActiveCell.Value
Do Until Item = ""
    Mosaic_CCI_Obj.ResolveUrl (Item)
    Application.Wait (Now + TimeValue("00:00:05"))
    Item = Mosaic_CCI_Obj.GetCurrentDocUrl()
    If (Item = ActiveCell.Value) Then
        ActiveCell.Offset(1, 0).Activate
        Item = ActiveCell.Value
    Else
        Item = ""
    End If
Loop
End Sub

Description: This macro uses some of the methods we have already seen to run a slide show in Mosaic. It starts with the URL in the currently-active cell and moves down until it reaches an empty cell. At each cell, it orders Mosaic to display the document referenced by the URL, and waits 5 seconds.


Monitor the URLs visited by Mosaic

Sub EavesDrop()
Item = Mosaic_CCI_Obj.GetCurrentDocUrl()
Do Until Item = ""
    ActiveCell.Value = Item
    Last = Item
    ActiveCell.Offset(1, 0).Activate
    Item = Mosaic_CCI_Obj.GetCurrentDocUrl()
    Do Until (Item <> Last)
        Item = Mosaic_CCI_Obj.GetCurrentDocUrl()
    Loop
    If (Item = "http://www.ncsa.uiuc.edu/SDG/Software/WinMosaic/HomePage.html") Then
        Item = ""
    End If
Loop
End Sub

Description: This macro constantly polls Mosaic, watching for new URLs to be accessed. Each time a new URL is accessed by Mosaic, it is brought into the next cell of the spreadsheet using GetCurrentDocUrl. The monitoring stops when the Windows Mosaic home page is accessed.


WWW'95 Tutorial, Darmstadt, 10 Apr 1995
Briand Sanderson (briand@ncsa.uiuc.edu)
Tom Magliery (mag@ncsa.uiuc.edu)