VBA ADO访问受密码保护的文件

时间:2018-07-31 17:31:05

标签: vba excel-2010 ado

我在excel 2010中使用ADO和VBA。我可以访问在excel文件中具有密码的表。 excel文件提示我输入密码。我想知道是否可以对密码进行编码,以便在尝试在受密码保护的excel文件上使用ADO时不提示输入密码。

下面的代码:

Option Explicit

Private Sub btnTest_Click()

Dim xl As Object 
Dim conn As New ADODB.Connection
Dim recSet As ADODB.Recordset
Dim conString As String
Dim wkbName As String
Dim SQL As String
Dim DBPath As String

DBPath = "path\to\file\data.xlsx"
Set xl = GetObject(DBPath)
wkbName = "[table1$]"
conString = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
SQL = "select * from " & wkbName

'Open connection
conn.Open conString

'Itterate over record set
Set recSet = New ADODB.Recordset
recSet.Open SQL, conn

Do Until recSet.EOF
    'process your data here
    Debug.Print recSet!Col1
    recSet.MoveNext
Loop
End Sub

0 个答案:

没有答案