2

我想.bmp使用 VBA 修改文件的一部分,一切都很好,除了当我覆盖选择的字节时,下一个字节被设置为零。我的宏是:

Sub WriteBinaryFile()
Dim i As Integer
Dim nFileNum As Integer
Dim sFilename As String

sFilename = "C:\Users\Piotr\Desktop\test1.bmp"

' Get an available file number from the system
nFileNum = FreeFile

' Open the file in binary mode.  Locks are optional

Open sFilename For Binary Lock Read Write As #nFileNum

     ' Put the data in the file
     ' Below code should write 255 value to byte number 100
     ' but it writes also 0 value to byte number 101
     Put #nFileNum, 100, 255 

Close #nFileNum

End Sub

为什么当我修改字节数 100 时,字节数 101 的值设置为 00 ?如何改变它,为什么会发生?

编辑 Cor_Blimey 指出使用转换函数 CByte(255) 解决了问题,因为255在 VBA 中是整数,它是 16 位数字,因此将其放入文件会覆盖两个字节

4

1 回答 1

1

正如上面 Cor_Blimey 所说:

...255 是一个整数,它是 16 位,即 VBA 中的 2 个字节。尝试Put #nFileNum, 100, CByte(255)

于 2015-08-05T19:07:26.433 回答