Excel VBA功能-pictures.insert与shapes.addpicture

时间:2018-07-25 11:30:01

标签: excel vba image insert shapes

我被Excel VBA函数中的图片插入功能所困扰。 我使用Google API进行QR码生成,并将此图片插入工作表, ActiveCell的水平右对齐,垂直居中对齐。

  • 带有图片的1号变体。插入-此变体还可以

    Public Function QRX() As Integer
      Dim target As Range, data As String
      Set target = ActiveCell
      data = target.Offset(0, -1).Text
      If data <> "" Then
        With target.Worksheet.Pictures.Insert
        ("https://chart.apis.google.com/chart?chl=" & Replace(data, " ", "%20") 
        & "&chs=300x300&cht=qr&chld=h|1&choe=ISO-8859-1/chart.png")
          With .ShapeRange
            .AlternativeText = data
            .LockAspectRatio = msoTrue
            .Width = Application.CentimetersToPoints(2)
            .Height = Application.CentimetersToPoints(2)
          End With
          .Name = "QR_" & target.Address(0, 0)
          .Left = target.Left + (target.Width - .Width)
          .Top = target.Top + (target.Height - .Height) / 2
          .Placement = 1
          .PrintObject = True
        End With
      End If
      QRX = 0
    End Function
    
  • 带有shapes.addpicture的变体2-在此变体中,垂直居中对齐确定,水平向右对齐不正确-。左参数行为与picture.insert变体不同。

    Public Function QRX() As Integer
      Dim target As Range, data As String
      Set target = ActiveCell
      data = target.Offset(0, -1).Text
      If data <> "" Then
        With target.Worksheet.Shapes.AddPicture
        ("https://chart.apis.google.com/chart?chl=" & Replace(data, " ", "%20") 
        & "&chs=300x300&cht=qr&chld=h|1&choe=ISO-8859-1/chart.png", 
        False, True, 0, 0, -1, -1)
          .AlternativeText = data
          .LockAspectRatio = msoTrue
          .Width = Application.CentimetersToPoints(2)
          .Height = Application.CentimetersToPoints(2)
          .Name = "QR_" & target.Address(0, 0)
          .Left = target.Left + (target.Width - .Width)
          .Top = target.Top + (target.Height - .Height) / 2
          .Placement = 1
          .PrintObject = True
        End With
      End If
      QRX = 0
    End Function
    

    1 /有人可以建议变形2中的问题在哪里

    2 /您是否知道DataMatrix的一些在线API,以及Google QRCode的API?如果没有,我将使用Shape.addoleobject方法(类似于shapes.addpicture)使用StrokeScribe ActiveX组件,因此我需要解决变体2

非常感谢您的建议

1 个答案:

答案 0 :(得分:0)

在我看来,他们就像一堆又一堆地堆叠在一起,没有看到问题所在

enter image description here