Link to home
Start Free TrialLog in
Avatar of rdy123 rdy
rdy123 rdy

asked on

macro for active word document

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.

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
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rdy123 rdy
rdy123 rdy

ASKER

its working, thank you so much..
hi,

this is continuation of the above scenario,

i want to copy the whole content of word document to outlook,but the pathh = "C:\Templates\xxx.docx" is already opened by above code,i want to keep this open and run the below macro and copy the whole content to outlook,please suggest

the code which i run is the below,

Sub emailFromDoc()
    Dim wd As Object, editor As Object
    Dim doc As Object
    Dim oMail As Object
Dim objOutlook As Object

    Set wd = CreateObject("Word.Application")
    Set doc = wd.Documents.Open("C:\Templates\xxx.docx")
    doc.Content.Copy
    doc.Close
    Set wd = Nothing
  Set objOutlook = CreateObject("Outlook.Application")
     
    Set oMail = objOutlook.CreateItem(olMailItem)
    'Set oMail = Application.CreateItem(olMailItem)
    With oMail
        .HTMLBody = Replace(.HTMLBody, "<<Company1>>", CompanyName)
        Set editor = .GetInspector.WordEditor
        editor.Content.Paste
        .Display
    End With
End Sub

the document is already opened and want to copy the content to outlook. please suggest.thank you in advance.
You're welcome! Glad to help.
Please open a new question and post a link to this question there if required