2017-08-31 176 views
0

我想在文档的每个页面的右上角放置一个徽标。此功能已存在于由我们管理的Word加载项中。但是,此功能无法正常工作。该加载项将图像转换为形状,然后将此图像与左侧文档角落放置在一个固定的距离处。这适用于A4格式的文档,但只要文档的方向或大小发生更改,徽标位置就会关闭。VSTO Word&Visual basic:Shape.Left属性不承担指定值

我已经尝试了很多策略来解决这个问题,但还没有找到一个令人满意的方法。我目前的策略是动态确定左页面和徽标之间的距离,然后通过调用.RelativeHorizo​​ntalPosition属性并将其链接到右边空白区域来使该位置相对于页面的右侧。

不幸的是与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 

回答

0

相反的左侧位置设置为一个绝对值,你可以把它相对的,本质上是“右对齐”的形状。如果您按如下所示设置RelativeHorizo​​ntalPosition和Left属性,则图像将被放置在右上角,并且即使在文档的格式或大小被更改时也将保持其相对于该角的位置。

Const imgpath As String = "[your path]" 

    Dim app As New Microsoft.Office.Interop.Word.Application 
    Dim doc As Microsoft.Office.Interop.Word.Document = app.Documents.Add() 
    Dim head As Microsoft.Office.Interop.Word.HeaderFooter = doc.Sections(1).Headers(1) 
    Dim img As Microsoft.Office.Interop.Word.Shape = head.Shapes.AddPicture(imgpath, False, True) 
    With img 
     .RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin 
     .Left = Microsoft.Office.Interop.Word.WdShapePosition.wdShapeRight 
    End With 
    app.Visible = True 

    'dispose references 

编辑:如果你需要更多的控制权的定位不是简单的图像锚定到页面的右上角,内联形状本身不具备这一点。相反,请考虑在标题中使用无边界表格以提供对其内容的更多控制。一旦图像是表的一个孩子,您可以访问所有的表格式控件到图像上使用:

Const imgpath As String = "[your path]" 
    Const imgMarginCM As Integer = 2 

    Dim app As New Microsoft.Office.Interop.Word.Application 
    Dim doc As Microsoft.Office.Interop.Word.Document = app.Documents.Add() 
    Dim head As Microsoft.Office.Interop.Word.HeaderFooter = doc.Sections(1).Headers(1) 
    Dim tbl As Microsoft.Office.Interop.Word.Table = doc.Tables.Add(head.Range, 1, 1) 
    With tbl 
     .Borders.Enable = False 
     .AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitWindow) 
     .Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight 
     .Cell(1, 1).TopPadding = app.CentimetersToPoints(imgMarginCM) 
     .Cell(1, 1).RightPadding = app.CentimetersToPoints(imgMarginCM) 
     .Cell(1, 1).Range.InlineShapes.AddPicture(imgpath, False, True) 
    End With 
    app.Visible = True 

    'dispose references 

当然,如果你在标题中有其他项目,那么你会创建一个表格中有多个单元格并适当调整间距,但对于此示例,我只是在标题中放置了一个无边界的单元格表格,并将其自动调整行为设置为适合窗口,以便表格将填充页面的宽度,即使边距或格式被改变。然后,我只是用图像设置单元格的顶部和右侧填充,并且您正在查找的行为已实现。

+0

谢谢你的回答!我意识到以下技术,但是使用这种技术,我无法在形状/徽标和页面的角落之间留下一些空白区域。有没有办法调整这些代码,以便将徽标放置在离顶部和右侧角1或2厘米处? – Fluous

+0

请参阅编辑,内联形状有许多图形格式选项,但没有太多布局选项。使用表格来保持形状将使您对布局有更多的控制权。 – soohoonigan