【问题标题】:VSTO Word & Visual basic: Shape.Left property does not take on assigned valueVSTO Word 和 Visual Basic:Shape.Left 属性不具有赋值
【发布时间】:2017-09-01 22:52:40
【问题描述】:

我想在文档每一页的右上角放置一个徽标。此功能已存在于我们管理的 Word 加载项中。但是,此功能无法正常工作。加载项将图像转换为形状,然后将此图像放置在距文档左角固定距离的位置。这适用于 A4 格式的文档,但只要文档的方向或大小发生变化,徽标位置就会关闭。

我尝试了很多策略来解决这个问题,但还没有找到令人满意的方法。我目前的策略是动态确定页面左侧和徽标之间的距离,然后通过调用 .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

【问题讨论】:

    标签: vb.net vsto


    【解决方案1】:

    除了将左侧位置设置为绝对值外,您还可以将其设为相对位置,并且基本上“右对齐”形状。如果您如下所示设置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
    

    当然,如果您在标题中有其他项目,那么您将创建一个包含多个单元格的表格并适当调整间距,但对于这个示例,我只是在标题中放置一个无边框的单单元格表格并设置其自动调整行为 fitwindow 以便表格将填充页面的宽度,即使边距或格式发生更改。然后我只用图像设置单元格的顶部和右侧填充,您正在寻找的行为就实现了。

    【讨论】:

    • 感谢您的回答!我知道以下技术,但是使用这种技术,我无法在形状/徽标和页面角落之间留下一些空白。有没有办法调整此代码,以便将徽标放置在距离右上角和右上角 1 或 2 厘米的位置?
    • 查看编辑,内联形状有很多图形格式选项,但没有那么多布局选项。使用表格来保存形状可以让您更好地控制布局。
    猜你喜欢
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多