【问题标题】:VBA copying to one document in Word from specified place in another Word document (including)VBA从另一个Word文档中的指定位置复制到Word中的一个文档(包括)
【发布时间】:2021-02-04 08:41:23
【问题描述】:

我正在尝试将文本的确切部分从一个 Word 文档复制到另一个文档。下面是一个文本示例:

——————————————————

关于公司

呜呜呜呜

呜呜呜

呸呸呸

感谢您的关注

——————————————————-

想象一下文本位于 Word 的末尾。 所以我想复制从“关于公司”到“感谢您的关注”的整个文本,包括两者。 我下面的代码只复制了“关于公司”和“感谢您的关注”之间的内容,但我也需要复制它们(请不要建议添加额外的词以使代码找到它们,这在我的案子)。有什么想法吗?

Dim Pos As Word.Document
Set Pos = Documents(1)
Set myRange = Pos.Content
Dim IngStart As Long 
Dim IngEnd As Long 

With myRange.Find 
    .ClearFormatting 
    .Wrap = wdFindStop
    .MatchCase = False 
    .Text = "About the company" 
        If .Execute = False Then 
            MsgBox "'About the company' not found.", vbExclamation 
            Exit Sub 
        End If 
    myRange.Collapse Direction:=wdCollapseEnd 
    IngStart = myRange.End 
    .Text = "Thank you for attention" 
        If .Execute = False Then 
            MsgBox "'Thank you for attention' not found.", vbExclamation 
            Exit Sub 
        End If 
    IngEnd = myRange.Start 
End With 

Pos.Range(lngStart, lngEnd).Copy 
    objWrdDoc.ContentControls(18).Range.PasteSpecial DataType:=2

提前谢谢你!

【问题讨论】:

  • 你在重新发明轮子!这段代码没有任何理由。您应该使用 AutoText/Building Blocks 来保存您的模式文本并从中插入。这是我关于插入自动图文集或构建块的文章。 addbalance.com/usersguide/… 要标记文档或位置中的文本,请使用书签。
  • 只需将起点移动您要搜索的文本中的字符数即可。所以“关于公司”是 17 个字符。然后将起点设为“IngStart = myRange.End - 17”。然后做同样的事情来抓取结束文本。
  • PeterT 提出了一个很好的解决方案

标签: vba ms-word


【解决方案1】:

真的,您只需要一个通配符查找,其中:

查找 = 关于公司*感谢关注

您甚至不需要宏!也就是说:

Sub Demo()
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = False
    .Forward = True
    .MatchWildcards = True
    .Wrap = wdFindContinue
    .Text = "About the company*Thank you for attention"
    .Execute
  End With
  If .Find.Found = True Then .Copy
End With
End Sub

【讨论】:

    【解决方案2】:

    如果我理解正确,您希望在最终处理的范围内包含第一个搜索文本“About the company”和第二个搜索文本“Thank you for attention”。

    您当前的代码在第一次查找后很快就崩溃了 MyRange,而在第二次查找时您选择了错误的结束地址。我已经进行了修改,它现在应该可以按照您的意愿工作了。

    Dim Pos As Word.Document
    Set Pos = Documents(1)
    Set myRange = Pos.Content
    Dim IngStart As Long
    Dim IngEnd As Long
    
    With myRange.Find
        .ClearFormatting
        .Wrap = wdFindStop
        .MatchCase = False
        .Text = "About the company"
            If .Execute = False Then
                MsgBox "'About the company' not found.", vbExclamation
                Exit Sub
            End If
        IngStart = myRange.Start
        myRange.Collapse Direction:=wdCollapseEnd
        .Text = "Thank you for attention"
            If .Execute = False Then
                MsgBox "'Thank you for attention' not found.", vbExclamation
                Exit Sub
            End If
        IngEnd = myRange.End
    End With
    
    Pos.Range(lngStart, lngEnd).Copy
        objWrdDoc.ContentControls(18).Range.PasteSpecial DataType:=2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多