vba - 按标题中的名称访问形状

标签 vba ms-word

我的第一篇文章,对于任何错误,深表歉意。

我正在尝试替换文档标题中的图像。这将完成数千次。

这行代码:

Set shp = ActiveDocument.Sections(1).Headers.Shapes(strPic)

原因:

Compile error: Method or data member not found

Set shp = ActiveDocument.Shapes("strPic")

原因:

Run-time error '-2147024809(80070057): The item with the specified name wasn't found.

我已经用多种方式编写,但无法让它分配 shp 我觉得这是一个非常简单的问题,关于我如何访问 header ,或者也许整个子都是错误的。获取形状名称之前的 strPic 行很好。

Sub imagerepl()

With Selection
         ' \\ If we selected a InlineShape then convert to Shape
        If .Type = wdSelectionInlineShape Then
            .InlineShapes(1).ConvertToShape
        End If
    End With

strPic = Selection.ShapeRange.Name
Set shp = ActiveDocument.Sections(1).Headers.Shapes(strPic)

'Capture properties of exisitng picture such as location and size
With shp
    t = .Top
    l = .Left
    h = .Height
    w = .Width
End With

ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange(strPic).Delete


Set shp = ActiveDocument.Shapes.AddPicture("C:\Users\tk\Pictures\DFHlogo.png",        msoFalse, msoTrue, l, t, w, h)
shp.Name = strPic
shp.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
shp.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue


End Sub

最佳答案

您可以通过 Range.InlineShapes 集合访问标题图像。例如,以下例程将删除集合中的第一个图像,然后将新图像添加到标题中。

Dim sh As InlineShape

With ThisDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range

    ' Get the first (possibly only, for you) image in the header...
    Set sh = .InlineShapes(1)

    ' Delete it...
    sh.Delete

    ' Add a new image to the header...
    Set sh = .InlineShapes.AddPicture("c:\path\to\my.jpg")

    ' Set its properties...
    sh.Width = 100
    sh.Height = 100
    ...

End With

关于vba - 按标题中的名称访问形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32276939/

相关文章:

mysql - 从word文件导入数据以填充mysql数据库中的表

java - 可以将Word文档docx)转pdf的库

vba - Excel 删除所有空行 600 000 行并重新格式化

vba - 从 VBA 插入公式时出现不同语言问题

java - 向 ms word 添加内容,包括宏

sql - 是否能够将 VBA 变量传递给 ADO SQL 语句?

excel - 我可以在不打开 Excel 或 Word 等相关文件的情况下复制 VBA 代码吗?

excel - 如何获取当月的星期一(完整日期)?

ms-word - tinyMCE 删除 MS Word 标签

excel - 插入的字表不显示边框