PDA

View Full Version : Solved: Insert current date using preferred date format



hkeiner
11-01-2011, 02:47 PM
I wrote the below sub to paste the current date in to selected Word documents (as controlled by other VBA code). This sub works fine except for the date format of the text it pastes. It pastes the date in the PC's Windows (XP) date format and I would prefer that it paste the date it in a different format. Is there a way to change the below code so that I can control the date format that is pasted?


Example Windows date format: Nov/01/11
Example preferred date format: 11/01/11

Note: I know I can change the Windows date format setting via control panel but I do not want to do that. This sub will be used by several PC users and I can not chage/control their individual PCs' settings.





Public Sub InsertTodayDate_QAT()
Dim TodayDate As String
TodayDate = Date
Selection.InsertAfter TodayDate
End Sub

macropod
11-01-2011, 05:15 PM
Hi hkeiner,

You could, for example, use:
TodayDate = Format(Date, "dddd, d MMMM YYYY")

However, your code can be made much simpler:
Sub InsertTodayDate_QAT()
Selection.InsertAfter Format(Date, "dddd, d MMMM YYYY")
End Sub

hkeiner
11-02-2011, 09:47 AM
Your suggested code change worked great. Thanks.