1

ExcelシートにリンクされたMS Accessでリンクテーブルを作成しようとしています。VBscripting を使用してこれを実行したいと考えています。

私のシナリオは、非常に頻繁に更新される Excel シートを作成することです。しかし、私のスクリプトは、Excel シート (リンクされたテーブル) のレプリカである必要がある MSAccess のテーブルから値を取得します。

VBscript に Excel シートへのリンク テーブルを作成できるコードがあるかどうかを知りたいです。

4

1 に答える 1

1

サンプルスクリプトを次に示します。

   Dim cn ''As ADODB.Connection
   Dim ct ''As ADOX.Catalog
   Dim tbl ''As ADOX.Table

   Dim strLinkXL ''As String
   Dim strMDB ''As String

   strLinkXL = "C:\Docs\LTD.xls"
   strMDB = "C:\Docs\LTD.mdb"

   ''Create Link...
   Set cn = CreateObject("ADODB.Connection")
   cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & strMDB & ";" & _
          "Persist Security Info=False"

   Set ct = CreateObject("ADOX.Catalog")
   Set ct.ActiveConnection = cn

   Set tbl = CreateObject("ADOX.Table")
   Set tbl.ParentCatalog = ct


   ''Link Excel using named range
   Set tbl = CreateObject("ADOX.Table")
   Set tbl.ParentCatalog = ct

   With tbl
     .Name = "LinkTableXLRange"
     .properties("Jet OLEDB:Link Provider String") = "Excel 8.0;DATABASE=" _
         & strLinkXL & ";HDR=Yes"
     ''The named range
     .properties("Jet OLEDB:Remote Table Name") = "Data_Range"
     .properties("Jet OLEDB:Create Link") = True
   End With

   ''Append the table to the tables collection
   ct.Tables.Append tbl
   Set tbl = Nothing

   ''Link Excel by sheet name
   Set tbl = CreateObject("ADOX.Table")
   Set tbl.ParentCatalog = ct

   With tbl
     .Name = "LinkTableXLSheet"
     .properties("Jet OLEDB:Link Provider String") = "Excel 8.0;DATABASE=" _
           & strLinkXL & ";HDR=Yes"
     ''Note the use of $, it is necessary
     .properties("Jet OLEDB:Remote Table Name") = "Sheet2$"
     .properties("Jet OLEDB:Create Link") = True
   End With

   ''Append the table to the tables collection
   ct.Tables.Append tbl
   Set tbl = Nothing

から: http://wiki.lessthandot.com/index.php/Linking_Tables_via_Jet_and_ADO

于 2010-08-23T16:39:15.857 に答える