Well, I have never done this, but since you can use VBA you may be able to use a web service to convert the text to a URL. I have provided some sample code that shows how to access one such service (This is just the first service I came across on Google
- I do not know whether it is suitable for your needs).
Sub testGetQRCode()
' This is just an example of how you could use a
' web service to create a QR code.
' The steps are:
' convert the text to a "URLencoded" format
' build a request URL tht specifies the text and the size of the QR code
' send the request
' save the result to a file
' insert the file at the end of the document
' to use any such service for real, you obviously have to find out
' what their terms and conditions are, and you will probably need
' your users to understand that they are sending data to a 3rd-party
' This sample actually uses 3 Windows COM objects to do this, and also
' uses the Windows JScript interprter.
' You will need to make references to all these objects via
' VBA Editor->Tools->References
' But the code will only work on Windows systems that have those objects
' The references you need are as follows:
' To issue the request and retrieve the result:-
' Microsoft WinHTTP Services, version 5.1 (in winhttpcom.dll)
' To save the binary file easily:-
' Microsoft ActiveX Data Objects Library
' (I happen to be using version 6.1, which is in msado15.dll)
' To encode the string:
' Microsoft Script control (msscript.ocx)
' None of the above are available on Mac.
' There are other ways you can save to disk without using ActiveX Data Objects
' so yoou should probably avoid that if possible
' There are also other ways to do the URL Encoding and the .ocx probably
' will not work on 64-bit versions of Word
' so you should probably avoid that if possible
'
' However, I do not think the HTTP control
' or something like it is probably unavoidable.
' Finally, in the scenario you describe, you might need
' to avoid saving the image to disk. It may be possible to avoid
' that by encapsulating the returned .png in an XML format Word document
' and using Word's insertXML method to insert the image, but
' I am not sure about that
'
' To distribute code that may use different versions of referenced
' objects you normally have to change declarations such as
' "Dim request as WinHttpRequest"
' to
' "Dim request as Object"
' and you may need to replace constant names by their values
' My test!
Const QRImageFile As String = "c:\a\qrimage.png"
Const TextToEncode As String = "Cirinnà, Abbascià"
Call getQRCode(TextToEncode:=TextToEncode, FullName:=QRImageFile)
' Insert the image at the end of the document
Set r = ActiveDocument.Content
r.Collapse Direction:=Word.WdCollapseDirection.wdCollapseEnd
r.InlineShapes.AddPicture _
FileName:=QRImageFile, _
LinkToFile:=False, _
SaveWithDocument:=True
Set r = Nothing
End Sub
Function getQRCode(TextToEncode As String, FullName As String)
' This is the URL of an example text-to-QR code web service
Const QRServerUrlStem As String = "https://api.qrserver.com/v1/create-qr-code/"
Dim request As WinHttpRequest
Set request = CreateObject("WinHttp.WinHttpRequest.5.1")
With request
.SetTimeouts _
ResolveTimeout:=60000, _
ConnectTimeout:=60000, _
SendTimeout:=60000, _
ReceiveTimeout:=60000
.Open _
Method:="GET", _
URL:=QRServerUrlStem & "?data=" & encodeURL(TextToEncode) & "&size=100x100", _
Async:=False
' set a few options
.Option(WinHttpRequestOption_UserAgentString) = "Test QR code retrieval"
.Option(WinHttpRequestOption_EnableRedirects) = True
.Option(WinHttpRequestOption_EnableHttpsToHttpRedirects) = True
' issue the request
.Send
' in this example we want to save the response,
' which is basically a .png image, directly to a file.
' FileSystemObject is not a good choice for binary data
' We might be able to use the ResponseStream property of the request
' which is an Istream but I think that is a little bit hard
' to work with in VBA
' But we also get a bytearray back so we can use ADO
Call SaveBinaryData(FullName:=FullName, ByteArray:=.ResponseBody)
End With
Set request = Nothing
End Function
Sub testEncodeURL()
Debug.Print encodeURL("Cirinnà, Abbascià")
End Sub
Function encodeURL(str As String) As String
' This came from http://stackoverflow/questions/218181
' which also shows some other ways to do the encoding
Dim ScriptEngine As ScriptControl
Set ScriptEngine = New ScriptControl
ScriptEngine.Language = "JScript"
ScriptEngine.AddCode "function encode(str) {return encodeURIComponent(str);}"
Dim encoded As String
encoded = ScriptEngine.Run("encode", str)
encodeURL = encoded
End Function
Function SaveBinaryData(FullName As String, ByteArray)
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Dim BinaryStream As ADODB.Stream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = adTypeBinary
BinaryStream.Open
' Write the ata to the stream
BinaryStream.Write ByteArray
' Save the stream to disk
BinaryStream.SaveToFile FullName, adSaveCreateOverWrite
End Function