因為需要,小編透過Scripting.FileSystemObject做了一個寫CSV的模組,沒有用純文字檔處理的方式:
VBA CODE:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub file_save(sFileName, SAVE_STR) | |
'sFileName 設定的寫入檔案名稱 | |
'SAVE_STR 資料來源 | |
On Error GoTo line1 | |
Set fs = CreateObject("Scripting.FileSystemObject") | |
'On Error Resume Next | |
EXIT_FILE_out = EXIT_FILE(sFileName) '檢查檔案是否存在(重複) | |
If EXIT_FILE_out = 1 Then | |
fs.DeleteFile sFileName | |
End If | |
Set A = fs.CreateTextFile(sFileName, True) | |
temp = Split(SAVE_STR, Chr(13)) ' STEP BY FILE AVOID IN BIG FILE | |
For l = LBound(temp) To UBound(temp) | |
A.WriteLine temp(l) | |
Next | |
A.Close | |
SAVE_STR = "" | |
Exit Sub | |
line1: | |
MsgBox "存檔錯誤,請使用中斷+pause 做debug" | |
End Sub | |
Function EXIT_FILE(S) | |
Set fs = CreateObject("Scripting.FileSystemObject") | |
If fs.FileExists(S) Then | |
EXIT_FILE = 1 | |
Else | |
EXIT_FILE = 0 | |
End If | |
End Function |
以上
主要是寫入資料>檢查有無重複>有刪除/無寫入新檔>DONE
為避免單一次寫入資料過大,透過迴圈逐行寫入。
後來發現資料中會帶逗號,思考兩條路。
1.不要寫CSV,因為CSV就是用逗號區分資料的 ,用其他格式然後給EXCEL讀檔這樣???
2.直寫EXCEL檔案,因為用CSV主要目的是給EXCEL用,就直接寫EXCEL八
定案方案2直接衝:
VBA CODE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub file_excel_save(file_path, str_comm, SHEET_NAME) | |
Dim sFolder As String | |
Set xlApp = CreateObject("Excel.Application") | |
Set xlBook = xlApp.Workbooks.Open(file_path) | |
xlApp.Visible = True | |
xlBook.Application.Run "autorun2", str_comm, SHEET_NAME | |
xlBook.Close (True) | |
xlApp.Quit | |
Set xlApp = Nothing | |
End Sub |
直接將整理資料方法寫在EXCEL上,然後透過OUTLOOK或WORD的 VBA去傳送資料跟乎交。
主要VBA>生成EXCEL物件>生成WORKBOOKS物件>開啟檔案>執行SUB>SUB 接收資料並處理
缺點:會占用使用時間較久,原方案是透過VBA>VBS>背景作業,無奈資料本身有逗號 XD
內容看起來沒啥,也利用了假日5小時做測試。一定要記錄一下。
沒有留言:
張貼留言