简体   繁体   English

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

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

I know that the following Word VBA 2007 code我知道以下 Word VBA 2007 代码

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

can get the text inside the cell D3 (Row 3, Column 4) of an opened Excel workbook worksheet named "Book1".可以获取名为“Book1”的已打开 Excel 工作簿 工作表的单元格 D3(第 3 行,第 4 列)内的文本。 But how to get the way of alignment (ie left, right, center or justified) of the cell?但是如何获得单元格的对齐方式(即左、右、居中或对齐)? Thanks!谢谢!

There are two aspects that govern the horizontal cell alignment.有两个方面控制水平单元格对齐。 The first is a forced non-default cell alignment that has been applied manually or through code.第一个是手动或通过代码应用的强制非默认单元格对齐。 The second is the xlGeneral format which may be right, left or center aligned according to the nature of the data.第二种是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

Are you sure it is your worksheet and not your workbook that is called Book1 ?您确定这是您的工作表而不是您的工作簿,称为Book1吗?

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

相关问题 使用Excel 2007+ VBA和单元格的NumberFormat属性设置数字文本的格式 - Using Excel 2007+ VBA and a Cell's NumberFormat Property to Format Number Text 结合使用VBA和Excel 2007将字符串中存储的数字与包含以文本格式设置的数字的单元格进行比较 - Using VBA with excel 2007 to compare a number stored in a string to a cell which contains a number formatted as text 使用 VBA 从 Excel 中打开 Word Doc 以查找日期文本并将其替换为 Excel 中单元格中的日期文本 - Open a Word Doc from Excel using VBA to find a Date text and replace it with a date text from a cell in Excel VBA Excel 用 Excel 单元格值替换 Word 文本 - VBA Excel replace Word text with Excel cell value VBA Excel2007。在单元格中创建按钮 - VBA excel 2007. Create Button in cell 使用 Word VBA 将特定 Excel 单元格值拉入 Word 文档 - Pull particular Excel cell value into Word document using Word VBA 如何在Excel中使用VBA删除文本字符串中单词的最后一个字符并插入到另一个单元格? - How to remove the last character of a word in a text string and insert to another cell using VBA in Excel? 使用VBA从Word将文本获取到Excel - Getting text from Word to Excel using VBA 如何使用excel VBA将文本复制到word中 - How to copy text into word using excel VBA VBA从Word复制多行文本粘贴到Excel单元格 - VBA Copy multiline text from Word paste into Excel cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM