VBScript Excel问题

VBScript Excel问题,excel,vbscript,Excel,Vbscript,我制作了一个vbs脚本,可以将Excel文件读入Dictionary对象。当我运行脚本时,它什么也不做。没有错误消息 代码如下: Set objWords = CreateObject("Scripting.Dictionary") objWords.CompareMode = 1 CurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") Set objExcel = CreateObject(

我制作了一个vbs脚本,可以将Excel文件读入Dictionary对象。当我运行脚本时,它什么也不做。没有错误消息

代码如下:

Set objWords = CreateObject("Scripting.Dictionary")
objWords.CompareMode = 1

CurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(CurPath & "/RE Glossary.xls")
Set objWorksheet = objExcel.ActiveWorkBook.WorkSheets("MG")


intRow = 1

Do Until (objExcel.Cells(intRow, 1).Value) = ""
    Value1 = (objExcel.Cells(intRow, 1).Value)
    Value2 = (objExcel.Cells(intRow, 2).Value)
    objWords.item(Value1) = Value2
Loop

objExcel.Quit

msgbox "There are " & objWords.Count & " words in the glossary."

word = inputbox("word")
if objWords.exists(word) then
    msgbox word & vbnewline & "------------" & vbnewline & objWords.item(word)
else
    msgbox word & " is not in the glossary."
end if

您不需要将
intRow=intRow+1
添加到循环中吗

intRow = 1
Do Until (objExcel.Cells(intRow, 1).Value) = ""
    Value1 = objExcel.Cells(intRow, 1).Value
    Value2 = objExcel.Cells(intRow, 2).Value
    objWords.item(Value1) = Value2
    intRow = intRow + 1
Loop

如果您只想在Excel中查找单词,那么使用ADO会更快。以下是一些注意事项:

Dim cn, rs
Dim strFile, strCon, strSQL, Word

CurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

strFile = "/RE Glossary.xls"

''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used. 
''This is the Jet 4 connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurPath & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"

''Late binding, so no reference is needed

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

Word = InputBox("Word")

If Word <> vbNullString Then
   strSQL = "SELECT Count(F1) As WordCount " _
       & "FROM [Sheet2$] AS a " _

   rs.Open strSQL, cn, 3, 3

   strMessage = "There are " & rs.Fields("WordCount") & " words in the glossary."

   rs.Close

   strSQL = "SELECT F1 " _
       & "FROM [Sheet2$] a " _
       & "WHERE F1 = '" & Word & "'"

   rs.Open strSQL, cn, 3, 3

   If rs.RecordCount > 0 Then
      strMessage = strMessage & vbNewLine & word & vbNewLine & "------------" & vbNewLine & rs.Fields("F1")
   Else
      strMessage = strMessage & vbNewLine & word & " is not in the glossary."
   End If

   ''Tidy up
   rs.Close
   Set rs=Nothing
   cn.Close
   Set cn=Nothing

Else
   strMessage = "Nothing selected."
End If

MsgBox strMessage
Dim cn,rs
Dim strFile、strCon、strSQL、Word
CurPath=CreateObject(“Scripting.FileSystemObject”).GetAbsolutePathName(“.”)
strFile=“/RE Glossary.xls”
''注意,如果HDR=No,则F1、F2等用于列名,
''如果HDR=是,则范围第一行中的名称
''可以使用。
''这是Jet 4连接字符串,您可以获得更多
“这里:http://www.connectionstrings.com/excel
strCon=“Provider=Microsoft.Jet.OLEDB.4.0;数据源=”&CurPath&strFile_
&“扩展属性=”“Excel 8.0;HDR=No;IMEX=1”“
''后期绑定,因此不需要引用
Set cn=CreateObject(“ADODB.Connection”)
Set rs=CreateObject(“ADODB.Recordset”)
cn.开放式strCon
Word=输入框(“Word”)
如果单词为空字符串,则
strSQL=“选择Count(F1)作为WordCount”_
&“从[Sheet2$]开始作为”_
开放式strSQL,cn,3,3
strMessage=“词汇表中有”&rs.Fields(“字数”)和“单词”
rs.Close
strSQL=“选择F1”_
&“从[Sheet2$]a”_
&“其中F1='”&Word&'”
开放式strSQL,cn,3,3
如果rs.RecordCount>0,则
strMessage=strMessage&vbNewLine&word&vbNewLine&“--------------”&vbNewLine&rs.Fields(“F1”)
其他的
strMessage=strMessage&vbNewLine&word&“不在术语表中。”
如果结束
“整理
rs.Close
设置rs=无
cn.Close
设置cn=Nothing
其他的
strMessage=“未选择任何内容。”
如果结束
MsgBox strMessage

当你说
它什么都不做时,你看到输入框和msgboxes了吗?谢谢大家,我正在写的时候正要睡觉:)