none
Word VSTO - Change the absolute positions' type? RRS feed

  • Question

  • I use the following VB.NET (VSTO) code to add a shape in MS-Word,

    Dim app As Word.Application = Globals.ThisAddIn.Application
    Dim doc As Word.Document = app.ActiveDocument
    Dim left As Single = CSng(Convert.ToDouble(app.Selection.Information(Word.WdInformation.wdHorizontalPositionRelativeToPage)))
    Dim top As Single = CSng(Convert.ToDouble(app.Selection.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)))
    Dim shape As Word.Shape = doc.Shapes.AddShape(1, left, top, 225.1F, 224.5F)
    
    shape.Fill.BackColor.RGB = ColorTranslator.ToOle(Color.Transparent)
    shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
    shape.Fill.Transparency = 0.0F
    shape.Line.Transparency = 0.0F
    shape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse

    What this code does is, it adds a rectangle shape at cursor point and makes it transparent (both background and line).

    Now I like to change the absolute positions' type. To explain further, when you select the rectangle shape, then if you select the Ribbon tab Format > Position > More Layout Options... as shown in the image below,

    It will open the following dialog,

    In the above dialog I like to change Column and Paragraph marked by the red rectangles into the type Margin. How to do this by code?

    Sunday, August 2, 2015 7:04 PM

Answers

  • Hi Codename,

    >> In the above dialog I like to change Column and Paragraph marked by the red rectangles into the type Margin

    If you want to change the Horizontal and Vertical, you could set the RelativeHorizontalPosition Property and RelativeHorizontalPosition Property, here is simple code as below:

        Private Sub ShapeFormat_Click(sender As Object, e As RibbonControlEventArgs) Handles ShapeFormat.Click
            With Globals.ThisAddIn.Application.ActiveDocument.Shapes(1)
                .RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
                .RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin
            End With
        End Sub

    For setting other properties of Shape, you could refer the link below:

    # Shape Properties (Word)
    https://msdn.microsoft.com/en-us/library/office/dn353090.aspx

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Codename K2 Monday, August 3, 2015 7:47 AM
    Monday, August 3, 2015 6:00 AM

All replies

  • Hi Codename,

    >> In the above dialog I like to change Column and Paragraph marked by the red rectangles into the type Margin

    If you want to change the Horizontal and Vertical, you could set the RelativeHorizontalPosition Property and RelativeHorizontalPosition Property, here is simple code as below:

        Private Sub ShapeFormat_Click(sender As Object, e As RibbonControlEventArgs) Handles ShapeFormat.Click
            With Globals.ThisAddIn.Application.ActiveDocument.Shapes(1)
                .RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
                .RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin
            End With
        End Sub

    For setting other properties of Shape, you could refer the link below:

    # Shape Properties (Word)
    https://msdn.microsoft.com/en-us/library/office/dn353090.aspx

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Codename K2 Monday, August 3, 2015 7:47 AM
    Monday, August 3, 2015 6:00 AM
  • Thanks Edward Z. Your solution worked.

    Monday, August 3, 2015 7:48 AM