【发布时间】:2017-12-12 08:36:33
【问题描述】:
我正在尝试编写一个 VBA 宏来将图像与页面的右上角对齐,并将文本环绕设置为“文本后”和“页面上的固定位置”。
通常我选择图像并通过对话框进行所有这些设置。但一段时间后它会变得乏味。我想知道是否有办法以编程方式进行。我希望我可以打开页眉,粘贴到我的页眉图像中,然后单击宏按钮以使宏与仍选择的图像对齐。
我尝试记录我的操作宏,但该宏没有记录我的任何对话操作(文本后面、固定页面位置等)。所以该方法没有提供解决方案。我用页眉内外的图像进行了尝试,但没有成功。
是否可以让一些 VBA 代码对齐当前选定的图像对象?理想情况下,我会打开页眉,粘贴我的页眉图像,然后运行 VBA 宏来执行上面的四个操作(文本后面、页面上的固定位置、顶部与页面对齐、右侧与页面对齐)。我希望有人可以向我展示如何或向我指出一些说明如何做到这一点的文档或示例。
更新
由于某种原因,我无法在论坛上发帖,所以我研究了几天的问题,最后为下一个人拼凑了这个解决方案。我希望我知道在哪里可以找到这类事情的手册或教程。
但唯一的方法似乎是从网上论坛拼凑解决方案。这是我的贡献! :-)
Sub AlignTopRight()
' Paste an image into Word so it is still selected
' Then invoke this macro to align it to the top right corner
' And to set it behind text, fixed position on the page
Application.ScreenUpdating = False
Dim Shp As Shape
On Error Resume Next
'I'm not sure if this block is required, but it works
Set Shp = Selection.InlineShapes(1)
If Not Shp Is Nothing Then
Set Shp = Selection.InlineShapes(1).ConvertToShape
Else
Set Shp = Selection.ShapeRange.Item(1)
End If
If Not Shp Is Nothing Then
With Shp
.LockAspectRatio = True
' for absolute positioning
'.Left = CentimetersToPoints(5.5)
'.Top = CentimetersToPoints(0.5)
'.Width = CentimetersToPoints(2.5)
'put the image behind text
.WrapFormat.Type = wdWrapBehind
'this was the tricky part, discovering this
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Top = wdShapeTop 'if you say =0, it sets the AbsolutePx in the dialog
.Left = wdShapeRight 'these wdShapeXX objects set the Align field in the dialog
End With
End If
Set Shp = Nothing
Application.ScreenUpdating = True
End Sub
【问题讨论】: