在 Excel VBA (2019) 中,我正在嘗試寫出 .bri 型別的過渡交易檔案格式 .bri 的規范需要重復(取決于總交易中的板數)集
- 每塊板卡的 39 個 ASCII 碼
- 10 個通常的 ASCII 空格
- 22 個進一步的 ASCII 空間
- 18 個空值位元組(所有位設定為 0)作為檔案中的一個連續行。
我嘗試過的示例代碼僅通過以下方式撰寫 1 個板:
Type BriType
boardVar As String
byteArr(17) As Byte
End Type
Sub TestBriWrite()
Dim i, j, k, l, m As Integer
Dim FileName As String, fileNo As Integer
Dim FileLoc As Variant
Dim filler As String
Dim BriVar As BriType
BriVar.boardVar = "010203040506070809101112131415161718192021222324252627282930313233343536373839 "
For m = 0 To 17
BriVar.byteArr(m) = 0
Next m
FileLoc = Application.GetSaveAsFilename(Title:="Save the Deal File as xxxxx.bri")
fileNo = FreeFile
Open FileLoc For Binary As #fileNo
Put #fileNo, 1, BriVar
Close #fileNo
End Sub
這似乎接近我想要的,但每個板條目在輸出檔案中都以“n”開頭。誰能建議我如何實作我想要做的事情?
抱歉,這是我在董事會的第一篇文章,所以我可能沒有遵守所有規則。
uj5u.com熱心網友回復:
由于BriVar.boardVar被宣告為可變長度字串,因此前兩個位元組用于跟蹤其長度以供后續讀取。
在您的示例中,您為變數分配了 110 個字符的字串。因此它使用兩個位元組將該值存盤在您的檔案中。第一個位元組的值為 110,第二個位元組的值為 0。
因此,當您在文本編輯器中打開檔案時,您會看到 ASCII 值 110 和 0 的字符表示,它們是字母“n”和空字符。
您可以通過宣告一個固定長度的字串來避免這兩個位元組。例如,假設您的字串長度始終為 110 個字符。然后你會宣告你的字串如下......
Type BriType
boardVar As String * 110 '<--- fixed length string
byteArr(17) As Byte
End Type
順便說一句,您應該修改您的代碼,以便在提示用戶保存檔案時退出子程式,然后單擊取消...。
FileLoc = Application.GetSaveAsFilename(Title:="Save the Deal File as xxxxx.bri")
If FileLoc = False Then Exit Sub '<-- user cancelled
所以這是你的宏修改如上......
Type BriType
boardVar As String * 110
byteArr(17) As Byte
End Type
Sub TestBriWrite()
Dim i, j, k, l, m As Integer
Dim FileName As String, fileNo As Integer
Dim FileLoc As Variant
Dim filler As String
Dim BriVar As BriType
FileLoc = Application.GetSaveAsFilename(Title:="Save the Deal File as xxxxx.bri")
If FileLoc = False Then Exit Sub
BriVar.boardVar = "010203040506070809101112131415161718192021222324252627282930313233343536373839 "
For m = 0 To 17
BriVar.byteArr(m) = 0
Next m
fileNo = FreeFile
Open FileLoc For Binary As #fileNo
Put #fileNo, 1, BriVar
Close #fileNo
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/484135.html
