Read and write into a file using VBScript

前端 未结 9 1475
轮回少年
轮回少年 2020-11-27 06:34

How can we read and write some string into a text file using VBScript? I mean I have a text file which is already present so when I use this code below:-

Set         


        
9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 07:18

    Below is some simple code to execute this:

    sLocation = "D:\Excel-Fso.xls"
    sTxtLocation = "D:\Excel-Fso.txt"
    Set ObjExl = CreateObject("Excel.Application")
    Set ObjWrkBk = ObjExl.Workbooks.Open(sLocation)
    Set ObjWrkSht = ObjWrkBk.workSheets("Sheet1")
    ObjExl.Visible = True
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set FSOFile = FSO.CreateTextFile (sTxtLocation)
    sRowCnt = ObjWrkSht.usedRange.Rows.Count
    sColCnt = ObjWrkSht.usedRange.Columns.Count
    For iLoop = 1 to sRowCnt
      For jLoop = 1 to sColCnt 
        FSOFile.Write(ObjExl.Cells(iLoop,jLoop).value) & vbtab 
      Next
    Next
    
    Set ObjWrkBk = Nothing
    Set ObjWrkSht = Nothing
    Set ObjExl = Nothing
    Set FSO = Nothing
    Set FSOFile = Nothing
    

提交回复
热议问题