使用VBScript将Excel工作表链接到MS Access中的表
我正在尝试在MS Access中创建链接到Excel工作表的链接表。我想通过VBScript来实现这一点 我的设想是,我将有一个excel表格,该表格将经常更新。但我的脚本从MSAccess中的表中提取值,该表应该是Excel工作表(链接表)的副本使用VBScript将Excel工作表链接到MS Access中的表,excel,ms-access,vbscript,linked-tables,Excel,Ms Access,Vbscript,Linked Tables,我正在尝试在MS Access中创建链接到Excel工作表的链接表。我想通过VBScript来实现这一点 我的设想是,我将有一个excel表格,该表格将经常更新。但我的脚本从MSAccess中的表中提取值,该表应该是Excel工作表(链接表)的副本 所以我想知道VBscript中是否有任何代码,我可以在其中创建一个链接到Excel工作表的表。以下是一些示例脚本 Dim cn ''As ADODB.Connection Dim ct ''As ADOX.Catalog Dim t
所以我想知道VBscript中是否有任何代码,我可以在其中创建一个链接到Excel工作表的表。以下是一些示例脚本
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
From:这里是一些示例脚本
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
发件人:这听起来有点奇怪。如果Excel已链接,则会显示新值。图纸只需链接一次。您试图用这种方法克服的问题是什么?这听起来有点奇怪。如果Excel已链接,则会显示新值。图纸只需链接一次。您试图用这种方法克服的问题是什么?