【问题标题】:Changing the image size更改图像尺寸
【发布时间】:2020-07-05 16:03:46
【问题描述】:

我有这个代码:

Selection.InlineShapes.AddPicture FileName:=path & "\" & "image.png", LinkToFile:=False, SaveWithDocument:=True
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

Selection.InlineShapes.Item(1).ScaleHeight = 80
Selection.InlineShapes.Item(1).ScaleWidth = 80

但出现5941错误信息:

运行时错误“5941”请求的集合成员不存在。

我想设置特定的高度和宽度。

我该如何解决?

【问题讨论】:

  • 查看我对您问题的回答并尝试重写代码! :)

标签: vba ms-word


【解决方案1】:

哦(更新的答案)......那么请试试这个,(关于图像在文本后面的评论令人困惑......)。现在的问题似乎是当你改变段落格式您实际上取消选择图像。这可以通过在添加图像之前更改段落对齐方式来解决。可以这样做:

Sub ap()
    Dim imgPath As String
    imgPath = imgPath & "image.png"

    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

    Dim myIlsh As InlineShape
    Set myIlsh = Selection.InlineShapes.AddPicture(FileName:=imgPath, LinkToFile:=False, SaveWithDocument:=True)

    myIlsh.ScaleHeight = 80
    myIlsh.ScaleWidth = 80

    Set myIlsh = Nothing
End Sub

如果您确实有没有与文本内联的图像,您应该能够使用以下方法修复它们:

Sub resizeImage()
    Dim iLoop As Long
    For iLoop = 1 To ActiveDocument.Shapes.Count
        ActiveDocument.Shapes(iLoop).Select
        If MsgBox("resize shape & convert to inline?", vbYesNo) = vbYes Then

            If ActiveDocument.Shapes(iLoop).WrapFormat.Type <> wdWrapInline Then
                ActiveDocument.Shapes(iLoop).ConvertToInlineShape
            End If
            ActiveDocument.Shapes(iLoop).ScaleHeight 0.8, msoTrue
            ActiveDocument.Shapes(iLoop).ScaleWidth 0.8, msoTrue

        End If
    Next iLoop
End Sub

【讨论】:

  • 出现此错误:出现运行时错误“-2147024809 (80070057)”: ProposeOriginal 参数仅适用于图片或 OLE 对象。
  • 在你贴的代码中,第6行的代码ActiveDocument.Shapes (iLoop) .ScaleHeight 0.8, msoTrue执行时出现错误。
  • 我认为这是一种不同类型的图像(可能是链接的) - 你知道它是如何添加到文档中的吗?
  • 使用 VBA 脚本
  • @IratzarCarrassonBores - 更新答案;希望有帮助
【解决方案2】:

您可能想查看This。 以下是该网站的评论:

基础知识:

使用相关形状容器的 .ScaleHeight 和 .ScaleWidth 属性调整图片的大小。此属性确定相对于原始图片大小的百分比大小以缩放图像。

例子:

以下代码将图片高度调整为原始图片高度的90%:

InlineShapes.Item(1).ScaleHeight = 90

或者,您可以在创建图像时更改大小:

ActiveDocument.Shapes.AddPicture FileName:=path & "\" & "image.png", _
LinkToFile:=False, _
SaveWithDocument:=True, _
Left:=-0, _
Top:=0, _
Anchor:=Selection.Range, _
Width:=50, _
Height:=50

【讨论】:

  • 没错,但文字写在图片后面。我想分配“符合文本”格式。我该怎么做?
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 2011-09-26
  • 2021-09-28
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多