1. 批量替换文本
将文档中所有“旧文本”替换为“新文本”。
Sub ReplaceText()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "旧文本"
.Replacement.Text = "新文本"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
2. 自动保存文档
每隔5分钟自动保存当前文档。
Dim AutoSaveTime As Double
Sub AutoSaveStart()
AutoSaveTime = Now + TimeValue("00:05:00")
Application.OnTime AutoSaveTime, "AutoSaveMacro"
End Sub
Sub AutoSaveMacro()
ActiveDocument.Save
Call AutoSaveStart
End Sub
3. 插入当前日期
在光标位置插入系统当前日期。
Sub InsertCurrentDate()
Selection.TypeText Text:=Format(Now, "yyyy年mm月dd日")
End Sub
4. 删除空段落
清除文档中所有空行(空段落)。
Sub DeleteEmptyParagraphs()
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
If Len(Trim(para.Range.Text)) = 1 Then
para.Range.Delete
End If
Next para
End Sub