【问题标题】:Inserting a image in first page header, and defining its position in VBA在首页页眉中插入图像,并在 VBA 中定义其位置
【发布时间】:2019-06-10 22:22:19
【问题描述】:

我正在尝试通过 VBA 在文档的第一页页眉中插入图像。

有多行可以做到这一点,但每行都有问题,我将列出:

这是我最喜欢的方法,但它不是在第一页的页眉中插入图像,而是在所有剩余的页眉中插入图像,而且它也不允许我设置位置:

ActiveDocument.Sections(1).Headers(2).Shapes.AddPicture ("C:\1.jpg")

这会返回一个越界错误:

Set shpCanvas=ActiveDocument.Shapes.AddCanvas(Left:=0, Top:=0, Width:=180, Height:=50)

shpCanvas.CanvasItems.AddPicture FileName:="C:\1.jpg", LinkToFile:=False, SaveWithDocument:=True

直接插入图片,但它通常不在位置,停留在标题的中间,我宁愿把它放在左边

ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.InlineShapes.AddPicture ("C:\1.jpg")

我只是 VBA 和 word 的初学者,对于我可能有的任何怪诞想法,我深表歉意

【问题讨论】:

  • 要弄清楚如何在 VBA 中做某事,您可以做的一件事是开始在 Word 中录制宏,手动执行您想做的任何事情,停止录制,然后查看生成的代码在宏中由 Word。这通常会提供很好的线索。

标签: vba ms-word


【解决方案1】:

第一个代码示例确实对我有用 - 我在第一页看到了图片。但是由于您没有描述文档的结构,我可能不会测试您正在使用的内容...

您不应该尝试使用画布。

ShapeInlineShape 之间的区别在于 Word 将后者处理为文本字符。如果第三行将图片定位在该行的中间,则该段落可能被格式化为“居中”,而不是“左”。尝试更改段落格式。

要在使用Shape 时定位结果,需要一个对象变量才能处理插入的内容。例如:

Dim shp As Word.Shape, ils As Word.InlineShape

Set shp = ActiveDocument.Sections(1).Headers(2).Shapes.AddPicture("C:\1.jpg")
shp.Top = 0
shp.Left = 0

声明一个对象,然后将插入的图片分配给该对象,一步到位。随后,对象变量就可以用来寻址图片了。

【讨论】:

    【解决方案2】:

    感谢您的帮助,更正确的是它是这样工作的

    Dim shp2 As Word.Shape
    Dim shp3 As Word.InlineShape
    
    Set shp3 = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.InlineShapes.AddPicture("C:\1.jpg")
                                Set shp2 = shp3.ConvertToShape
                                shp2.Top = 0
                                shp2.Left = 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-30
      • 2016-01-07
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      相关资源
      最近更新 更多