微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

检查每一行的 Word 文档样式

如何解决检查每一行的 Word 文档样式

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

from turtle import *
from random import *
import time

def randomcolour():

  colormode(255)
  red = randint(0,255)
  green = randint(0,255)
  blue = randint(0,255)
  color(red,green,blue)

def randomplace():
  penup()
  x = randint(-100,100)
  y = randint(-100,100)
  goto(x,y)
  pendown()

def randomheading():
  heading = randint(0,360)
  setheading(heading)

def drawrectangle():
  randomcolour()
  randomplace()
  hideturtle()
  length = randint(10,100)
  height = randint(10,100)
  begin_fill()
  forward(length)
  right(90)
  forward(height)
  right(90)
  forward(length)
  right(90)
  forward(height)
  right(90)
  end_fill()

shape("turtle")
speed(0)

for i in range(1,30):
  randomcolour()
  randomplace()
  randomheading()
  stamp()



def drawcircle():
  radius = randint(5,100)
  randomcolour()
  randomplace()
  dot(radius)

def drawstar():
  randomcolour()
  randomplace()
  randomheading()
  begin_fill()
  size = randint(20,100)

  for side in range(5):
    left(144)
    forward(size)

  end_fill()


clear()
setheading(0)

for i in range(20):
  drawrectangle()

clear()

for i in range(50):
  drawcircle()

clear()

for i in range(20):

  sleep(5000)
  drawstar()

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

enter image description here

解决方法

以下捕获所有标题并将它们保存到与活动文档同名的 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

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

如果您的标题有 #s 级并且您不希望在输出中使用这些,请替换:

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

与:

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

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

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

  ' do something here

End If

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。