Solved

Writing to Rows in EXCEL via VBScript

Posted on 2011-02-23
1
379 Views
Last Modified: 2012-05-11
How do I setup the script to write new line to next row in single Worksheet, without opening a new workbook in this script?
Option Explicit

Dim strFile, strInputLog, strOutputLog, objFSO, objInput, objOutput
Dim strLine, blnError, intCount, dtmToday, intSummary
Dim objExcel, objWorkbook

Const ForReading = 1

dtmToday = Now()

' Specify input log file.
strInputLog = "D:\Documents and Settings\figueroadaniel\My Documents\ClassPatchInstallation_0792.log"
' Output file.
strOutputLog = "d:\output.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

' Open files.
Set objInput = objFSO.OpenTextfile(strInputLog, ForReading)
Set objOutput = objFSO.CreateTextFile(strOutputLog, True)

' Read each line of input log file.
intCount = 0
blnError = False
Do Until objInput.AtEndOfStream
    strLine = objInput.ReadLine
    intCount = intCount + 1
    If (InStr(strLine, "Class") > 0) Then
    
    	Set objExcel = CreateObject("Excel.Application")
		Set objWorkbook = objExcel.Workbooks.Open("d:\test.xls")

		objExcel.Application.Visible = True
		objWorkbook.WorkSheets(1).Activate
		objWorkbook.WorkSheets(1).Cells(1, 1).Value = strLine
		objExcel.ActiveWorkbook.Close
		objExcel.Application.Quit
   
      
        objOutput.WriteLine intCount
        objOutput.WriteLine strLine
        blnError = True
    Else
        If (blnError = True) Then
            objOutput.WriteLine strLine
            blnError = False
        End If
    End If
Loop

' Close input file.

objInput.Close
objOutput.Close

Open in new window

0
Comment
Question by:danfiggolf
1 Comment
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34966410
Hi, try this.  You will need to modify the strColumns value to specify each column names that your inserting data into.

Regards,

Rob.
Option Explicit

Dim strFile, strInputLog, strOutputLog, objFSO, objInput, objOutput
Dim strLine, blnError, intCount, dtmToday, intSummary
Dim objExcel, objWorkbook

Const ForReading = 1

dtmToday = Now()

' Specify input log file.
strInputLog = "D:\Documents and Settings\figueroadaniel\My Documents\ClassPatchInstallation_0792.log"
' Output file.
strOutputLog = "d:\output.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

' Open files.
Set objInput = objFSO.OpenTextfile(strInputLog, ForReading)
Set objOutput = objFSO.CreateTextFile(strOutputLog, True)

strXLS = "D:\Test.xls"
strSheetName = "data"
strColumns = "Field1,Field2"

Set adoConnection = CreateObject("ADODB.Connection")
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strXLS & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
adoConnection.Open strConnect

' Read each line of input log file.
intCount = 0
blnError = False
Do Until objInput.AtEndOfStream
    strLine = objInput.ReadLine
    intCount = intCount + 1
    If (InStr(strLine, "Class") > 0) Then

		strSQL = "insert into [" & strSheetName & "] (" & strColumns & ") values ('" & strLine & "')"
		adoConnection.execute strSQL

        objOutput.WriteLine intCount
        objOutput.WriteLine strLine
        blnError = True
    Else
        If (blnError = True) Then
            objOutput.WriteLine strLine
            blnError = False
        End If
    End If
Loop

adoConnection.close
Set adoConnection = Nothing

' Close input file.

objInput.Close
objOutput.Close

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question