[英]Check for an empty Text Box
我在 Word 2016 文档中有一个文本框。 这不是 TextBox 表单对象,而是您从此处的“插入”选项卡插入的普通文本框:
我正在尝试检查文档打开时是否为空。 对于我的生活,我无法弄清楚如何做到这一点。 我已经尝试了以下所有方法:
If (Len(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text & vbNullString) = 0) Then
If (IsNull(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text)) Then
If (LTrim(RTrim(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text)) = "") Then
If (ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text = "") Then
当文本框为空时,这些都不返回true? 这是文本框:
似乎文本框总是包含一个段落标记(我无法删除)。 这是 VBA 手表显示的内容:
Watch : : ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text : "
" : String : ThisDocument.Document_Open
请注意,手表是两条独立的线,这让我觉得里面有 CRLF 或其他东西?
在其他为空的文本框中总是有一个分节符。 因此,您可以使用以下内容:
Private Sub Document_Open()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2")
If Not .TextFrame Is Nothing Then
If Trim(.TextFrame.TextRange.Text) = vbCr Then
MsgBox "Empty Text Range"
End If
Else
MsgBox "No Text Range"
End If
End With
End Sub
如果文本框在特定的几页 msword 中包含空文本或没有文本,我可以知道如何编码以删除文本框吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.