【发布时间】:2017-09-01 22:52:40
【问题描述】:
我想在文档每一页的右上角放置一个徽标。此功能已存在于我们管理的 Word 加载项中。但是,此功能无法正常工作。加载项将图像转换为形状,然后将此图像放置在距文档左角固定距离的位置。这适用于 A4 格式的文档,但只要文档的方向或大小发生变化,徽标位置就会关闭。
我尝试了很多策略来解决这个问题,但还没有找到令人满意的方法。我目前的策略是动态确定页面左侧和徽标之间的距离,然后通过调用 .RelativeHorizontalPosition 属性并将其链接到右边距区域,使该位置相对于页面右侧。
不幸的是,与 Shape 对象的 .Left 属性交互很麻烦。 .Left 属性不采用我分配的值,而是采用负值。我已经检查了我多次分配的参数。有谁知道为什么会出现这种情况以及如何解决?
示例代码
Private Sub AddLogos(section As Section, header As HeaderFooter)
Dim wordApp As Word.Application = Globals.ThisAddIn.Application
Dim pageWidth As Single = section.PageSetup.PageWidth
Dim imgFilePath As String = "filepath"
Dim leftDistanceA4 As Single = 11
Dim logo As Word.Shape
Try
If wordApp.ActiveDocument.SaveFormat >= 12 Then
logo = header.Range.InlineShapes.AddPicture(m_sImageLogo, False, True).ConvertToShape()
Else 'Word 97-2003 Support
logo = header.Shapes.AddPicture(imgFilePath, False, True)
End If
Catch ex As Exception
Throw New Exception("Error message.")
End Try
Dim distanceFromRightPageEdge = wordApp.CentimetersToPoints(21 - leftDistanceA4)
Dim distanceFromLeftPageEdge = pageWidth - distanceFromRightPageEdge
With logo
.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage
.Left = distanceFromLeftPageEdge
.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionRightMarginArea
End With
【问题讨论】: