[英]add excel range after specific text in word
從 excel 中,我想使用宏將單元格范圍復制到現有 word 文檔中的特定 position。 position 應該在該 word 文檔中的唯一單詞之后。
以下是 2 個文件: https://1drv.ms/u/s?AoL8lLA69BSAiu0z6xdsvRK7IzyC9A?e=R8E5ex (帶有和不帶宏的 Excel 表)
這是 excel 宏:
Sub test()
'
' test Macro
'
' Sneltoets: Ctrl+Shift+P
Worksheets("artikelen").Range("A2:I6").Copy
Dim wdapp As Object, wddoc As Object
Dim strdocname As String
On Error Resume Next
Set wdapp = GetObject(, "Word.Application")
If Err.Number = 429 Then
Err.Clear
Set wdapp = CreateObject("Word.Application")
End If
wdapp.Visible = True
strdocname = "C:\temp\test1.docx"
If Dir(strdocname) = "" Then
MsgBox "The document " & strdocname & vdCrLf & " is not found at the defined location.", vbExclamation
Exit Sub
End If
wdapp.Activate
If wddoc Is Nothing Then
Set wddoc = wdapp.Documents.Open(strdocname)
End If
Set myRange2 = wddoc.Content
myRange2.Find.Execute FindText:="searchtext"
wddoc.Range.Paste
wddoc.Save
' wdapp.Quit
Set wddoc = Nothing
Set wdapp = Nothing
Application.CutCopyMode = False
End Sub
++++++++
唯一不起作用的是在我的word-doc中的單詞:'searchtext'之后粘貼excel范圍。 這是一段代碼:
Set myRange2 = wddoc.Content
myRange2.Find.Execute FindText:="searchtext"
wddoc.Range.Paste
...那不工作
我覺得我有 99% 在那里......任何幫助將不勝感激。 提前致謝。 rgrds,理查德
您的代碼幾乎是正確的。
當您粘貼到作為整個文檔的wddoc.Range
,粘貼無法正常工作。 您應該粘貼到myRange2
中,但前提是找到您要搜索的文本。
幸運的是,成功的查找會將myRange2
設置為找到的文本的范圍。 如下更改您的代碼應該使粘貼適合您:
If myRange2.Find.Execute(FindText:="searchtext") Then
myRange2.Collapse wdCollapseEnd
myRange2.Paste
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.