VBA从另一个Word文档中的指定位置复制到Word中的一个文档(包括)
VBA copying to one document in Word from specified place in another Word document (including)
我正在尝试将文本的准确部分从一个 Word 文档复制到另一个文档。这是一个文本示例:
——————————————————
关于公司
Bla bla bla bla
Bla bla bla
啦啦啦
谢谢关注
————————————————————-
假设文本位于单词的末尾。
所以我想复制从“关于公司”到“感谢您的关注”的整个文本,包括两者。
我下面的代码只复制了“关于公司”和“感谢您的关注”之间的内容,但我需要它们也被复制(请不要建议添加额外的词来让代码找到它们,这是不可能的案件)。有什么想法吗?
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
提前致谢!
如果我没理解错的话,您希望在最终处理的范围内包含第一个搜索文本“关于公司”和第二个搜索文本“感谢您的关注”。
您当前的代码在第一次查找后过早地折叠 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
真的,您只需要 通配符 查找,其中:
查找=关于公司*谢谢关注
您甚至不需要宏!也就是说:
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
我正在尝试将文本的准确部分从一个 Word 文档复制到另一个文档。这是一个文本示例:
——————————————————
关于公司
Bla bla bla bla
Bla bla bla
啦啦啦
谢谢关注
————————————————————-
假设文本位于单词的末尾。 所以我想复制从“关于公司”到“感谢您的关注”的整个文本,包括两者。 我下面的代码只复制了“关于公司”和“感谢您的关注”之间的内容,但我需要它们也被复制(请不要建议添加额外的词来让代码找到它们,这是不可能的案件)。有什么想法吗?
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
提前致谢!
如果我没理解错的话,您希望在最终处理的范围内包含第一个搜索文本“关于公司”和第二个搜索文本“感谢您的关注”。
您当前的代码在第一次查找后过早地折叠 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
真的,您只需要 通配符 查找,其中:
查找=关于公司*谢谢关注
您甚至不需要宏!也就是说:
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