Grigorii
Grigorii

Reputation: 7

VBA copying to one document in Word from specified place in another Word document (including)

I am trying to copy exact part of text from one Word document to another. Here is an example of text:

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

About the company

Bla bla bla bla

Bla bla bla

Bla bla

Thank you for attention

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

Imagine that text is located in the end of the Word. So I want to copy the whole text from “About the company” till “Thank you for attention” including both. My code below copies only what is between “About the company” and “Thank you for attention” but I need them also to be copied (please don't suggest to add extra words to make the code find them, it is impossible in my case). Any ideas?

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

Thank you in advance!

Upvotes: 0

Views: 126

Answers (2)

macropod
macropod

Reputation: 13515

Really, all you need for this is a wildcard Find, where:

Find = About the company*Thank you for attention

You don't even need a macro! That said:

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

Upvotes: 1

Rich Michaels
Rich Michaels

Reputation: 1713

If I understand correctly you want to include the first search text "About the company" and the second search text "Thank you for attention" in the range you ultimately process.

Your current code is collapsing MyRange too soon after the first find and on the second find you are picking up the wrong ending address. I have made the modifications belong and it should now work as you desire it to.

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

Upvotes: 1

Related Questions