【问题标题】:2010 Access - how to add and work with shapes in Excel file2010 Access - 如何在 Excel 文件中添加和使用形状
【发布时间】:2018-05-07 17:14:01
【问题描述】:

我想在 Excel 工作表中添加形状并从 2010 Access vba 修改它们。

我根据Excel‘记录宏’写的代码是:

StrSheetName = "Menu"
wkbModels.Sheets.Add(before:=wkbModels.Sheets("Models")).Name = StrSheetName
Set wksModelsMenu = wkbModels.Sheets(StrSheetName)

With wksModelsMenu
    iLeft = 1
    iTop = 1
    iWidth = 125
    iHeight = 200
    .Shapes.AddPicture fDirectory & "logo.jpg", False, True, iLeft, iTop, iWidth, iHeight
    iLeft = 240
    iTop = 1
    iWidth = 300
    iHeight = 125
    .Shapes.AddShape(msoShapeRoundedRectangle, iLeft, iTop, iWidth, iHeight).Select
    .Shapes(1).Range.ShapeStyle = msoShapeStylePreset10
    .Shapes(1).Range.TextFrame2.TextRange.Font.Bold = msoTrue
    .Shapes(1).Range.TextFrame2.VerticalAnchor = msoAnchorMiddle
    .Shapes(1).Range.TextFrame2.TextRange.Characters.Text = _
                "TEST TEST TEST"
    .Shapes(1).Range.TextFrame2.TextRange.Characters.ParagraphFormat.Alignment = msoAlignCenter
    .Shapes(1).Range.TextFrame2.TextRange.Characters.Font.Bold = msoTrue
End With

在“.Shapes(1).Range.ShapeStyle...”和“.Shapes(1)”之后。我收到错误“对象不支持此属性或方法”。删除“(1)”会产生编译错误。

mso 库已安装,并且引用的项目(即 msoShapeStylePreset10)具有正确的值。

对我需要做什么有什么想法吗?

【问题讨论】:

    标签: excel ms-access vba


    【解决方案1】:

    Shapes(1) 返回一个Shape 对象,该对象没有Range 成员,因此Shapes(1).Range 将产生错误。从每一行删除Range 可能会解决您的问题,例如Shapes(1).ShapeStyle = ...

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      Worksheet.Shapes.AddShape() 返回一个Shape 对象。

      最好使用这个对象,而不是依赖创建的形状是工作表上的第二个形状(在某些时候可能会改变)。

      此代码(不含 .Range)在 Access 2010 中适用于我:

      Dim shp As Excel.Shape
      
      ' Note: there is no reason to select the shape
      Set shp = wksModelsMenu.Shapes.AddShape(msoShapeRoundedRectangle, iLeft, iTop, iWidth, iHeight)
      ' Use the created object to set its properties
      With shp
          .ShapeStyle = msoShapeStylePreset10
          .TextFrame2.TextRange.Font.Bold = msoTrue
          .TextFrame2.VerticalAnchor = msoAnchorMiddle
          .TextFrame2.TextRange.Characters.Text = "TEST TEST TEST"
          .TextFrame2.TextRange.Characters.ParagraphFormat.Alignment = msoAlignCenter
          .TextFrame2.TextRange.Characters.Font.Bold = msoTrue
      End With
      

      你也可以不使用 Shape 变量(但我发现上面的代码可读性更好):

      With wksModelsMenu.Shapes.AddShape(msoShapeRoundedRectangle, iLeft, iTop, iWidth, iHeight)
          .ShapeStyle = msoShapeStylePreset10
          ' etc.
      End With
      

      补充说明:
      创建工作表时也可以这样做:

      Set wksModelsMenu = wkbModels.Sheets.Add(before:=wkbModels.Sheets("Models"))
      wksModelsMenu.Name = StrSheetName
      

      【讨论】:

      • 是的,硬编码的“2”可能不是新形状是正确的。为了克服这个,在添加之后,我有: i=ws.shapes.count 然后使用 i 作为索引。到目前为止,我已经能够使用它添加许多形状,并且运行良好。
      • 好的。但上面确实是解决新创建对象的最佳方法。 Set myBar = foo.CreateBar() 经常适用于 Office 对象模型。 @LEBoyd
      【解决方案3】:

      因此,无论如何,我发现如果我将索引从“形状”之后移动到“范围”之后,它可以根据需要工作。

      这个:

      Shapes(1).Range.....
      

      更改为:

      Shapes.Range(1)
      

      当然,如果您查看代码,您会发现第二个形状是我真正想要处理的。

      Shapes(2).Range
      

      没用。

      Shapes.Range(2)
      

      完美运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-27
        • 2012-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-17
        相关资源
        最近更新 更多