|
本帖最后由 XuHeng925 于 2022-10-8 16:20 编辑
需求
在wps中用vba插入二维码图片,实现思路是写了一个函数,把二维码文本和尺寸等信息传给这个函数,然后函数去qrserver服务去生成二维码图片,然后把返回的二维码图片插入到指定的单元格。
问题
插入图片在一些电脑可行,在另外一些电脑失效。
过程
pictures.insert方法失效
刚开始我先是用了pictures.insert方法,这个方法在一些电脑成功了,但是在另外一些电脑报错,报错信息是:
以下是我的用pictures.insert的代码
- Sub GenQRCode(ByVal data As String, ByVal color As String, ByVal bgcolor As String, ByVal size As Integer, sht As Worksheet, rng As Range)
- On Error Resume Next
- For i = 1 To sht.Shapes.Count
- If sht.Shapes(i).Name = "QRCode" Then
- sht.Shapes(i).Delete
- Exit For
- End If
- Next i
- surl = "https://api.qrserver.com/v1/create-qr-code/?" + "size=" + Trim(Str(size)) + "x" + Trim(Str(size)) + "&color=" + color + "&bgcolor=" + bgcolor + "&data=" + data
-
- Debug.Print surl
- sht.Pictures.Insert(sURL + sParameters).Select
-
- With selection
- .Name = "QRCode"
- .Left = rng.Left
- .Top = rng.Top
- .Placement = xlMoveAndSize
- End With
- End Sub
复制代码 Shapes.AddShape.Fill方法失效
因为是WPS,所以我没有在外网搜解决办法,百度了一下发现这里有人遇到类似的问题,里面提到的解决思路是先添加形状,然后在形状中添加图片。于是我也尝试了这个思路,在一些电脑也跑成功了,但是在另外一些电脑(和上面那些不行的电脑是一拨)却只添加了形状但是图片没过来。
以下是用Shapes.AddShape.Fill的代码:
- Sub GenQRCode(ByVal data As String, ByVal color As String, ByVal bgcolor As String, ByVal size As Integer, sht As Worksheet, rng As Range)
- On Error Resume Next
- For i = 1 To sht.Shapes.Count
- If sht.Shapes(i).Name = "QRCode" Then
- sht.Shapes(i).Delete
- Exit For
- End If
- Next i
- surl = "https://api.qrserver.com/v1/create-qr-code/?" + "size=" + Trim(Str(size)) + "x" + Trim(Str(size)) + "&color=" + color + "&bgcolor=" + bgcolor + "&data=" + data
-
- Debug.Print surl
-
- sht.Shapes.AddShape(msoShapeRectangle, 40, 80, 40, 40).Fill.UserPicture (surl)
- With sht.Shapes(sht.Shapes.Count)
- .Name = "QRCode"
- .Left = rng.Left
- .Top = rng.Top
- .Placement = xlMoveAndSize
- End With
- End Sub
复制代码
|
-
|