尝试使用 VBA 在 Word 中自动拆分文档

问题描述

我正在尝试使用 VBA 使我的团队和我自己目前手动执行的流程自动化——获取一个 Word 文档并根据 H1 部分将其拆分为多个文档(我的意思是,如果一个文档有 6 个 H1 ,那么我们最终得到 6 个文件)。

我发现了一些运行良好的代码,但有几部分我无法完全弄清楚。

  1. 从我的原始文档中获取页脚以显示在子文档中,以及
  2. 在每个文件名的开头添加一个序列号。

一个要求非常简单——我的原始文档有一个页脚,我希望代码吐出的文档具有相同的页脚。现在,生成文件有空白页脚。后一个要求是我最终希望新文件文件名格式为“XX - [headingText].docx”。我使用的代码可以很好地获取标题文本,但我似乎无法插入顺序编号。

这是我正在使用的代码;任何帮助将不胜感激!

    Sub Separateheadings()
    '
    ' Separateheadings Macro
    '
    '
    Application.ScreenUpdating = False
    Dim StrTmplt As String,StrPath As String,StrFlNm As String,Rng As Range,Doc As Document,i As Long
    Dim iTemp As Integer

    With ActiveDocument
     StrTmplt = .AttachedTemplate.FullName
     StrPath = .Path & "\"
      With .Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Style = "heading 1"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
    End With
   
   
    do while .Find.Found
      Set Rng = .Paragraphs(1).Range.Duplicate
      With Rng
        StrFlNm = Replace(.Text,vbCr,"")
       
        For i = 1 To 255
          Select Case i
            Case 1 To 31,33,34,37,42,44,46,47,58 - 63,91 - 93,96,124,147,148
            StrFlNm = Replace(StrFlNm,Chr(i),"")
          End Select
        Next
       iTemp = iTemp + 1
        Do
          If .Paragraphs.Last.Range.End = ActiveDocument.Range.End Then Exit Do
        Select Case .Paragraphs.Last.Next.Style
          Case "heading 1"
            Exit Do
          Case Else
            .MoveEnd wdParagraph,1
          End Select
        Loop
       
      End With
       
      
      Set Doc = Documents.Add(Template:=StrTmplt,Visible:=False)
      With Doc
        .Range.FormattedText = Rng.FormattedText
        .SaveAs2 FileName:=StrPath & StrFlNm,Fileformat:=wdFormatXMLDocument,AddToRecentFiles:=False
        .Close False
      End With
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
End With
Set Doc = nothing: Set Rng = nothing
Application.ScreenUpdating = True
End Sub

解决方法

试试:

Sub SplitDocByHeading1()
Application.ScreenUpdating = False
Dim StrTmplt As String,StrPath As String,StrFlNm As String
Dim Rng As Range,i As Long,j As Long,Doc As Document
Const StrNoChr As String = """*./\:?|"
With ActiveDocument
  StrTmplt = .FullName
  StrPath = .Path & "\"
  'Convert auto numbering to static numbering
  .ConvertNumbersToText (wdNumberAllNumbers)
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = ""
      .Replacement.Text = ""
      .Style = wdStyleHeading1
      .Format = True
      .Forward = True
      .Wrap = wdFindStop
      .Execute
    End With
    Do While .Find.Found
      Set Rng = .Duplicate: i = i + 1
      StrFlNm = Split(Rng.Paragraphs(1).Range.Text,vbCr)(0)
      For j = 1 To Len(StrNoChr)
        StrFlNm = Replace(StrFlNm,Mid(StrNoChr,j,1),"_")
      Next
      StrFlNm = Format(i,"00") & "_" & StrFlNm & ".docx"
      Set Rng = Rng.GoTo(What:=wdGoToBookmark,Name:="\HeadingLevel")
      Set Doc = Documents.Add(Template:=StrTmplt,Visible:=False)
      With Doc
        .Range.FormattedText = Rng.FormattedText
        .SaveAs2 FileName:=StrPath & StrFlNm,Fileformat:=wdFormatXMLDocument,AddToRecentFiles:=False
        .Close False
      End With
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
End With
Set Doc = Nothing: Set Rng = Nothing
Application.ScreenUpdating = True
End Sub