Excel VBA merge cell value to Word Bookmark keeping date format

Hi

I am using Excel VBA to merge a date in a cell
to a Word Bookmark.

The date format changes. Is there a way to keep it looking exactly the same as the viewer sees it in Excel?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
Saurabh Singh TeotiaCommented:
Mubro,

You can use something like this to format date in the manner you want to see..In this case i have assumed you want to format date in mm/dd/yyyy

Dim dt As Date
dt = Format(your date here, "mm/dd/yyyy")

Open in new window


Saurabh....
0
 
Rob HensonFinance AnalystCommented:
Would I be right to assume the value being exported to the Bookmark is the serial number value of the date, ie today would be 42123?

If so you will need to convert to text prior to export to the bookmark, for example:

=TEXT(TODAY(),"dd/mm/yyyy")

Thanks
Rob H
0
 
Haris DulicCommented:
Hi,

in VBA you can use function FORMAT and then merge it to other string..

Format(YOUR_DATE, "MM/dd/yyyy h:mm:ss tt")

Open in new window

0
 
GrahamSkanRetiredCommented:
If you do not always know what the formatting will be, you may have to get it from the cell. The shows how:
Option Explicit

Sub FillWordBookmark(wdDoc As Word.Document)

Dim dt As Date
Dim strNf As String
Dim strNumberFormatParts() As String
Dim strNumberFormat As String
Dim strDate As String

dt = ActiveSheet.Cells(2, 1).Value
strNf = ActiveSheet.Cells(2, 1).NumberFormat

strNumberFormatParts = Split(strNf, ";")
strNumberFormat = strNumberFormatParts(0)
strDate = Format(dt, strNumberFormat)
wdDoc.Bookmarks("MyDate").Range.Text = strDate

End Sub

Open in new window

0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thank you all very much. This is one of those questions where it is a pity to have to split  the points
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.