0
votes

I have a Word File containing flagged blocks of text viz:

Text 1
Text 1 Line 1
Text 1 Line 2
Text 1 End

Text 2
Text 2 Line 1 
Text 2 Line 2
Text 2 Line 3
     .
     .
Text 2 Line N
Text 2 End

etc.

I need my Excel Macro to find blocks of text between the specified flags "Text X" and "Test X End" then take a copy of the text (not including the flags) and paste it into a second Word Document at a specified location.

I've tried tweaking many of the solutions posted on stack exchange but have been unable to get these to compile and/or run as most seem to assume that I am writing my macro from within Word rather than Excel.

My reason for working within Excel is that I am adding functionality to an existing suite of Excel macros accessed via an Excel Interface.

Current State of the code is:

Sub Word_In_Word_Out()

Application.ScreenUpdating = False
Path = ThisWorkbook.Path
Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
Set inDoc = wrdApp.Documents.Open(Path & "\" & "Word Input" & ".docx")
inDoc.Activate

Dim rng1 As Range
Dim rng2 As Range
Dim strTheText As String

Set rng1 = ActiveDocument.Range
If rng1.Find.Execute(FindText:="Text 2") Then
    Set rng2 = ActiveDocument.Range(rng1.End, ActiveDocument.Range.End)
    If rng2.Find.Execute(FindText:="Text 2 End") Then
        strTheText = ActiveDocument.Range(rng1.End, rng2.Start).Text
        MsgBox strTheText
    End If
End If

End Sub

This currently stops compilation at the line:

{If rng1.Find.Execute(FindText:="Text 2") Then}

with ".Find" highlighted and an "Argument not Optional" error message

1
Add some code so we can see how far you've gotten. Also, please explain why you are running this in Excel. - ChipsLetten
Code added as per your request - Swifty

1 Answers

1
votes

Both Excel and Word have a Range object. Because you are in Excel VBA but are trying to reference the Word Range object you need to qualify the variable declaration so that Excel knows you are using a Word Range object.

Dim rng1 As Word.Range
Dim rng2 As Word.Range

You also do not need to use the ActiveDocument because you have already created a reference to the document using inDoc. So in your code replace ActiveDocument by inDoc.

Having made these changes, your code sample worked for me using a small test document.