2013年4月4日 星期四

VBA - COMBINE 多個檔案成為 一個

VBA code :


Sub Append_Text_Files()


Dim oFS As FileSystemObject
Dim oFS1 As FileSystemObject

Dim oTS As TextStream
Dim oTS1 As TextStream

Dim vTemp

Set oFS = New FileSystemObject
Set oFS1 = New FileSystemObject


For i1 = 1 To 3

Set oTS = oFS.OpenTextFile(ActiveWorkbook.Path & "\" & "Doc" & i1 & ".txt", ForReading)
vTemp = oTS.ReadAll

Set oTS1 = oFS.OpenTextFile(ActiveWorkbook.Path & "\" & "Output.txt", ForAppending, True)
oTS1.Write (vTemp)
oTS1.Close
oTS.Close

Next i1

End Sub

上面的程式碼 : 將 Doc1.txt 加 Doc2.txt 加 Doc3.txt , 合成 新檔 "Output.txt"

VBA - Write & Print Different in (.TXT) FILE


VBA CODE "

Sub WriteToATextFile()
'first set a string which contains the path to the file you want to create.
'this example creates one and stores it in the root directory

 MyFile = ActiveWorkbook.Path & "\" & "whateveryouwant.txt"
'set and open file for output
fnum = FreeFile()
Open MyFile For Output As fnum
'write project info and then a blank line. Note the comma is required
Write #fnum, "I wrote this"
Print #fnum, Sheets("Sheet1").Range("a1")
'use Print when you want the string without quotation marks
Print #fnum, "I printed this OK"
Close #fnum
End Sub

OUTPUT FILE ( whateveryouwant.txt )

"I wrote this"
tooltips[0]=["xman.jpg", "G1(270),G2(198)(2nd)(paid5.0)<br /> <B>G3</B>(b75),G4(530)", {background:"#FFFFFF", color:"black", border:"5px ridge darkblue"}]
I printed this OK


Different in with / without quotation marks