|
楼主 |
发表于 2021-3-13 22:32
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
3.批量生成二维码并插入到指定单元格,二维码放进系统剪贴板
可以利用BarcodeWriter对象GetStdPicture方法获取StdPicture图片对象,
然后放进剪贴板,然后粘贴到Excel表的单元格中,而无需使用Pictures.Insert
从磁盘文件中重新导入到Excel。
- Rem 生成二维码并转为StdPicture对象
- Function Encode_To_QR_Code(cont As String) As IPictureDisp
- Dim Writer As IBarcodeWriter
- Dim qrCodeOptions As QrCodeEncodingOptions
- Set qrCodeOptions = New QrCodeEncodingOptions
- Set Writer = New BarcodeWriter
- Writer.Format = BarcodeFormat_QR_CODE
- Set Writer.Options = qrCodeOptions
- qrCodeOptions.Height = 200
- qrCodeOptions.Width = 200
- qrCodeOptions.Margin = 5
- qrCodeOptions.CharacterSet = "UTF-8"
- qrCodeOptions.ErrorCorrection = ErrorCorrectionLevel_H
- Set Encode_To_QR_Code = Writer.GetStdPicture(cont)
- End Function
复制代码
- #If VBA7 Then
- Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
- Declare PtrSafe Function SetClipboardData Lib "user32" (ByVal Format As Long, ByVal hMem As LongPtr) As Long
- Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
- Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
- #Else
- Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
- Declare Function SetClipboardData Lib "user32" (ByVal Format As Long, ByVal hMem As Long) As Long
- Declare Function CloseClipboard Lib "user32" () As Long
- Declare Function EmptyClipboard Lib "user32" () As Long
- #End If
- Const CF_BITMAP = 2&
- Rem 把图片放进剪贴板
- Function SetClipboard(cont As String) As Boolean
- Dim hwnd As LongPtr
- On Error GoTo ErrHandler
- hwnd = 0
- If OpenClipboard(hwnd) Then
- EmptyClipboard
- SetClipboardData CF_BITMAP, Encode_To_QR_Code(cont)
- CloseClipboard
- SetClipboard = True
- Exit Function
- End If
- ErrHandler: SetClipboard = False
- End Function
复制代码
- Rem 批量生成QR二维码并插入指定单元格
- Sub MakeQRCode()
- Dim Rng As Range, Shp As Shape, r&
- Application.ScreenUpdating = False
- r = Range("a" & Rows.Count).End(3).Row
- For Each Shp In ActiveSheet.Shapes
- If Shp.Type = msoPicture Then Shp.Delete
- Next
- For Each Rng In Range("a1:a" & r)
- If SetClipboard(Rng.Value) Then
- ActiveSheet.Paste Rng.Offset(, 2)
- With Selection.ShapeRange
- .LockAspectRatio = msoTrue
- .Top = Rng.Offset(, 2).Top + 2
- .Left = Rng.Offset(, 2).Left + 2
- .Height = Rng.Offset(, 2).Height - 4
- End With
- End If
- Next
- Application.ScreenUpdating = True
- End Sub
复制代码
|
|