我使用的是 Word 14。我创建了可处理与文本内联的图像的宏,例如
Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
以及将图像居中
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
如何操作位于文本后面的图像?
这可能吗?我在录制宏时无法选择该图像。
我有一个尺寸为 1.2 厘米 x 1.2 厘米(约 1/2 英寸见方)的小方形图像,位于文本后面(或文本前面)。它的水平位置始终是页边距右侧 0 厘米,其垂直位置有一个绝对位置低于页边距,例如 4.2 厘米。
我想复制此图像,然后将其粘贴到相同的垂直位置(页边距下方 4.2 厘米),但在页面上的水平位置(页边距右侧 3.7 厘米)进一步粘贴。
对于配置为在文本后面换行的形状,
WrapFormat.Type
属性将返回wdWrapBehind
。
Sub demo()
Dim objDoc As Document
Dim objShp As shape, dupShape As shape
Set objDoc = ActiveDocument
For Each objShp In objDoc.Shapes
' Get the shape object (Behind Text)
If objShp.WrapFormat.Type = wdWrapBehind Then
objShp.Select
Selection.Copy
Selection.Range.Paste
Set dupShape = Selection.ShapeRange(1)
dupShape.WrapFormat.Type = wdWrapBehind
' set shape object location
dupShape.Top = objShp.Top
dupShape.Left = InchesToPoints(1.46) ' 3.7cm = 1.46inch
Exit For
End If
Next
Selection.Collapse Direction:=wdCollapseEnd
End Sub
WrapFormat.Type 语法请参考微软文档。
https://learn.microsoft.com/en-us/office/vba/api/word.wrapformat.type?WT.mc_id=M365-MVP-33461
非常感谢。我忘了提及,我一次只想制作一张图像,当我一页一页地写文档时,可能总是最后一张图像。从你的代码(文档、形状、选择、范围等)中学习,我最终得到了这个,它似乎可以满足我的需要。不太理解它,我希望它不会对我的文档造成问题。
Dim objDoc As Document
Dim dupShape As Shape
Set objDoc = ActiveDocument
n = objDoc.Shapes.Count ' Last obj added to document
m = n + 1 ' New obj being added here
' Get position of last obj added
it = objDoc.Shapes(n).Top
il = objDoc.Shapes(n).Left
'Copy/Paste
objDoc.Shapes(n).Select
Selection.Copy
Selection.Range.Paste
' set new obj position
objDoc.Shapes(m).Top = it
objDoc.Shapes(m).Left = il + 104.7 ' 104.7 = 3.7cm (1.46")