0
我目前正在创建一个脚本。该脚本将需要使用ADO获取已关闭的Excel工作簿的工作表名称。但是,表单中有时会有多余字符,例如单引号和美元符号。ADO + VBA表名返回值增加了额外的字符
结果表明:工作清单表 - >“作业列表表” $或作业表 - >招贤纳才$
我知道这很容易被操纵,但有人可以告诉我为什么?
这里是我的代码:
Option Explicit
Private Const SCHEMA_TABLES As Integer = 20
Private Const OPEN_FORWARD_ONLY As Integer = 0
Private Const LOCK_READ_ONLY As Integer = 1
Private Const CMD_TEXT As Long = 1
Private Const PROVIDER As String = "Microsoft.ACE.OLEDB.12.0"
Private Const XL_PROP As String = """Excel 12.0;HDR=No"""
Private Const SHEETS_FIELD_NAME As String = "TABLE_NAME"
Private Sub GetJoblist_Click()
Dim fPath As String
Dim fname As String
Dim oConn As Object
Dim oRS As Object
Dim connString As String
Dim sql As String
Dim found As Boolean
Dim sheetField As String
Dim i As Integer
Dim key As String
Dim job_used As String
Dim sheetfieldvalue As String
Dim SelectedFile As String
Dim Source_flow As Range, target_flow As Range
Dim TP_location As String, TP_filename As String, TP_formula As String, getcellvalue As String
'Define the path and file name
fPath = TextBox1.Value
'Define key word
key = "Job"
'Late bind the ADO objects
Set oConn = CreateObject("ADODB.Connection")
Set oRS = CreateObject("ADODB.Recordset")
'Open conection
connString = "Provider=" & PROVIDER & ";" & _
"Data Source=" & fPath & ";" & _
"Extended Properties=" & XL_PROP & ";"
oConn.Open connString
'Search for the sheet name containing your key
'in the tables (ie sheets) schema
found = False
oRS.Open oConn.OpenSchema(SCHEMA_TABLES)
i = 1
Sheets.Add.Name = "Job_list"
Do While Not oRS.EOF
sheetField = oRS.Fields(SHEETS_FIELD_NAME).Value ' why does this returns a value with ' and '$?
Cells(i, 18) = sheetField
If InStr(sheetField, key) > 0 Or InStr(sheetField, UCase(key)) > 0 Then
found = True
If Right(sheetField, 2) = "$'" Then
sheetfieldvalue = Left(sheetField, Len(sheetField) - 2)
sheetfieldvalue = Right(sheetfieldvalue, Len(sheetfieldvalue) - 1)
Cells(i, 17) = sheetfieldvalue
Else:
sheetfieldvalue = Left(sheetField, Len(sheetField) - 1)
Cells(i, 17) = sheetfieldvalue
End If
Exit Do
End If
oRS.MoveNext
i = i + 1
Loop
oRS.Close
Set oRS = Nothing
oConn.Close
Set oConn = Nothing
End Sub