这是一个测试示例,您需要在Word中标记,以便大胆的单词具有" heading1"样式。
宏将在标题上将文档分为单个.RTF文件,这些文件将使用粗体标题作为文件名 .rtf扩展。
hadrotes
文本段落
Perisseia
文本段落
Perisseuma
文本段落
Sub SplitDocOnHeading1ToRtfWithoutHeadingInOutput()
'Splits the document on Heading1 style, into new documents, Heading1 is included in the data.
Application.ScreenUpdating = False
Dim Rng As Range, DocSrc As Document, DocTgt As Document
Dim i As Long, StrTxt As String: Const StrNoChr As String = """*/:?|"
Set DocSrc = ActiveDocument
With DocSrc.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.Forward = True
.Text = ""
.Style = wdStyleHeading1
.Replacement.Text = ""
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
Set Rng = .Paragraphs(1).Range
Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
Set DocTgt = Documents.Add(DocSrc.AttachedTemplate.FullName)
With DocTgt
Application.ScreenUpdating = False
.Range.FormattedText = Rng.FormattedText
StrTxt = Split(.Paragraphs.First.Range.Text, vbCr)(0)
' Strip out illegal characters
For i = 1 To Len(StrNoChr)
StrTxt = Replace(StrTxt, Mid(StrNoChr, i, 1), "_")
Next
.Paragraphs.First.Range.Delete
.SaveAs2 FileName:=DocSrc.Path & "" & StrTxt & ".rtf", Fileformat:=wdFormatRTF, AddToRecentFiles:=False
.Close False
End With
.Start = Rng.End
.Find.Execute
Loop
End With
Set Rng = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
Application.ScreenUpdating = True
End Sub
停止单词打开另一个窗口,每次宏制作新文档时,只需在这些地方添加此代码###########
Sub SplitDocOnHeading1ToRtfWithoutHeadingInOutput()
'Splits the document on Heading1 style, into new documents, Heading1 is NOT included in the data
'but becomes the file name.
With Word.Application '##########
.Visible = False '##########
Application.ScreenUpdating = False
Dim rng As Range, DocSrc As Document, DocTgt As Document
Dim i As Long, StrTxt As String: Const StrNoChr As String = """*/:?|"
Set DocSrc = ActiveDocument
With DocSrc.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.Forward = True
.Text = ""
.Style = wdStyleHeading1
.Replacement.Text = ""
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
Set rng = .Paragraphs(1).Range
Set rng = rng.GoTo(What:=wdGoToBookmark, Name:="HeadingLevel")
Set DocTgt = Documents.Add(DocSrc.AttachedTemplate.FullName)
With DocTgt
Application.ScreenUpdating = False
.Range.FormattedText = rng.FormattedText
StrTxt = Split(.Paragraphs.First.Range.Text, vbCr)(0)
' Strip out illegal characters
For i = 1 To Len(StrNoChr)
StrTxt = Replace(StrTxt, Mid(StrNoChr, i, 1), "_")
Next
.Paragraphs.First.Range.Delete 'comment out this line if you want to retain headings in the output file
.SaveAs2 FileName:=DocSrc.Path & "" & StrTxt & ".rtf", Fileformat:=wdFormatRTF, AddToRecentFiles:=False
.Close False
End With
.Start = rng.End
.Find.Execute
Loop
End With
Set rng = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
Application.ScreenUpdating = True
.Visible = True '##########
End With '##########
End Sub
使用
Set DocTgt = Documents.Add(DocSrc.AttachedTemplate.FullName,,,False)
这应该使文档看不见。希望这会有所帮助。
您真正需要做的就是更改:
Set DocTgt = Documents.Add(DocSrc.AttachedTemplate.FullName)
to:
Set DocTgt = Documents.Add(Template:=DocSrc.AttachedTemplate.FullName, Visible:=False)
隐藏词通过:
With Word.Application
.Visible = False
是有风险的 - 如果出现任何问题,您可能最终会出现在后台运行并保持文档打开的无形单词会话。然后,您需要使用任务管理器杀死Word - 然后尝试恢复您的工作。