检查每一行的 Word 文档样式

Check Word Document Style for every single lines

我有一个混合样式的 Word 文档,如果该行匹配 h1、h2 或 h3,则需要添加 HTML 标签。 我正在尝试将新字符串保存到数组中并另存为 .html 文件 但是不知道.vba中的if条件怎么写,所以请帮忙。

Public Sub Word2HTML()
    Dim NewArray() As String
    Dim TextFile As Integer
    Dim FilePath As String
    FilePath = "C:\Temp\final.html"
    TextFile = FreeFile
    'Open the text file
    Open FilePath For Output As TextFile
    
    For i = 1 To ActiveDocument.Paragraphs.Count
        ' error on the next line
        If ActiveDocument.Paragraphs(i).Style.Text = "Heading 1" Then
            ReDim Preserve NewArray(i-1)
            NewArray(i-1) = "<h1>" + ActiveDocument.Paragraphs(i) + "</h1>"
        Elseif ActiveDocument.Paragraphs(i).Style.Text = "Heading 2" Then
            ReDim Preserve NewArray(i-1)
            NewArray(i-1) = "<h2>" + ActiveDocument.Paragraphs(i) + "</h2>"
        Elseif ActiveDocument.Paragraphs(i).Style.Text = "Heading 3" Then
            ReDim Preserve NewArray(i-1)
            NewArray(i-1) = "<h3>" + ActiveDocument.Paragraphs(i) + "</h3>"
        Else
        End If
    Next i
    
    For Each headline In NewArray
        Print #TextFile, headline
    Next
    Close TextFile

End Sub

这是我从 MS Word 得到的错误信息 VB

样式没有 .Text 属性。您可能正在尝试这样做:

If ActiveDocument.Paragraphs(i).Style = "Heading 1" Then

  ' do something here

End If

以下捕获所有标题并将它们保存到与活动文档同名的 HTML 文件中:

Sub HeadingsToHTML()
Application.ScreenUpdating = False
Dim Rng As Range, h As Long
With ActiveDocument
  With .Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Font.Bold = True
    .Wrap = wdFindContinue
    .Text = ""
    .Replacement.Text = "<strong>^&</strong>"
    .Execute Replace:=wdReplaceAll
    .Text = "^p"
    .Replacement.Text = "</strong>^&<strong>"
    .Execute Replace:=wdReplaceAll
  End With
  Set Rng = .Range(0, 0)
  Set Rng = .TablesOfContents.Add(Range:=Rng, UseHeadingStyles:=True, _
    UpperHeadingLevel:=1, LowerHeadingLevel:=9, IncludePageNumbers:=False).Range
  Rng.Fields.Unlink
  Rng.Collapse wdCollapseEnd
  Rng.End = .Range.End
  Rng.Delete
  With .Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    For h = 1 To 9
      .Style = "TOC " & h
      .Text = "([!^13]{1,})"
      .Replacement.Text = "<h" & h & "></h" & h & ">"
      .Execute Replace:=wdReplaceAll
    Next
  End With
  .SaveAs2 FileName:=Split(.FullName, ".doc")(0) & ".html", FileFormat:=wdFormatText, AddToRecentFiles:=False
End With
Application.ScreenUpdating = True
End Sub

也比遍历所有段落快得多。

如果您的标题有 #s 级并且您不想在输出中使用它们,请替换:

  .Text = "([!^13]{1,})"

与:

  .Text = "[!^13]@^t([!^13]{1,})"