Word VBA PageSetup.LeftMargin returns 9999999 while textbox is selected

Word VBA PageSetup.LeftMargin returns 9999999 while textbox is selected

我正在尝试将选定的文本框与活动文档的左边距对齐。我的文档只有一个部分,因此 ActiveDocument.Sections[0].PageSetup.LeftMargin(当前部分)宽度应与 ActiveDocument 相同。PageSetup.LeftMargin 值。

我在 SO 上发现了一个类似的问题。但它没有帮助(除了增加我的理解)。

这是我当前的 C# 代码。我已经尝试过变体(例如 ActiveDocument.Sections 1),但似乎没有任何效果。这几乎就像选择文本框形状时,ActiveDocument.PageSetup 边距值设置为 999999。Word 显然是在说“99999”,因为可能有多个值,但我不明白为什么。我的文档只有 1 个部分。

我做错了什么?谢谢。

    var testMe = wapp.ActiveDocument.PageSetup.LeftMargin; // equals 999999
    foreach (Word.Shape shape in sel.ShapeRange) {
      shape.Left = doc.PageSetup.LeftMargin; // align the textbox with left margin
    }

如果我对问题的理解正确,您只需分配 .Left = 0 即可将形状与左边距对齐。另见 Shape.Left property Remarks.
尝试 (C#):

foreach (Word.Shape shape in sel.ShapeRange) {
  shape.Left = 0; // align the textbox with left margin
}

至于定义 PageSetup.PageWidth 和 PageSetup.LeftMargin,您可以使用 (VBA):

Sub Align()
    Dim sh As Shape
    
    pw = ActiveDocument.PageSetup.PageWidth
    lm = ActiveDocument.PageSetup.LeftMargin
    Debug.Print "PageWidth = " & pw, "LeftMargin = " & lm
    
    For Each sh In ActiveDocument.Range.ShapeRange
        Debug.Print vbLf, "================", vbLf
        section_number = sh.Anchor.Information(wdActiveEndSectionNumber)
        Debug.Print "Shape '" & sh.Name & "' is in " & section_number & " Section"
        
        sec_width = ActiveDocument.Sections(section_number).PageSetup.PageWidth
        Debug.Print "Section " & section_number & " has Width = " & sec_width
        
        sec_lm = ActiveDocument.Sections(section_number).PageSetup.LeftMargin
        Debug.Print "Section " & section_number & " has Left = " & sec_lm
        
        Debug.Print "LeftMargin of the Section got from Shape's Anchor: " & sh.Anchor.Sections(1).PageSetup.LeftMargin
        Debug.Print "Shape '" & sh.Name & "' has Left = " & sh.Left
        
        sh.RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
        sh.Left = 0 '0 is LeftMargin
    Next
End Sub

打印:

PageWidth = 9999999         LeftMargin = 9999999

              ================            

Shape 'Text Box 1' is in 1 Section
Section 1 has Width = 595,3
Section 1 has Left = 85,05
LeftMargin of the Section got from Shape's Anchor: 85,05
Shape 'Text Box 1' has Left = 216

              ================            

Shape 'Text Box 2' is in 2 Section
Section 2 has Width = 841,9
Section 2 has Left = 56,7
LeftMargin of the Section got from Shape's Anchor: 56,7
Shape 'Text Box 2' has Left = 319,75

加法

部分的 .PageSetup.LeftMargin 诊断工具:

Sub Margins()
    Dim i As Integer
    With ActiveDocument
        Debug.Print "ActiveDocument has " & .Sections.Count & " sections"
        Debug.Print "The whole ActiveDocument has .PageSetup.LeftMargin = " & .PageSetup.LeftMargin
        For i = 1 To .Sections.Count
            Debug.Print "Section # " & i & " has .PageSetup.LeftMargin = " & .Sections(i).PageSetup.LeftMargin
        Next
    End With
End Sub

在我的包含 3 个部分的测试文档中打印:

ActiveDocument has 3 sections
The whole ActiveDocument has .PageSetup.LeftMargin = 9999999
Section # 1 has .PageSetup.LeftMargin = 127,6
Section # 2 has .PageSetup.LeftMargin = 70,9
Section # 3 has .PageSetup.LeftMargin = 113,4