PDA

View Full Version : Word 2010 - VBA code for inputting dates



Mulverine
03-18-2015, 09:24 AM
Hello

I am trying to run a VBA Macro that searches a document for the word effdate and then asks the user to input a date (string) and then replaces the effdate word with the typed date.

This is what I have so far but it only highlights the word, asks for a date with a textbox but then does not replace it with the typed date.

Public Sub UserDate()
Dim strDate As String
strDate = InputBox("Insert date in format dd/mm/yy", "User date", Format(Now(), "dd/mm/yy"))
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Text = "effdate"
.Replacement.Text = "strDate"
.Execute
End With
End Sub

Any help is appreciated!

gmaxey
03-18-2015, 01:55 PM
Public Sub UserDate()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Forward = True
.Wrap = wdFindStop
.Text = "effdate"
.Replacement.Text = InputBox("Insert date in format dd/mm/yy", "User date", Format(Now(), "dd/mm/yy"))
.Execute Replace:=wdReplaceAll
End With
lbl_Exit:
Exit Sub
End Sub

Mulverine
03-19-2015, 02:28 AM
Amazing! Thank you! :) That is just what I am after.

Mulverine
03-19-2015, 03:16 AM
One more question if you please.

If I want to add this plus another step within the same macro to replace testdate after effdate how would I do this?

I also want this to be incorporated into another macro so is there anything I need to tailor for this to work?

Thanks a million

Mike

Edit: It's ok I've sussed it! :)