VBA copy and replace to word (open word doc)

Largey980

New Member
Joined
Jun 28, 2016
Messages
17
Hi,

Im have been using below to update word docs, after modifying it to update an open word document rather than pointing to specific document anyone able to assist with conversion.
--------------------------------------------------------
Sub FindReplaceInWord()


Dim Wbk As Workbook: Set Wbk = ThisWorkbook
Dim Wrd As New Word.Application
Dim Dict As Object
Dim RefList As Range, RefElem As Range


Wrd.Visible = True
Dim WDoc As Document
Set WDoc = Wrd.Documents.Open("file path") 'Modify as necessary.


Set Dict = CreateObject("Scripting.Dictionary")
Set RefList = Wbk.Sheets("Sheet1").Range("C2:C16") 'Modify as necessary.


With Dict
For Each RefElem In RefList
If Not .Exists(RefElem) And Not IsEmpty(RefElem) Then
.Add RefElem.Value, RefElem.Offset(0, 1).Value
End If
Next RefElem
End With


For Each Key In Dict
With WDoc.Content.Find
If Dict(Key) = "" Then
.Execute FindText:=Key & "^p", ReplaceWith:=Dict(Key)
Else
.Execute FindText:=Key, ReplaceWith:=Dict(Key)
End If
End With
Next Key

--------------------------------------------------------
I have tried adding in........

Dim wdApp As Object


On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
On Error GoTo 0


If wdApp Is Nothing Then
MsgBox "No instances of Word found"
Exit Sub
End If



Always end up with run time error 424 at line "WDoc.Content.Find"

Any help always appreciated.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,215,046
Messages
6,122,854
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top