【发布时间】:2022-11-12 06:04:44
【问题描述】:
你能帮我如何按字符分割word文件吗?
我找不到任何方法来按互联网上的字符数分割 word 文件!
【问题讨论】:
-
到目前为止,您尝试过什么代码?
-
请提供足够的代码,以便其他人可以更好地理解或重现该问题。
标签: ms-word
你能帮我如何按字符分割word文件吗?
我找不到任何方法来按互联网上的字符数分割 word 文件!
【问题讨论】:
标签: ms-word
例如,要将文档拆分为 500 个字符的块:
Sub SplitDocument()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long
Const Char As Long = 500
With ActiveDocument
' Process each Section
For i = 1 To Int(.Characters.Count / Char)
' Get the whole Section
Set Rng = .Range((i - 1) * Char, i * Char)
' Copy the range
Rng.Copy
Rng.Collapse wdCollapseEnd
Call NewDoc(ActiveDocument, (i - 1) * Char + 1)
Next
If Rng.End < .Range.End Then
Rng.End = .Range.End
' Copy the range
Rng.Copy
Rng.Collapse wdCollapseEnd
Call NewDoc(ActiveDocument, (i - 1) * Char + 1)
End If
End With
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Sub NewDoc(DocSrc As Document, i As Long)
Dim DocTgt As Document, HdFt As HeaderFooter, j As Long
j = j + 1
' Create the output document
Set DocTgt = Documents.Add(Template:=DocSrc.AttachedTemplate.FullName, Visible:=False)
With DocTgt
' Paste contents into the output document, preserving the formatting
.Range.PasteAndFormat (wdFormatOriginalFormatting)
' Replicate the headers & footers
For Each HdFt In DocSrc.Sections(DocSrc.Characters(i).Sections(1).Index).Headers
.Sections(1).Headers(HdFt.Index).Range.FormattedText = HdFt.Range.FormattedText
Next
For Each HdFt In DocSrc.Sections(DocSrc.Characters(i).Sections(1).Index).Footers
.Sections(1).Footers(HdFt.Index).Range.FormattedText = HdFt.Range.FormattedText
Next
' Save & close the output document
.SaveAs FileName:=Split(DocSrc.FullName, ".doc")(0) & "_" & j & ".docx", _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
Set DocTgt = Nothing: Set DocSrc = Nothing
End Sub
【讨论】: