简体   繁体   English

使用vbscript并将ADO记录集设置为变量

[英]Using vbscript and setting an ADO recordset to a variable

I have a lot of PDF files I am trying to rename based on criteria found in an excel spreadsheet. 我有很多PDF文件,我正在尝试根据Excel电子表格中的条件重命名。 The spreadsheet lists the ID, last name, first name. 电子表格中列出了ID,姓氏,名字。

| 123456 | Smith | Joe |

I can open and read from excel fine and can create the ado connection and get a response to my query, but cannot find a way to take the part of the recordset that contains the filename and save as a variable. 我可以打开并从excel中读取,可以创建ado连接并获得对查询的响应,但是找不到找到包含文件名并将其另存为变量的记录集的方法。

The code I am using:(updated to current codeset) 我正在使用的代码:(已更新为当前代码集)

'On Error Resume Next

LetterDirectory = InputBox("What letter are we scanning?")

DirectoryLocation = "C:\CLNotes\"
LetterDirectory = UCase(LetterDirectory)
SubDirectory = DirectoryLocation & LetterDirectory
FullDirectory = DirectoryLocation & LetterDirectory & "\*.pdf"

'Creating Excel objects
Set xl = CreateObject("Excel.Application")
Set xlBook = xl.Workbooks.Open("c:\CLnotes\dbo_Patient.xlsx")
Set xlSheet = xlBook.Worksheets(LetterDirectory)
xl.Visible = True

'Set Variables
xlRow = 1
totalRows = xl.WorksheetFunction.CountA(xlSheet.Range("A:A"))
oldFilename = 0

For xlRow = 1 to totalRows

LastName = xlSheet.Cells.Item(xlRow, 2).text
FirstName = xlSheet.Cells.Item(xlRow, 3).text
FullName = LastName & " " & Firstname
newFilename = xlSheet.Cells.Item(xlRow, 1).Text
'FullName = "b lorraine"

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
objConnection.Open ("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';")
objRecordSet.Open ("SELECT System.FileName FROM SYSTEMINDEX WHERE Contains('" & FullName & "')"), objConnection
objRecordSet.MoveFirst
oldFilename = objRecordset.Fields.Item("System.FileName")
Do Until objRecordset.EOF
    Wscript.Echo objRecordset.Fields.Item("System.FileName")
    objRecordset.MoveNext
Loop
Next

Thanks. 谢谢。

IF 如果

Wscript.Echo objRecordset.Fields.Item("System.FileName")

outputs 'the part of the recordset that contains the filename' (you are interested in) AND 输出“记录集的包含文件名的部分”(您对此感兴趣),并且

Set oldFilename = objRecordset.Fields.Item("System.FileName")

was your attempt to 'save [that value] as a variable' THEN 是您尝试“将[那个值]保存为变量”然后

oldFilename = objRecordset.Fields.Item("System.FileName")

will solve your problem, because Set is to be used for assignment of objects only (not 'plain' data types like strings). 将解决您的问题,因为Set仅用于分配对象(不用于字符串等“普通”数据类型)。

That Set var = non-object is the cause of your trouble - as I assume - is hidden by your use of the EVIL global On Error Resume Next . Set var = non-object是您造成麻烦的原因(我假设是),因为您使用EVIL全局On Error Resume Next来隐藏它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM