|
回答がつかず、今更ですが、自分の勉強として書いてみました。
直接、新規ファイルに抽出しています。
キーワードに挟まれた部分にブックマークをつけ
エクセルA列セルの値の順番通りに、コピペです。
Sub test()
Dim myDoc As Document
Dim b As Bookmark
Dim myRng As Range
Dim n As Long
Const myKey1 As String = "【はじめ】"
Const myKey2 As String = "【おわり】"
Set myDoc = ActiveDocument
Set myRng = myDoc.Range(0, 0)
With myRng.Find
.Text = myKey1 & "*" & myKey2
.MatchWildcards = True
Do While .Execute
n = n + 1
myRng.Bookmarks.Add Name:="BM" & n
Loop
End With
Dim xlApp As New Excel.Application
Dim myBook As Excel.Workbook
Dim c As Excel.Range
Dim s As String
xlApp.Visible = True
Set myBook = xlApp.Workbooks.Open("C:\****\****\****.xls")
Application.Documents.Add
With myBook.worksheets("Sheet1")
For Each c In .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
If myDoc.Bookmarks.Exists(c.Value) Then
myDoc.Bookmarks(c.Value).Range.Copy
Selection.Paste
Selection.TypeParagraph
End If
Next
For Each b In ActiveDocument.Bookmarks
b.Delete
Next
End With
myBook.Close False
xlApp.Quit
Set myBook = Nothing
Set xlApp = Nothing
For Each b In myDoc.Bookmarks
b.Delete
Next
End Sub
|
|