有没有一种方法可以在Word中运行此宏,而无需Word打开其他窗口并闪烁 [英] Is there a way to run this macro in Word, without Word opening other windows and flashing

查看:161
本文介绍了有没有一种方法可以在Word中运行此宏,而无需Word打开其他窗口并闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个测试示例,您需要在Word中进行标记,以便粗体字具有样式"heading1".

Here is a test example you will need to markup in Word, so that the bold words have the style "heading1".

该宏会将标题处的文档拆分为单个.rtf文件,这些文件将使用粗体标题作为文件名+ .rtf扩展名.

The macro will split the document at the headings into individual .rtf files that will use the bold heading as the filename + .rtf extension.

帽子

此处的文字段落

perisseia

此处的文字段落

perisseuma

此处的文字段落

Sub SplitDocOnHeading1ToRtfWithoutHeadingInOutput()
'Splits the document on Heading1 style, into new documents, Heading1 is  included in the data.



Application.ScreenUpdating = False
Dim Rng As Range, DocSrc As Document, DocTgt As Document
Dim i As Long, StrTxt As String: Const StrNoChr As String = """*/\:?|"
Set DocSrc = ActiveDocument
With DocSrc.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = True
    .Forward = True
    .Text = ""
    .Style = wdStyleHeading1
    .Replacement.Text = ""
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found
    Set Rng = .Paragraphs(1).Range
    Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
    Set DocTgt = Documents.Add(DocSrc.AttachedTemplate.FullName)
    With DocTgt
    Application.ScreenUpdating = False
      .Range.FormattedText = Rng.FormattedText
      StrTxt = Split(.Paragraphs.First.Range.Text, vbCr)(0)
      ' Strip out illegal characters
      For i = 1 To Len(StrNoChr)
        StrTxt = Replace(StrTxt, Mid(StrNoChr, i, 1), "_")
      Next
      .Paragraphs.First.Range.Delete
      .SaveAs2 FileName:=DocSrc.Path & "\" & StrTxt & ".rtf", Fileformat:=wdFormatRTF, AddToRecentFiles:=False
      .Close False
    End With
    .Start = Rng.End
    .Find.Execute
  Loop
End With
Set Rng = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
Application.ScreenUpdating = True
End Sub

推荐答案

您真正需要做的就是更改:

All you really need do is change:

Set DocTgt = Documents.Add(DocSrc.AttachedTemplate.FullName)

收件人:

Set DocTgt = Documents.Add(Template:=DocSrc.AttachedTemplate.FullName, Visible:=False)

通过以下方式隐藏字词:

Hiding Word via:

With Word.Application
    .Visible = False

是有风险的-如果有任何问题,您可能最终在后台运行一个不可见的Word会话,并使文档保持打开状态.然后,您需要使用任务管理器杀死Word-然后尝试恢复您的工作.

is risky - If anything goes wrong you may end up with an invisible Word session running in the background and keeping your document(s) open. You'd then need to use Task Manager to kill Word - and then try to recover your work.

这篇关于有没有一种方法可以在Word中运行此宏,而无需Word打开其他窗口并闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆