Export to Excel, Lotus notes domino - lotus-domino

I have a view which is displaying 9 lines of information per every document. In this view I have Export to Excel functionality using the below code to Export document to excel.
Data isn’t exporting properly for first two documents , for example if I have 7 lines for the first document then it should export 7 lines but its exporting 2 lines only . It is happening for the first 2 documents only, from the 3rd document irrespective of any line no.of information it is exporting to excel perfectly. I tried to modify the code for row% from row% = row%+2 to row% = row%+3 , 4 or 5, but its unnecessary creating rows in the excel sheet its not the dynamic one and looks odd as well. Any idea what should I do so that rows should increase dynamically.
Sub Initialize
'On Error Goto errhandler
On Error Resume Next
Dim session As New NotesSession
Dim db As NotesDatabase
Dim doccoll As NotesDocumentCollection
Dim view As NotesView
Dim doc As NotesDocument
Dim otherdoc As NotesDocument
Set db = session.CurrentDatabase
Set view = db.GetView("CRMOpenIssue")
Set doccoll=db.UnprocessedDocuments
Set oExcel = CreateObject ( "Excel.Application" )
Set oWorkbook = oExcel.Workbooks.Add
Set oWorkSheet= oWorkbook.Sheets ( 1 )
oWorkSheet.Cells(1,1).value="Quote# "
oWorkSheet.Cells(1,2).value="Quote Line#"
oWorkSheet.Cells(1,3).value="Customer - fab"
oWorkSheet.Cells(1,4).value="OppNum"
oWorkSheet.Cells(1,5).value="OppLine#"
oWorkSheet.Cells(1,6).value="Open Issue#"
oWorkSheet.Cells(1,7).value="Open Issue"
oWorkSheet.Cells(1,8).value="Category"
oWorkSheet.Cells(1,9).value="Due date"
oWorkSheet.Cells(1,10).value="Owner to resolve issue"
oWorkSheet.Cells(1,11).value="Owner/PME Verify when closed"
oExcel.Worksheets(1).Range("A1:K1").Font.Bold = True
oExcel.columns("A:A").ColumnWidth=15.00
oExcel.columns("B:B").ColumnWidth=8.00
oExcel.columns("C:C").ColumnWidth=15.00
oExcel.columns("D:D").ColumnWidth=10.00
oExcel.columns("E:E").ColumnWidth=8.00
oExcel.columns("F:F").ColumnWidth=8.00
oExcel.columns("G:G").ColumnWidth=30.00
oExcel.columns("H:H").ColumnWidth=30.00
oExcel.columns("I:I").ColumnWidth=15.00
oExcel.columns("J:J").ColumnWidth=15.00
oExcel.columns("K:K").ColumnWidth=30.00
row% = 1
offset% = 0
lastOffset% = 0
If doccoll.count >1 Then 'if more than one doc selected then confirm
resp = Messagebox("Do you want to export only the " & _
"selected " & doccoll.count & " documents?", 36, "Selected only?" )
Else
Messagebox "Exporting all rows. (To export only selected " & _
"rows tick those required in the left margin first.)"
End If '6= yes
oExcel.visible=True
If resp=6 Then 'selected documents
Set doc = doccoll.GetFirstDocument
While Not doc Is Nothing
If resp=6 Then
row% = row%+2
col% = 0 'Reset the Columns
Set otherdoc = view.getnextdocument(doc)
If otherdoc Is Nothing Then
Set otherdoc = view.getprevdocument(doc)
If otherdoc Is Nothing Then
Print " >1 doc should be selected"
End
Else
Set otherdoc = view.getnextdocument(otherdoc)
End If
Else 'got next doc
Set otherdoc = view.getprevdocument(otherdoc)
End If
End If
Forall colval In otherdoc.ColumnValues
col% = col% + 1
If Isarray(colval) Then
columnVal=Fulltrim(colval)
For y = 0 To Ubound(columnVal)
offset% = row% + y +lastOffset%
oWorkSheet.Cells(offset%,col%).value = columnVal(y)
Next
Else
oWorkSheet.Cells(row%, col%).value = colval
End If
End Forall
Set doc = doccoll.GetNextDocument(doc)
Wend
Else 'all documents
Set otherdoc =view.GetFirstDocument
While Not otherdoc Is Nothing
row% = row% + 2
col% = 0 'Reset the Columns
'Loop through all the column entries
'Forall colval In entry.ColumnValues
Forall colval In otherdoc.ColumnValues
col% = col% + 1
If Isarray(colval) Then
columnVal=Fulltrim(colval)
For y = 0 To Ubound(columnVal)
offset% = row% + y +lastOffset%
oWorkSheet.Cells(offset%,col%).value = columnVal(y)
Next
Else
oWorkSheet.Cells(row%, col%).value = colval
End If
End Forall
row%=offset%
Set otherdoc=view.GetNextDocument(otherdoc)
Wend
End If
'errhandler:
Call oExcel.quit()
Set oWorkSheet= Nothing
Set oWorkbook = Nothing
Set oExcel = Nothing
Print "Done"
End Sub

I see you're using Excel automation. Excel automation is cumbersome at times.
I'd try NPOI for Excel XLS files. Take a look at it. Really straightforward to work with:
Create Excel (.XLS and .XLSX) file from C#

There is something very wrong with the code you have uploaded. You must have removed or added an If loop because the first If loop closes before you close the While loop it contains. That being said, this should work, although I haven't tested it.
Option Public
Option Declare
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim doccoll As NotesDocumentCollection
Dim view As NotesView
Dim doc As NotesDocument
Dim resp As Integer, row As Integer, offset As Integer, nextrow As Integer, col As Integer
Dim oExcel As Variant
Dim oWorkbook As Variant
Dim oWorkSheet As Variant
On Error GoTo olecleanup
Set db = session.CurrentDatabase
Set view = db.GetView("CRMOpenIssue")
Set doccoll=db.UnprocessedDocuments
Set oExcel = CreateObject ( "Excel.Application" )
Set oWorkbook = oExcel.Workbooks.Add
Set oWorkSheet = oWorkbook.Sheets ( 1 )
oWorkSheet.Cells(1,1).value="Quote# "
oWorkSheet.Cells(1,2).value="Quote Line#"
oWorkSheet.Cells(1,3).value="Customer - fab"
oWorkSheet.Cells(1,4).value="OppNum"
oWorkSheet.Cells(1,5).value="OppLine#"
oWorkSheet.Cells(1,6).value="Open Issue#"
oWorkSheet.Cells(1,7).value="Open Issue"
oWorkSheet.Cells(1,8).value="Category"
oWorkSheet.Cells(1,9).value="Due date"
oWorkSheet.Cells(1,10).value="Owner to resolve issue"
oWorkSheet.Cells(1,11).value="Owner/PME Verify when closed"
oExcel.Worksheets(1).Range("A1:K1").Font.Bold = True
oExcel.columns("A:A").ColumnWidth=15.00
oExcel.columns("B:B").ColumnWidth=8.00
oExcel.columns("C:C").ColumnWidth=15.00
oExcel.columns("D:D").ColumnWidth=10.00
oExcel.columns("E:E").ColumnWidth=8.00
oExcel.columns("F:F").ColumnWidth=8.00
oExcel.columns("G:G").ColumnWidth=30.00
oExcel.columns("H:H").ColumnWidth=30.00
oExcel.columns("I:I").ColumnWidth=15.00
oExcel.columns("J:J").ColumnWidth=15.00
oExcel.columns("K:K").ColumnWidth=30.00
offset% = 0
nextrow% = 3
If doccoll.count >1 Then 'if more than one doc selected then confirm
resp = MessageBox("Do you want to export only the " & _
"selected " & doccoll.count & " documents?", 36, "Selected only?" )
Else
MessageBox "Exporting all rows. (To export only selected " & _
"rows tick those required in the left margin first.)"
End If '6= yes
oExcel.visible=True
If resp=6 Then 'selected documents
Set doc = doccoll.GetFirstDocument
If doccoll.count = 1 Then
Print " >1 doc should be selected"
End If
Else
Set doc =view.GetFirstDocument
End if
While Not doc Is Nothing
row% = nextrow%
col% = 0 'Reset the Columns
nextrow% = row% + 1
ForAll colval In doc.ColumnValues
col% = col% + 1
If IsArray(colval) Then
offset% = row%
ForAll cv In colval
If CStr(cv) <> "" Then
oWorkSheet.Cells(offset%,col%).value = cv
offset% = offset% + 1
End If
End ForAll
If nextrow% < offset% Then nextrow% = offset%
Else
oWorkSheet.Cells(row%, col%).value = colval
End If
End ForAll
If resp=6 Then 'selected documents
Set doc = doccoll.Getnextdocument(doc)
Else
Set doc =view.Getnextdocument(doc)
End If
Wend
oExcel.activeworkbook.close
oExcel.quit
Set oExcel = Nothing
Finish :
Print "Done"
Exit Sub
olecleanup :
' Call LogError() 'Enable to use OpenLog
If Not(IsEmpty(oExcel)) Then
oExcel.activeworkbook.close
oExcel.quit
Set oExcel = Nothing
End If
Resume Finish
End Sub

Uh, this code definitely needs to be more readable, I bet there's a simpler way to do what you want.
OK, can you explain what do you use "CRMOpenIssue" view for?
I suggest you forget about number of lines each document is represented by in your view and use document fields as your data source, instead of data displayed directly in the view columns.

Related

LibreOffice Writer API - Cursors and text selection / replacement from VB6

I have been attempting to replace Office OLE in a vb6 application with LibreOffice.
I have had some success, however, I am falling short trying to search for text, then create a cursor based on the text that was found, then insert an image at that cursors point in the document.
I have been able to piece together working code that will allow me to search for text, replace text and insert an image, however, I cannot seem to figure out how to create a cursor that will allow me to insert an image at the pace where the text is that I have found . In the provided example, the [PICTUREPLACEHOLDER] text in the document.
Has anyone ever done this before and do they have any suggestions how I can create a cursor that will allow me to specify where the image will be inserted.
I have included the code for the VB6 test app so you can see the source code to see how its currently working.
Any suggestions would be very much appreciated.
Please Note - this is experimental code - very rough and ready - not final code by a long shot - just trying to figure out how this works with LibreOffice Writer.
To run this, you will need to create an empty vb6 app with a button.
You also need LibreOffice installed.
Many thanks
Rod.
Sub firstOOoProc()
Dim oSM 'Root object for accessing OpenOffice from VB
Dim oDesk, oDoc As Object 'First objects from the API
Dim arg() 'Ignore it for the moment !
'Instanciate OOo : this line is mandatory with VB for OOo API
Set oSM = CreateObject("com.sun.star.ServiceManager")
'Create the first and most important service
Set oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
Dim oProvider As Object
Set oProvider = oSM.createInstance("com.sun.star.graphic.GraphicProvider")
'Open an existing doc (pay attention to the syntax for first argument)
Set oDoc = oDesk.loadComponentFromURL("file:///c:/dev/ooo/testfile.doc", "_blank", 0, arg())
' now - replace some text in the document
Dim Txt
Txt = oDoc.GetText
Dim TextCursor
TextCursor = Txt.CreateTextCursor
' attempt to replace some text
Dim SearchDescriptor
Dim Replace
Replace = oDoc.createReplaceDescriptor
Replace.SearchString = "[TESTDATA1]"
Replace.ReplaceString = "THIS IS A TEST"
oDoc.replaceAll Replace
Dim searchCrtiteria
SearchDescriptor = oDoc.createReplaceDescriptor
' Now - attempt try to replace some text with an image
SearchDescriptor.setSearchString ("[PICTUREPLACEHOLDER]")
SearchDescriptor.SearchRegularExpression = False
Dim Found
Found = oDoc.findFirst(SearchDescriptor)
' create cursor to know where to insert the image
Dim oCurs As Object
Set thing = oDoc.GetCurrentController
Set oCurs = thing.GetViewCursor
' make hte call to insert an image from a file into the document
InsertImage oDoc, oCurs, "file:///c:/dev/ooo/imagefilename.jpg", oProvider
'Save the doc
Call oDoc.storeToURL("file:///c:/dev/ooo/test2.sxw", arg())
'Close the doc
oDoc.Close (True)
Set oDoc = Nothing
oDesk.Terminate
Set oDesk = Nothing
Set oSM = Nothing
End Sub
Function createStruct(strTypeName)
Set classSize = objCoreReflection.forName(strTypeName)
Dim aStruct
classSize.CreateObject aStruct
Set createStruct = aStruct
End Function
Sub InsertImage(ByRef oDoc As Object, ByRef oCurs As Object, sURL As String, ByRef oProvider As Object)
' Init variables and instance object
Dim oShape As Object
Dim oGraph As Object
Set oShape = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Set oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
'Set oProvider = serviceManager.CreateInstance("com.sun.star.graphic.GraphicProvider")
' Add shape to document
oDoc.getDrawPage.Add oShape
' Set property path of picture
Dim oProps(0) As Object
Set oProps(0) = MakePropertyValue("URL", sURL)
' Get size from picture to load
Dim oSize100thMM
Dim lHeight As Long
Dim lWidth As Long
Set oSize100thMM = RecommendGraphSize(oProvider.queryGraphicDescriptor(oProps))
If Not oSize100thMM Is Nothing Then
lHeight = oSize100thMM.Height
lWidth = oSize100thMM.Width
End If
' Set size and path property to shape
oShape.graphic = oProvider.queryGraphic(oProps)
' Copy shape in graphic object and set anchor type
oGraph.graphic = oShape.graphic
oGraph.AnchorType = 1 'com.sun.star.Text.TextContentAnchorType.AS_CHARACTER
' Remove shape and resize graphix
Dim oText As Object
Set oText = oCurs.GetText
oText.insertTextContent oCurs, oGraph, False
oDoc.getDrawPage.Remove oShape
If lHeight > 0 And lWidth > 0 Then
Dim oSize
oSize = oGraph.Size
oSize.Height = lHeight * 500
oSize.Width = lWidth * 500
oGraph.Size = oSize
End If
End Sub
'
'Converts a Ms Windows local pathname in URL (RFC 1738)
'Todo : UNC pathnames, more character conversions
'
Public Function ConvertToUrl(strFile) As String
strFile = Replace(strFile, "\", "/")
strFile = Replace(strFile, ":", "|")
strFile = Replace(strFile, " ", "%20")
strFile = "file:///" + strFile
ConvertToUrl = strFile
End Function
'
'Creates a sequence of com.sun.star.beans.PropertyValue s
'
Public Function MakePropertyValue(cName, uValue) As Object
Dim oStruct, oServiceManager As Object
Set oServiceManager = CreateObject("com.sun.star.ServiceManager")
Set oStruct = oServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
oStruct.Name = cName
oStruct.Value = uValue
Set MakePropertyValue = oStruct
End Function
'
'A simple shortcut to create a service
'
Public Function CreateUnoService(strServiceName) As Object
Dim oServiceManager As Object
Set oServiceManager = CreateObject("com.sun.star.ServiceManager")
Set CreateUnoService = oServiceManager.createInstance(strServiceName)
End Function
Public Function RecommendGraphSize(oGraph)
Dim oSize
Dim lMaxW As Double
Dim lMaxH As Double
lMaxW = 6.75 * 2540
lMaxH = 9.5 & 2540
If IsNull(oGraph) Or IsEmpty(oGraph) Then
Exit Function
End If
oSize = oGraph.Size100thMM
If oSize.Height = 0 Or oSize.Width = 0 Then
oSize.Height = oGraph.SizePixel.Height * 2540# * Screen.TwipsPerPixelY() '/ 1440
oSize.Width = oGraph.SizePixel.Width * 2540# * Screen.TwipsPerPixelX() '/ 1440
End If
If oSize.Height = 0 Or oSize.Width = 0 Then
Exit Function
End If
If oSize.Width > lMaxW Then
oSize.Height = oSizeHeight * lMax / oSize.Width
oSize.Width = lMaxW
End If
If oSize.Height > lMaxH Then
oSize.Width = oSize.Width * lMaxH / oSize.Height
oSize.Height = lMaxH
End If
RecommendGraphSize = oSize
End Function
Private Sub Command1_Click()
firstOOoProc
End Sub
The content of the testFile.Doc file is as shown below:
This is a test File
[TESTDATA1]
[PICTUREPLACEHOLDER]
It looks like you need to move the view cursor to the found location.
Found = oDoc.findFirst(SearchDescriptor)
oVC = oDoc.getCurrentController().getViewCursor()
oVC.gotoRange(Found, False)
oVC.setString("")

VBA Macros in CorelDraw. Export current selection

Everyone! 
I'm working on macros which should select cdrBitmapShape and save it as a separate file.
I've already found out how to search and select such an object, but I've run into a problem of saving it.
I don't get how should I save the chosen image, it is quite unclear from the docs.
As I understand from here  I should somehow assign to the Document variable the current selection Item and export it.
Here is the test file
How can I do that?
Sub Findall_bit_map()
' Recorded 03.02.2020
'frmFileConverter.Start
'Dim d As Document
Dim retval As Long
Dim opt As New StructExportOptions
opt.AntiAliasingType = cdrNormalAntiAliasing
opt.ImageType = cdrRGBColorImage
opt.ResolutionX = 600
opt.ResolutionY = 600
Dim pal As New StructPaletteOptions
pal.PaletteType = cdrPaletteOptimized
pal.NumColors = 16
pal.DitherType = cdrDitherNone
Dim Filter As ExportFilter
Set OrigSelection = ActivePage.ActiveLayer.Shapes.All
For Each shpCheck In OrigSelection
re = shpCheck.Type
If shpCheck.Type = cdrBitmapShape Then
retval = MsgBox("BITMAP", vbOKCancel, "Easy Message")
shpCheck.AddToSelection
Set Filter = Document.ExportBitmap("D:\some.jpg", cdrJPEG)
If Filter.ShowDialog() Then
Filter.Finish
Else
MsgBox "Export canceled"
End If
End If
Next shpCheck
retval = MsgBox("Click OK if you agree.", vbOKCancel, "Easy Message")
'ActivePage.Shapes.FindShapes(Query:="#type='BitmapShape'")
If retval = vbOK Then
MsgBox "You clicked OK.", vbOK, "Affirmative"
End If
End Sub
I don't know were was the bug, but here is the working version.
Sub Findall_bit_map_snip()
Dim retval As Long
Dim doc As Document
Dim pal As New StructPaletteOptions
pal.PaletteType = cdrPaletteOptimized
pal.ColorSensitive = True
pal.NumColors = 300000000
pal.DitherType = cdrDitherNone
Dim Filter As ExportFilter
Set OrigSelection = ActivePage.ActiveLayer.Shapes.All
For Each shpCheck In OrigSelection
Set doc = ActiveDocument
doc.ClearSelection
re = shpCheck.Type
If shpCheck.Type = cdrBitmapShape Then
retval = MsgBox("BITMAP", vbOKCancel, "Easy Message")
shpCheck.AddToSelection
Set Filter = doc.ExportBitmap("D:\some.jpg", cdrJPEG, cdrSelection, , , , 600, 600, cdrNoAntiAliasing, , False, , , , pal)
Filter.Finish
End If
Next shpCheck
End Sub

Charts/graphs in Access VBA

How to initialize a chart with data in MS Access VBA? Eventually I found a suggestion here which with a bit of modification kept intellisense happy and resulted in this code:
Dim objchart As Chart
Dim arrData(3, 1) As Double
arrData(0, 0) = 1
arrData(1, 0) = 1
arrData(2, 0) = 1
arrData(3, 0) = 1
arrData(0, 1) = 1
arrData(1, 1) = 1
arrData(2, 1) = 1
arrData(3, 1) = 1
Set objchart = Me.Chart1
With objchart
.ChartTitle = "test"
.ChartType = acChartLine
.ChartValues = arrData
End With
But this throws
Compile error: Type mismatch
on the line
.ChartValues = arrData
I have tried it as "row-first" (arrData(1, 3)) and also just passing in a single array (arrData(3)). These both result in the same type mismatch error. While the intellisense is telling me that Chart1 exists, and that .ChartValues is a valid field, it doesn't tell me what kind of object it is expecting. I have googled extensively on this and come up empty. The best references I could find for Access VBA were Building VBA Apps and this but neither go into detail on Charts or ChartObjects.
Obviously I'd like to get past this type mismatch error. Even better would be if someone can give me some general advice on how to go about this when the closest thing to a language reference is silent on the part of the language you need.
This is one way to do it. First, create a new table and add some data:
Private Sub Form_Load()
Dim db As DAO.Database
Dim rec As Recordset
Dim tbl As DAO.TableDef
Set db = CurrentDb
Set tbl = db.CreateTableDef("tbl")
With tbl
.Fields.Append .CreateField("first", dbInteger)
.Fields.Append .CreateField("second", dbInteger)
End With
db.TableDefs.Append tbl
db.TableDefs.Refresh
Set rec = db.OpenRecordset("tbl")
rec.AddNew
rec("first").Value = 0
rec("second").Value = 2
rec.Update
rec.AddNew
rec("first").Value = 1
rec("second").Value = 2
rec.Update
rec.AddNew
rec("first").Value = 2
rec("second").Value = 2
rec.Update
rec.AddNew
rec("first").Value = 3
rec("second").Value = 2
rec.Update
Set rec = Nothing
Set db = Nothing
End Sub
Second, graph that data by referencing the new table:
Private Sub command0_click()
Dim objchart As Chart
Set objchart = Me.Chart1
With objchart
.ChartTitle = "tbl: second ~ first"
.RowSource = "tbl"
.ChartAxis = "first"
.ChartValues = "second"
End With
End Sub

How to skip or ignore find tables MS Word?

I've a macro code (created by Davy C) to find paragraph styles and add comment for each one if found. I need to improve this code. I want to run this macro code only paragraphs and need to skip/ignore tables when found. How do I do this?
Sub CheckKeepWithNext01()
Const message As String = "Check Keep With Next"
Const styleMask As String = "Bold + KWN"
Dim paragraphCount As Integer
Dim i As Integer
Dim currentStyle As String
Dim doc As Document
Set doc = ActiveDocument
paragraphCount = doc.Paragraphs.count
Do While i < paragraphCount
i = i + 1
If doc.Paragraphs(i).Range.Bold = True Then
If doc.Paragraphs(i).KeepWithNext = False Then
currentStyle = doc.Paragraphs(i).Range.Style
If Left(currentStyle, Len(styleMask)) <> styleMask Then
doc.Paragraphs(i).Range.Select
Selection.Comments.Add Range:=Selection.Range
Selection.TypeText Text:=message
End If
End If
End If
Loop
Set doc = Nothing
End Sub
See below screenshot for more clarity:
I've got the answer!
If doc.Paragraphs(i).Range.Tables.count = 0 Then

VBSCRIPT to read multi files into an array then write them to a single file

As the title suggests, I have three separate text files that I want to join together in a certain order (i.e., append file1, file2, file3 (in order) to make file4).
From what I've read, to do this with VBScript would require the FileSystemObject to read the files into an array then write the contents to the new file (I am open to whatever works with VBScript if suggested)
I'm having the following issues with my code:
1) The script runs, but produces no data
2) After I get it to run, it is imperative that the files append to the output file in the order of the array in the order (per line) I suggest above.
Here is the Array example I'm working with :
CODE
Const ForReading = 1
Dim arrServiceList(2)
arrServiceList(0) = strText1
arrServiceList(1) = strText2
arrServiceList(2) = strText3
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("output.txt")
Set objTextFile1 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample1.txt", ForReading)
Set objTextFile2 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample2.txt", ForReading)
Set objTextFile3 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample3.txt", ForReading)
strText1 = objTextFile1.ReadAll
objTextFile1.Close
strText2 = objTextFile2.ReadAll
objTextFile2.Close
strText3 = objTextFile3.ReadAll
objTextFile3.Close
objOutputFile.WriteLine arrServiceList(0)
objOutputFile.Close
====================
UPDATE TO MY CODE 5-15-15 (Description of corrections in below post)
CODE
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("output.txt")
Set objTextFile1 = objFSO.OpenTextFile("C:\Users\Brill\Desktop\Grab1.txt", ForReading)
Set objTextFile2 = objFSO.OpenTextFile("C:\Users\Brill\Desktop\Grab2.txt", ForReading)
Set objTextFile3 = objFSO.OpenTextFile("C:\Users\Brill\Desktop\Grab3.txt", ForReading)
Do While objTextFile1.AtEndOfStream <> True
Do While objTextFile2.AtEndOfStream <> True
Do While objTextFile3.AtEndOfStream <> True
strText1 = objTextFile1.ReadLine
objOutputFile.Write strText1 & vbTab
strText2 = objTextFile2.ReadLine
objOutputFile.Write strText2 & vbTab
strText3 = objTextFile3.ReadLine
objOutputFile.Write strText3 & vbTab & vbCrLf
Loop
Loop
Loop
objOutputFile.Close
objTextFile1.Close
objTextFile2.Close
objTextFile3.Close
The below works.
Problems with your script. 1. You were assigning the variables to the array before you had populated them. 2. You were not writing all the elements of the array.
Const ForReading = 1
Dim arrServiceList(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("output.txt")
Set objTextFile1 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample1.txt", ForReading)
Set objTextFile2 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample2.txt", ForReading)
Set objTextFile3 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample3.txt", ForReading)
strText1 = objTextFile1.ReadAll
objTextFile1.Close
strText2 = objTextFile2.ReadAll
objTextFile2.Close
strText3 = objTextFile3.ReadAll
objTextFile3.Close
arrServiceList(0) = strText1
arrServiceList(1) = strText2
arrServiceList(2) = strText3
objOutputFile.WriteLine arrServiceList(0)
objOutputFile.WriteLine arrServiceList(1)
objOutputFile.WriteLine arrServiceList(2)
objOutputFile.Close
Merging/Zipping more then one collection (e.g. some 'column files') into one collection (e.g. a 'table file') is a standard problem with a standard solution strategy (which doesn't involve reading "the files into an array" at all).
This demo code:
Option Explicit
Dim goFS : Set goFS = CreateObject("FileSystemObject")
Dim oFZip : Set oFZip = New cFZip
oFZip.m_aIFSpecs = Split("..\data\a.txt ..\data\b.txt ..\data\c.txt")
oFZip.zip "..\data\abc.txt"
WScript.Echo goFS.OpenTextFile("..\data\abc.txt").ReadAll()
Class cFZip
Public m_aIFSpecs ' array of input files
Function zip(sOFSpec)
Dim tsOut : Set tsOut = goFS.CreateTextFile(sOFSpec)
Dim nUBFiles : nUBFiles = UBound(m_aIFSpecs)
ReDim aFiles(nUBFiles)
Dim f
For f = 0 To nUBFiles
Set aFiles(f) = goFS.OpenTextFile(m_aIFSpecs(f))
Next
Dim bDone
Do
Redim aData(UBound(m_aIFSpecs))
bDone = True
For f = 0 To nUBFiles
If Not aFiles(f).AtEndOfStream Then
bDone = False
aData(f) = aFiles(f).ReadLine()
End If
Next
If Not bDone Then tsOut.WriteLine Join(aData, ",")
Loop Until bDone
For f = 0 To nUBFiles
aFiles(f).Close
Next
tsOut.Close
End Function
End Class
output:
1,10,100
2,20,200
3,30,300
4,,400
,,500
shows the basic approach. I use a Class to make experiments/specific adaptions (e.g. delimiter, quoting, ...) easier.