code for active word document

rd123

New Member
Joined
Dec 15, 2016
Messages
10
hi,

i wrote a code in excel, where i want to replace few text with the excel.
now the issue is, i work on same file and want to replace the text once the word document is open instead of opening again and replace it.

please suggest.

below is the code.

Code:
Sub from()

Dim pathh As String
Dim pathhi As String
Dim oCell  As Integer
Dim WA As Object

pathh = "C:\Templates\xxx.docx"

Set WA = CreateObject("Word.Application")
WA.Documents.Open (pathh)
WA.Visible = True

WA.Selection.Find.ClearFormatting
WA.Selection.Find.Replacement.ClearFormatting
With WA.Selection.Find
        .Text = "abc"
        .Replacement.Text = Cells(Application.ActiveCell.Row, 15).Value
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    
     End With
WA.Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Sub sent()
Dim pathh As String
Dim pathhi As String
Dim oCell  As Integer
Dim WA As Object

pathh = "C:\Templates\xxx.docx"

Set WA = CreateObject("Word.Application")
WA.Documents.Open (pathh)
WA.Visible = True

WA.Selection.Find.ClearFormatting
WA.Selection.Find.Replacement.ClearFormatting
With WA.Selection.Find
        .Text = "dddd, mmmm dd, yyyy t:tt"
        .Replacement.Text = Cells(Application.ActiveCell.Row, 17).Value & " " & Cells(Application.ActiveCell.Row, 18).Value
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    
     End With
WA.Selection.Find.Execute Replace:=wdReplaceAll
End Sub


once the pathh = "C:\Templates\xxx.docx" is open,i want to keep this open and run the sent macro and replace the text,please suggest
 
Last edited by a moderator:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try:
Code:
Option Explicit
Dim WA As Object
Const pathh As String = "C:\Templates\xxx.docx"

Sub from()
If WA Is Nothing Then
  Set WA = CreateObject("Word.Application")
  WA.Visible = True
  WA.Documents.Open (pathh)
End If
Call FndRep("abc", Cells(Application.ActiveCell.Row, 15).Value)
End Sub

Sub sent()
If WA Is Nothing Then
  Set WA = CreateObject("Word.Application")
  WA.Visible = True
  WA.Documents.Open (pathh)
End If
Call FndRep("dddd, mmmm dd, yyyy t:tt", Cells(Application.ActiveCell.Row, 17).Value & " " & Cells(Application.ActiveCell.Row, 18).Value)
End Sub

Sub FndRep(StrFnd As String, StrRep As String)
With WA
  With .Activedocument.Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = StrFnd
    .Replacement.Text = StrRep
    .Forward = True
    .Wrap = wdFindAsk
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,803
Messages
6,174,687
Members
452,577
Latest member
Filipzgela

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