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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 DulicIT ArchitectCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.