Vba 检查每一行的Word文档样式

Vba 检查每一行的Word文档样式,vba,ms-word,Vba,Ms Word,我有一个混合样式的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&quo

我有一个混合样式的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
Public Sub-Word2HTML()
Dim NewArray()作为字符串
将文本文件设置为整数
将文件路径设置为字符串
FilePath=“C:\Temp\final.html”
TextFile=FreeFile
'打开文本文件
打开作为文本文件输出的文件路径
对于i=1到ActiveDocument.parations.Count
'下一行出现错误
如果ActiveDocument.段落(i).Style.Text=“标题1”,则
ReDim保留新阵列(i-1)
NewArray(i-1)=“”+ActiveDocument。段落(i)+“”
Elseif ActiveDocument.段落(i).Style.Text=“标题2”然后
ReDim保留新阵列(i-1)
NewArray(i-1)=“”+ActiveDocument。段落(i)+“”
Elseif ActiveDocument.段落(i).Style.Text=“标题3”然后
ReDim保留新阵列(i-1)
NewArray(i-1)=“”+ActiveDocument。段落(i)+“”
其他的
如果结束
接下来我
对于NewArray中的每个标题
打印#文本文件、标题
下一个
关闭文本文件
端接头
这是我从微软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 & ">\1</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
与:


太神了我忘了提到一些行有粗体字,我需要添加标记,那么找出它们的诀窍是什么呢?假设您在标题中提到粗体字,请参阅更新的答案。
  .Text = "([!^13]{1,})"
  .Text = "[!^13]@^t([!^13]{1,})"