如何将 GIF 图像嵌入 Excel 文件
How to embed a GIF image into an Excel file
通过 ActiveX 控件 Microsoft Web-browser
,我们可以在 Excel 的网络浏览器框中触发 GIF 文件的导航。为此,我定义了一个按钮并为其分配了一个 macro
,它给出了要完成导航的 GIF 图像的本地地址(或 link)。
问题是,为了使用这样的 excel 文件进行演示,您必须在要启动的任何计算机上也携带 GIF 文件。而当我们将图片插入Excel文件时,它会被嵌入其中,不需要携带真实的图像文件,例如PNG格式,Excel能够识别什么显示。
有谁知道 Excel 如何对 GIF 图像表现相同?
复制自http://www.vbaexpress.com/forum/showthread.php?55713-Store-image-in-VBA
如果您不想要工作表上的数据,您可能希望将其移动到 vba 并编写必要的转换代码。
如果代码适合你,你可以在上面提到的网站上留下代码的作者 "thank you"!
dim pic(1000) as string
pic(1)="47 49 46 38 39 61 F0 00 F0 00 F7 86 00 00 00 ... CD 1B 53"
测试:
;-)
Option Explicit
Sub Test()
Dim Filename As String
' Save picture to the worksheet Hex Byte Data.
Filename = "c:\temp\smiley.gif"
Call SaveAsHexFile(Filename)
' Restore the file to the user's Temp directory.
Filename = RestoreHexFile
Debug.Print Filename
' Filename now is the complete file path to the restored file.
' Pass this to another macro or application.
End Sub
Private Sub SaveAsHexFile(ByVal Filename As String)
Dim c As Long
Dim DataByte As Byte
Dim Data() As Variant
Dim i As Long
Dim n As Integer
Dim r As Long
Dim Wks As Worksheet
Dim x As String
If Dir(Filename) = "" Then
MsgBox "The File '" & Filename & "' Not Found."
Exit Sub
End If
On Error Resume Next
Set Wks = Worksheets("Hex Byte Data")
If Err = 9 Then
Worksheets.Add After:=Worksheets.Count
Set Wks = ActiveSheet
Wks.Name = "Hex Byte Data"
End If
On Error GoTo 0
Wks.Cells.ClearContents
Wks.Cells(1, "AH").Value = Dir(Filename)
n = FreeFile
Application.ScreenUpdating = False
Application.ErrorCheckingOptions.NumberAsText = False
With Wks.Columns("A:AF")
.NumberFormat = "@"
.Cells.HorizontalAlignment = xlCenter
Open Filename For Binary Access Read As #n
ReDim Data((LOF(n) - 1) \ 32, 31)
For i = 0 To LOF(n) - 1
Get #n, , DataByte
c = i Mod 32
r = i \ 32
x = Hex(DataByte)
If DataByte < 16 Then x = "0" & x
Data(r, c) = x
Next i
Close #n
Wks.Range("A1:AF1").Resize(r + 1, 32).Value = Data
.Columns("A:AF").AutoFit
End With
Application.ScreenUpdating = True
End Sub
Function RestoreHexFile() As String
Dim Cell As Range
Dim Data() As Byte
Dim File As String
Dim j As Long
Dim LSB As Variant
Dim MSB As Variant
Dim n As Integer
Dim Rng As Range
Dim Wks As Worksheet
On Error Resume Next
Set Wks = Worksheets("Hex Byte Data")
If Err <> 0 Then
MsgBox "The Worksheet 'Hex Byte Data' is Missing.", vbCritical
Exit Function
End If
On Error GoTo 0
Set Rng = Wks.Range("A1").CurrentRegion
File = Wks.Cells(1, "AH").Value
File = Replace(File, ".", "_NEW.")
If File <> "" Then
n = FreeFile
File = Environ("TEMP") & "\" & File
Open File For Binary Access Write As #n
ReDim Data(Application.CountA(Rng) - 1)
For Each Cell In Rng
If Cell = "" Then Exit For
MSB = Left(Cell, 1)
If IsNumeric(MSB) Then MSB = 16 * MSB Else MSB = 16 * (Asc(MSB) - 55)
LSB = Right(Cell, 1)
If Not IsNumeric(LSB) Then LSB = (Asc(LSB) - 55) Else LSB = LSB * 1
Data(j) = MSB + LSB
j = j + 1
Next Cell
Put #n, , Data
Close #n
End If
RestoreHexFile = File
End Function
通过 ActiveX 控件 Microsoft Web-browser
,我们可以在 Excel 的网络浏览器框中触发 GIF 文件的导航。为此,我定义了一个按钮并为其分配了一个 macro
,它给出了要完成导航的 GIF 图像的本地地址(或 link)。
问题是,为了使用这样的 excel 文件进行演示,您必须在要启动的任何计算机上也携带 GIF 文件。而当我们将图片插入Excel文件时,它会被嵌入其中,不需要携带真实的图像文件,例如PNG格式,Excel能够识别什么显示。
有谁知道 Excel 如何对 GIF 图像表现相同?
复制自http://www.vbaexpress.com/forum/showthread.php?55713-Store-image-in-VBA
如果您不想要工作表上的数据,您可能希望将其移动到 vba 并编写必要的转换代码。
如果代码适合你,你可以在上面提到的网站上留下代码的作者 "thank you"!
dim pic(1000) as string
pic(1)="47 49 46 38 39 61 F0 00 F0 00 F7 86 00 00 00 ... CD 1B 53"
测试:
;-)
Option Explicit
Sub Test()
Dim Filename As String
' Save picture to the worksheet Hex Byte Data.
Filename = "c:\temp\smiley.gif"
Call SaveAsHexFile(Filename)
' Restore the file to the user's Temp directory.
Filename = RestoreHexFile
Debug.Print Filename
' Filename now is the complete file path to the restored file.
' Pass this to another macro or application.
End Sub
Private Sub SaveAsHexFile(ByVal Filename As String)
Dim c As Long
Dim DataByte As Byte
Dim Data() As Variant
Dim i As Long
Dim n As Integer
Dim r As Long
Dim Wks As Worksheet
Dim x As String
If Dir(Filename) = "" Then
MsgBox "The File '" & Filename & "' Not Found."
Exit Sub
End If
On Error Resume Next
Set Wks = Worksheets("Hex Byte Data")
If Err = 9 Then
Worksheets.Add After:=Worksheets.Count
Set Wks = ActiveSheet
Wks.Name = "Hex Byte Data"
End If
On Error GoTo 0
Wks.Cells.ClearContents
Wks.Cells(1, "AH").Value = Dir(Filename)
n = FreeFile
Application.ScreenUpdating = False
Application.ErrorCheckingOptions.NumberAsText = False
With Wks.Columns("A:AF")
.NumberFormat = "@"
.Cells.HorizontalAlignment = xlCenter
Open Filename For Binary Access Read As #n
ReDim Data((LOF(n) - 1) \ 32, 31)
For i = 0 To LOF(n) - 1
Get #n, , DataByte
c = i Mod 32
r = i \ 32
x = Hex(DataByte)
If DataByte < 16 Then x = "0" & x
Data(r, c) = x
Next i
Close #n
Wks.Range("A1:AF1").Resize(r + 1, 32).Value = Data
.Columns("A:AF").AutoFit
End With
Application.ScreenUpdating = True
End Sub
Function RestoreHexFile() As String
Dim Cell As Range
Dim Data() As Byte
Dim File As String
Dim j As Long
Dim LSB As Variant
Dim MSB As Variant
Dim n As Integer
Dim Rng As Range
Dim Wks As Worksheet
On Error Resume Next
Set Wks = Worksheets("Hex Byte Data")
If Err <> 0 Then
MsgBox "The Worksheet 'Hex Byte Data' is Missing.", vbCritical
Exit Function
End If
On Error GoTo 0
Set Rng = Wks.Range("A1").CurrentRegion
File = Wks.Cells(1, "AH").Value
File = Replace(File, ".", "_NEW.")
If File <> "" Then
n = FreeFile
File = Environ("TEMP") & "\" & File
Open File For Binary Access Write As #n
ReDim Data(Application.CountA(Rng) - 1)
For Each Cell In Rng
If Cell = "" Then Exit For
MSB = Left(Cell, 1)
If IsNumeric(MSB) Then MSB = 16 * MSB Else MSB = 16 * (Asc(MSB) - 55)
LSB = Right(Cell, 1)
If Not IsNumeric(LSB) Then LSB = (Asc(LSB) - 55) Else LSB = LSB * 1
Data(j) = MSB + LSB
j = j + 1
Next Cell
Put #n, , Data
Close #n
End If
RestoreHexFile = File
End Function