繁体   English   中英

使用 Word VBA 2007 检查 Excel 中单元格的文本对齐方式

[英]Checking the text alignment of a cell in Excel using Word VBA 2007

我知道以下 Word VBA 2007 代码

objExcel.ActiveWorkbook.Sheets("Book1").Cells(3, 4)

可以获取名为“Book1”的已打开 Excel 工作簿 工作表的单元格 D3(第 3 行,第 4 列)内的文本。 但是如何获得单元格的对齐方式(即左、右、居中或对齐)? 谢谢!

有两个方面控制水平单元格对齐。 第一个是手动或通过代码应用的强制非默认单元格对齐。 第二种是xlGeneral格式,根据数据的性质可以右对齐、左对齐或居中对齐。

With objExcel.ActiveWorkbook.Sheets("Book1")
    Select Case .Cells(3, 4).HorizontalAlignment
        Case xlRight        ' equal to -4152
            Debug.Print "The cell is right-aligned."
        Case xlCenter       ' equal to -4108
            Debug.Print "The cell is center-aligned."
        Case xlLeft         ' equal to -4131
            Debug.Print "The cell is left-aligned."
        Case xlGeneral      ' equal to 1
            Select Case Application.Evaluate("TYPE(" & .Cells(3, 4).Address(0, 0, external:=True) & ")")
                Case 1
                    Debug.Print "The cell is right-aligned."
                Case 2
                    Debug.Print "The cell is left-aligned."
                Case 4, 16
                    Debug.Print "The cell is center-aligned."
                Case Else
                    Debug.Print "The cell is part of an array."
            End Select
    End Select
End With

您确定这是您的工作表而不是您的工作簿,称为Book1吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM