2015-10-06 114 views
0

我想通過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 
+0

已添加到Word對象庫的引用? Excel不知道(例如)'wdFormatOriginalFormatting'的值是... –

+0

是的。參考在那裏,代碼運行良好。它只是沒有做任何事情。我的直覺是它圍繞着選擇的東西。我不認爲該計劃正在將「主動」轉移到Word,並允許它控制和查找問題文本,然後採取行動。但是,顯然我不確定。當我遍歷代碼時,沒有任何反應,例如,在我想要看到光標實際移動的移動或移動後。 – strahanstoothgap

+0

代碼'Selection.movedown'(和類似的東西)將操縱Excel的選擇,而不是Word的。你可以通過使用'wdApp.Selection'或'wdDoc.ActiveWindow.Selection'或類似的東西來解決這個問題。 – xidgel

回答

0

我認爲這段代碼完成了你的工作。我在原帖中對代碼進行了一些小的修改,其中一些很重要,有些不太重要。希望這些意見有助於解釋爲什麼我做了什麼,我所做的:

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