通过Excel循环,在Word中键入值,粘贴Excel字符串
问题描述:
我想通过Excel循环,其中列A保存我想要在Word中找到的文本。 B列在找到文本的段落结束后保留我想要粘贴的内容。通过Excel循环,在Word中键入值,粘贴Excel字符串
在Word VBA中工作时,查找文本正在工作并移动到段落结尾。但是当我移动到Excel VBA时,find方法似乎没有做任何事情。
Sub UpdateWordDoc1()
Dim mywb As Excel.Worksheet
Set mywb = ActiveWorkbook.ActiveSheet
Dim wdDoc As Object, wdApp As Object
Dim questiontext As String
Dim oSearchRange
On Error Resume Next
Set wdDoc = CreateObject("C:\mydoc.docx")
Set wdApp = wdDoc.Application
Set oSearchRange = wdDoc.Content
With mywb
For i = 2 To .Range("A6000").End(xlUp).Row
questiontext = .Range("A" & i).Value
.Range("B" & i).Copy
Set blabla = oSearchRange.Find.Execute.Text = questiontext
blabla.Select
Selection.movedown unit:=wdparagraph
Selection.moveleft unit:=wdcharacter
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Next i
End With
'wdDoc.Close savechanges:=True
Set wdDoc = Nothing
Set wdApp = Nothing
End Sub
答
我认为这段代码完成了你的工作。我在原帖中对代码进行了一些小的修改,其中一些很重要,有些不太重要。希望这些意见有助于解释为什么我做了什么,我所做的:
Sub UpdateWordDoc1()
' REQUIRES A REFERENCE TO:
' Microsoft Word ##.# Object Library
Dim myws As Excel.Worksheet ' Changed wb to ws to better abbreviate worksheet
Dim wdDoc As Word.Document ' No longer a generic object
Dim wdApp As Word.Application ' No longer a generic object
Dim questiontext As String
Dim oSearchRange As Word.Range ' Word range is what will be searched
Dim i As Long ' Loop through rows by count (Long)
Set myws = ActiveWorkbook.ActiveSheet
' On Error Resume Next ' Can't find bugs if they're supressed!!!
Set wdApp = CreateObject("Word.Application") ' Create app before opening doc
' Need to explore what happens
' if Word is already running
wdApp.Visible = True ' Make it visible so we can watch it work
Set wdDoc = wdApp.Documents.Open("C:\mydoc.docx") ' Open the doc
With myws
For i = 2 To .Range("A6000").End(xlUp).Row
' Word's Find function is tricky to program, because
' when Find succeeds, the range is moved! (Find has many
' other odd behaviors). Assuming you want to search the entire doc
' for each search term, we reset the range every time through the
' loop.
Set oSearchRange = wdDoc.Content
questiontext = .Range("A" & i).Value
.Range("B" & i).Copy
' Set blabla = oSearchRange.Find.Execute.Text = questiontext
With oSearchRange.Find
' Note that Word's Find settings are "sticky". For example, if
' you were previously searching for (say) italic text before
' running this Sub, Word may still search for italic, and your
' search could fail. To kill such bugs, explicitly set all of
' Word's Find parameters, not just .Text
.Text = questiontext ' This is what you're searching for
If .Execute Then ' Found it.
' NOTE: This is only gonna make a change
' at the first occurence of questiontext
' When find is successful, oSearchRange will move
' to the found text. But not the selection, so do Select.
oSearchRange.Select
' Now move to where the new text is to be pasted
wdDoc.ActiveWindow.Selection.movedown unit:=wdparagraph
wdDoc.ActiveWindow.Selection.moveleft unit:=wdcharacter
' While debugging, the next statement through me out of single
' step mode (don't know why) but execution continued
' and the remaining words in my list we're found and text
' pasted in as expected.
wdDoc.ActiveWindow.Selection.PasteAndFormat (wdFormatOriginalFormatting)
End If
End With
Next i
End With
' Clean up and close down
wdDoc.Close savechanges:=True
Set oSearchRange = Nothing
Set wdDoc = Nothing
wdApp.Quit
Set wdApp = Nothing
Set myws = Nothing
End Sub
希望帮助
+0
这太棒了!非常感谢你的帮助,它完美地工作。尽管如此,发生的一件小事是如果文档已经打开,程序就会“挂起”。如果你这样做,我找到了一个工作: 'Set wdDoc = CreateObject(“C:\ mydoc.docx”)' 'Set wdApp = wdDoc.Application' – strahanstoothgap
已添加到Word对象库的引用? Excel不知道(例如)'wdFormatOriginalFormatting'的值是... –
是的。参考在那里,代码运行良好。它只是没有做任何事情。我的直觉是它围绕着选择的东西。我不认为该计划正在将“主动”转移到Word,并允许它控制和查找问题文本,然后采取行动。但是,显然我不确定。当我遍历代码时,没有任何反应,例如,在我想要看到光标实际移动的移动或移动后。 – strahanstoothgap
代码'Selection.movedown'(和类似的东西)将操纵Excel的选择,而不是Word的。你可以通过使用'wdApp.Selection'或'wdDoc.ActiveWindow.Selection'或类似的东西来解决这个问题。 – xidgel