Option Explicit
Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
#Else
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
Sub Test_QRCodeToCell()
Call QRCodeToCell("Bonjour", ActiveCell, 100, 100)
End Sub
Sub Test_QRCodeToFile()
Call QRCodeToFile("Bonjour", [I6].Value, 100, 100)
End Sub
Function QRCodeToCell(Chaine As String, Cellule As Range, _
Optional PicWidth As Integer = 120, _
Optional PicHeight As Integer = 120) As Picture
Dim Link As String
Dim Pic As Picture
Dim PicName As String
Link = "http://chart.googleapis.com/chart?cht=qr&chs=" & PicWidth & "x" & PicHeight & "&chl=" & Chaine
Cellule.Parent.Activate
Cellule.Activate
PicName = "QRCode_" & Cellule.Address(0, 0)
For Each Pic In ActiveSheet.Pictures
If Pic.Name = PicName Then Pic.Delete
Next Pic
Set Pic = ActiveSheet.Pictures.Insert(Link)
Pic.Name = PicName
Set QRCodeToCell = Pic
End Function
Sub QRCodeToFile(Chaine As String, NomCompletFichier, _
Optional PicWidth As Integer = 120, _
Optional PicHeight As Integer = 120)
Dim Ch As ChartObject
Dim Pic As Picture
Dim hPicAvail
Set Pic = QRCodeToCell(Chaine, ActiveCell, PicWidth, PicHeight)
Set Ch = ActiveSheet.ChartObjects.Add(0, 0, Pic.Width, Pic.Height)
Pic.Copy
Do: DoEvents: hPicAvail = IsClipboardFormatAvailable(14): Loop While hPicAvail = 0
Ch.Activate
Ch.Chart.Paste
Do While Ch.Chart.Pictures.Count < 1: Loop
Ch.Chart.Shapes(1).Width = Ch.Width
Ch.Chart.Shapes(1).Height = Ch.Height
Ch.Chart.Export NomCompletFichier, FilterName:=Mid(NomCompletFichier, InStrRev(NomCompletFichier, ".") + 1)
Pic.Delete
Ch.Delete
End Sub