不要忘了,VBA也能够在Word中使用,也可以在很多情况下取得不错的效果。下面整理了几段基础的Word VBA代码,供有兴趣的朋友学习参考。If Selection.Start = Selection.Paragraphs(1).Range.Start Then
MsgBox "处于段落开头."
Else
MsgBox "不处于段落开头"
End If
MsgBox ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count
If Selection.Characters(1) Like "[a-zA-Z0-9]" Then MsgBox "所选文本第1个字符是字母或数字."
If Selection.Characters(1) Like "[!a-zA-Z0-9]" Then MsgBox "所选文本第1个字符不是字母或数字."
x = Selection.Information(wdHorizontalPositionRelativeToPage)
y = Selection.Information(wdVerticalPositionRelativeToPage)
注意,要使上述代码返回正确的值,需要设置为正常的页面视图,页面缩放设置为100%。单位换算为:72
pts. = 1 inch = 2.54 cmMsgBox Selection.Information(wdNumberOfPagesInDocument)
MsgBox ActiveDocument.BuiltInDocumentProperties("Number of Pages")
MsgBox Selection.Information(wdActiveEndPageNumber)
代码1:所选择的文本是否位于末尾(选择一定要扩展到最后面的段落符号)Dim SelectionIncludesFinalParagraphMark As Boolean
If Selection.Type = wdSelectionNormal And Selection.End = ActiveDocument.Content.End Then SelectionIncludesFinalParagraphMark = True
MsgBox SelectionIncludesFinalParagraphMark
代码2:光标是否处于文档末尾(光标处于文档末尾处)Dim InsertionPointFlashingAtEndOfDoc As Boolean
If Selection.Type = wdSelectionIP And Selection.End = ActiveDocument.Content.End - 1 Then InsertionPointFlashingAtEndOfDoc = True
MsgBox InsertionPointFlashingAtEndOfDoc
Dim oCell As Cell
Dim oRow As Row
For Each oRow In Selection.Tables(1).Rows
For Each oCell In oRow.Cells
If oCell.Range.Text = Chr(13) & Chr(7) Then
MsgBox "表格中第" & oCell.RowIndex & "行,第" & oCell.ColumnIndex & "列的单元格为空单元格."
End If
Next oCell
Next oRow
Dim oCell As Cell
Dim oRow As Row
Dim MyRange As Range
For Each oRow In Selection.Tables(1).Rows
For Each oCell In oRow.Cells
Set MyRange = oCell.Range
MyRange.End = MyRange.End - 1
If Len(MyRange.Text) = 0 Then
MsgBox "表格中第" & oCell.RowIndex & "行,第" & oCell.ColumnIndex & "列的单元格为空单元格."
End If
Next oCell
Next oRow
欢迎在下面留言,完善本文内容,让更多的人学到更完美的知识。
欢迎到知识星球:完美Excel社群,进行技术交流和提问,获取更多电子资料,并通过社群加入专门的微信讨论群,更方便交流。