excel VBA to insert todays date in word

dean.rogers

New Member
Joined
Apr 6, 2012
Messages
31
Hey all,

What I would like to do is by use if excel VBA, I would like to have excel prompt word to enter in "todays date" where ever I have placed the respective variable. For instance, where ever I have [todays_date] located in my WordTemplate, then todays date will be inserted when I run my macro from excel. Below is the current code I have in place to map data from excel to word by finding a variable in word and replacing with a determined by a range in excel.

Code:
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
   
Set wrdDoc = wrdApp.Documents.Open("C:\Templates\WordTemplate.docx")

On Error Resume Next

With wrdDocSelection.Find
            .Text = "[excel_data]"
            .MatchWholeWord = False
            .Replacement.Text = ActiveWorkbook.Sheets("sheet1").Range("D3")
            .Execute , , , , , , , , , , wdReplaceAll

wrdApp.ActiveDocument.Save

I'm sure there is someway to do it but havent been able to find it anywhere.

Thank you for your help!!!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
This will search the entire Word doc including headers and footers
Code:
Sub doDate()

    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
       
    Set wrdDoc = wrdApp.Documents.Open("C:\xltest\WordTemplate.docx")
    
    On Error Resume Next
    Call FindAndReplace("[excel_date]", ActiveWorkbook.Sheets("sheet1").Range("D3"))
    wrdApp.ActiveDocument.Save

End Sub

Private Sub FindAndReplace(fndTxt As String, repTxt As String)
    Dim rngStory
    For Each rngStory In ActiveDocument.StoryRanges
        With rngStory.Find
            .Text = fndTxt
            .Replacement.Text = repTxt
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
    Next rngStory
End Sub
 
Upvote 0
Hey Warship thanks for your reply.

In my example below,

Code:
With wrdDocSelection.Find             
   .Text = "[excel_data]"             
   .MatchWholeWord = False            
   .Replacement.Text = ActiveWorkbook.Sheets("sheet1").Range("D3")             
   .Execute , , , , , , , , , , wdReplaceAll

I actually put excel DATA as the example for which I mapped data from excel to word.

What i was looking for is some code that basically decides what todays date is either based on the computer itself or Word determining that today is may 2nd, 2012 and then replacing every variable with [todays_date] in the WordTemplate

Any thoughts?

PS I do like your code and will test it out to see if it works well for me because the code i have currently takes up more space.
 
Upvote 0
Still using the FindAndReplace Sub posted previous.

Code:
Sub doDate()

    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
       
    Set wrdDoc = wrdApp.Documents.Open("C:\xltest\WordTemplate.docx")
    
    On Error Resume Next
    Call FindAndReplace("[todays_date]", Format(Date, "mm/dd/yyyy"))
    wrdApp.ActiveDocument.Save

End Sub

You can also do all at once:
Code:
Call FindAndReplace("[excel_data]", ActiveWorkbook.Sheets("sheet1").Range("D3"))
Call FindAndReplace("[todays_date]", Format(Date, "mm/dd/yyyy"))
 
Last edited:
Upvote 0
I just added the
Code:
Call FindAndReplace("[todays_date]", Format(Date, "mm/dd/yyyy"))</pre>
line of code to my existing code, however I got error "Sub or Function not defined" and it highlighted the code above.
 
Upvote 0
So I just applied some of your code and was successfully able to end up with todays date with this code

Code:
 With wrdDocSelection.Find
            'todays_date
            .Text = "[todays_date]"
            .MatchWholeWord = False
            .Replacement.Text = Format(Date, "mm/dd/yyyy")
            .Execute , , , , , , , , , , wdReplaceAll
 
Upvote 0
I just added the
Code:
Call FindAndReplace("[todays_date]", Format(Date, "mm/dd/yyyy"))</pre>
line of code to my existing code, however I got error "Sub or Function not defined" and it highlighted the code above.

its looking for:
Code:
Private Sub FindAndReplace(fndTxt As String, repTxt As String)
    Dim rngStory
    For Each rngStory In ActiveDocument.StoryRanges
        With rngStory.Find
            .Text = fndTxt
            .Replacement.Text = repTxt
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
    Next rngStory
End Sub

And has to be in the Same Module or try delete "Private"
 
Upvote 0
Oh that makes total sense. Not sure why I didn't catch that. Well you have helped tremendously. I will be testing out your code to see if it formats the way that I need because it is much cleaner than the syntax that I am currently using. Thanks for your help!!!
 
Upvote 0
You're quite welcome.. Anytime
 
Upvote 0

Forum statistics

Threads
1,217,908
Messages
6,139,333
Members
450,194
Latest member
AmateurHour

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