【发布时间】:2015-07-28 14:28:41
【问题描述】:
我有一个包含标记的文本块的 Word 文件,即:
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.
我需要我的 Excel 宏来查找指定标志“Text X”和“Test X End”之间的文本块,然后复制文本(不包括标志)并将其粘贴到第二个 Word 文档中指定位置。
我已尝试调整堆栈交换上发布的许多解决方案,但无法编译和/或运行这些解决方案,因为大多数人似乎认为我是从 Word 而不是 Excel 中编写宏。
我在 Excel 中工作的原因是我正在向通过 Excel 界面访问的现有 Excel 宏套件添加功能。
代码的当前状态是:
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
目前这会在以下行停止编译:
{If rng1.Find.Execute(FindText:="Text 2") Then}
突出显示“.Find”并显示“Argument not Optional”错误消息
【问题讨论】:
-
添加一些代码,这样我们就可以看到你已经走了多远。另外,请解释为什么要在 Excel 中运行它。
-
根据您的要求添加代码