qq_36944937 2018-03-31 17:16 采纳率: 0%
浏览 1035
已结题

vbs显示Set str = file.readall 会报错 缺少对象800a01a8

我的1.txt文件里是aaa bbb ccc 2.txt文件里是www aaa bbb 是不是我文本文件里面随便写的数据不对啊???求解决
Dim sdict
Dim i
Dim keys
Call GetInfo(sdict)

Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象

Set ExcelBook = ExcelApp.Workbooks.Add

Set ExcelSheet = ExcelBook.Worksheets(1) '添加工作页
ExcelSheet.Activate

ExcelApp.DisplayAlerts = False

ExcelSheet.Name="sheet1"

      ExcelSheet.range("A1").Value = "blockname"
      ExcelSheet.range("B1").value="1.txt"
      ExcelSheet.range("C1").value="2.txt"


      '取出所有关键字
keys = sdict.Keys
'取出所有值
items =sdict.Items
'循环dir逐条输出到excel中
      For i = 0 To sdict.Count - 1
    ExcelSheet.cells(i+2,1).value=keys(i)
    '输出所有的值
    value=value&items(i)&vbCrlf

    if items(i) = 0 then
    ExcelSheet.cells(i+2,2).value="Y"
    ExcelSheet.cells(i+2,3).value="Y"
    elseif items(i) =1 then 
    ExcelSheet.cells(i+2,2).value="Y"
    ExcelSheet.cells(i+2,3).value="N"
    elseif items(i)=2   then
    ExcelSheet.cells(i+2,2).value="N"
    ExcelSheet.cells(i+2,3).value="Y"
    end if
Next

ExcelBook.SaveAs "f:\1.xlsx"
ExcelBook.close
set excelApp=nothing
set ExcelBook=nothing
set ExcelSheet=nothing

Public Function GetInfo(sdict)

Dim strarry1
Dim strarry2
Dim Count1
Dim Count2
Dim i
'读取txt1
Call ReadFile("f:\1.txt",strarry1)
'读取txt2
Call ReadFile("f:\2.txt",strarry2)

'数组长度
Count1 = ubound(strarry1) + 1
Count2 = ubound(strarry2) + 1
set sdict=CreateObject("Scripting.Dictionary")

'如果仅在txt1中存在时,标记为1
For i = 0 To Count1 - 1
    sdict.add strarry1(i),1
Next

For i = 0 To Count2 - 1
    '如果同时在txt1和txt2中存在时,标记为0
    if sdict.Exists(strarry2(i)) then
        sdict.add strarry2(i),0
    '如果仅在txt2中存在时,标记为2
    else
        sdict.add strarry2(i),2
    end if
Next


End Function

Public Function ReadFile(filePath,strarry)
    '读取txt文件
    Const ForReading = 1
    Dim fs
    Dim file
    Dim str


    Set fs = CreateObject("Scripting.FileSystemObject")
    Set file = fs.OpenTextFile(filePath, ForReading)
    Set str = file.readall

fs.close

If   str=" "   then   
        msgbox "txt为空" 
    Exit Function
End   if  

strarry = split(str,vbCrlf)  
Set fs = nothing
End Function
  • 写回答

1条回答

  • threenewbee 2018-03-31 19:53
    关注

    Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象
    这个要求你的电脑中安装了office或者excel,而且是完整版

    你有安装么?

    如果你安装了,那么看下你的权限。

    评论

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题