2017-08-31 180 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