Visio 2003 Standard SP2, Visual Basic 6.3, Excel 2003 Pro SP3
This site highly recommended by a co-worker, first post, rather lenghty, sorry.
Posting to both Visio and VBA, and also contains an association with Excel, so here goes.
Background/problem: What I want to do is be able to open an Excel spreadsheet, and read in either one line at a time, or an entire table into Visio. I am trying to follow the documentation on MSDN. The Visio 2002 pages have a lot of detail, with examples but I am not having any luck implementing the code examples. The Visio 2003 MSDN reference is very high level and says to use the help that comes with Visio. So I am combining the examples from 2002 online and those embedded in 2003 help, and I have not gotten very far. The following code extract is how far I get before the VBA fails:
Sub dbXLInputs()
Dim wrkspcODBC As Workspace
Dim dbsOV As Database
Dim dbsOVtemp As Database
Dim rstNodes As Recordset
Dim prpLoop As Property
Dim qdfNew As QueryDef
Dim fldLoop As Field
‘ Create an ODBCDirect workspace.
Set wrkspcODBC = CreateWorkspace(“ODBCWorkspace”, “admin”, _
“”, dbUseODBC)
‘ Workspaces.Append wrkODBC
‘ Open read-only Database object by entering only the
‘ missing information in the ODBC Driver Manager dialog
‘ box.
‘ MsgBox “Opening OV7 Excel file …”
Set dbsOV = wrkspcODBC.OpenDatabase(“D:OV2.input.samples.xls”, _
dbDriverPrompt, _
True)
‘ 3rd param values dbDriverNoPrompt, dbDriverPrompt, dbDriverComplete or dbDriverCompleteRequired
‘ Have tried with and without this command, with no change in behavior
Set qdfNew = dbsOV.Connection.CreateQueryDef(“OVNodes”)
‘ Create and append new Field objects for the new
‘ TableDef object.
‘ With qdfNew
‘ ‘ The CreateField method will set a default Size
‘ ‘ for a new Field object if one is not specified.
‘ .Fields.Append .CreateField(“Node”, dbText)
‘ .Fields.Append .CreateField(“Description”, dbText)
‘ .Fields.Append .CreateField(“LinkToNode”, dbText)
‘ End With
‘ Open the record set as a …
‘ I have attempted to make this call each of the type values, dbOpenDynamic,
‘ dbOpenDynaset, dbOpenSpanshot and dbOpenForwardOnly
‘ All the examples us a Name, 1st arguement, that either does not appear prior
‘ to this call or is related to an .mdb file. The help says that using the ODBC
‘ interface is the way to access Excel files.
Set rstNodes = _
dbsOV.OpenRecordset(“OVNodes”, dbOpenDynamic, dbReadOnly)
‘ The OpenRecordSet gives me a 3001, invalid arguement error,every time
‘ I have tried what seems like every combination of the arguments that are valid
‘ for an ODBCDirect workspace (non Jet workspace)
‘ …
End
Assumptions for using this approach: Visio provides a wizard to create org charts from an Excel spreadsheet, so I was attempting to us the same mechanism to write my own Add-On.