judgejustin
Board Regular
- Joined
- Mar 3, 2014
- Messages
- 143
So this may be way beyond what's possible, but, I want to be able to run a macro that does these things, Makes a Workbook sharable from OneDrive that is also editable to the user and create a QR Code and paste it into the workbook. The reason for the pasting is because I will be printing it out as a label and I need users to be able to scan the QR code and edit some of the data. I will be linking this to a button in the workbook as well. Below is what I last worked on. I am no professional by any means so there may be stuff in here that is not necessary and there are a lot of notes in it so that I can remeber what I was doing. Feel free to edit any of it or start from scratch if this is useless. Also, this was actually programmed to get the QR from an online generator and not to pull it from OneDrive so at a minimum that would need to be changed.
Also, I am perfectly open to suggestions of how to do this better if anyone has a suggestion. If it would help I can also explain more in depth what this will be doing and how it will be used.
Thanks in advance.
Also, I am perfectly open to suggestions of how to do this better if anyone has a suggestion. If it would help I can also explain more in depth what this will be doing and how it will be used.
Thanks in advance.
Code:
Sub MakeSharableAndGenerateQRCode()
Dim ws As Worksheet
Dim qrURL As String
Dim sharableLink As String
Dim http As Object
Dim qrImage As Object
Dim imageURL As String
Dim imagePath As String
Dim cellForQRCode As Range
' Set worksheet and define cell for QR Code placement
Set ws = ThisWorkbook.Sheets(1) ' Modify if not the first sheet
Set cellForQRCode = ws.Range("A10") ' Adjust cell for QR placement
' Generate sharable link (This assumes the workbook is saved on a cloud/network location)
If ThisWorkbook.Path = "" Then
MsgBox "Please save the workbook to a network or cloud location first.", vbExclamation
Exit Sub
End If
sharableLink = ThisWorkbook.FullName ' You can customize this to point to an actual sharable link
' Use Google Chart API to generate the QR code
qrURL = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=" & sharableLink & "&choe=UTF-8"
' Download the QR code image
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", qrURL, False
http.Send
If http.Status = 200 Then
' Save the image to a temporary location
imagePath = Environ("TEMP") & "\QRCode.png"
With CreateObject("ADODB.Stream")
.Type = 1
.Open
.Write http.responseBody
.SaveToFile imagePath, 2
.Close
End With
' Insert the QR code image into the worksheet
Set qrImage = ws.Pictures.Insert(imagePath)
With qrImage
.Left = cellForQRCode.Left
.Top = cellForQRCode.Top
.Width = cellForQRCode.Width
.Height = cellForQRCode.Height
End With
MsgBox "QR Code generated and added to the sheet!", vbInformation
Else
MsgBox "Failed to generate QR code. Check your internet connection.", vbExclamation
End If
End Sub