Date Format

E

Elaine J.

I have a form with a bookmark called currentdate. I want to use VBA to add
the current date in the format of September 27, 2007. Everything I have
tried has given me an error. Can someone help with the syntax on this?

Thanks for your help.
 
J

Jay Freedman

I have a form with a bookmark called currentdate. I want to use VBA to add
the current date in the format of September 27, 2007. Everything I have
tried has given me an error. Can someone help with the syntax on this?

Thanks for your help.

If the bookmark's name is myDate, this will do it:

ActiveDocument.Bookmarks("myDate").Range.Text = _
Format(now,"MMMM dd, yyyy")

If you want the bookmark to still be available afterwards, see
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.
 
G

Graham Mayor

If it is a form field that you want to insert the date into then

ActiveDocument.FormFields("myDate").Result _
= Format(Now, "MMMM dd, yyyy")

will do that


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
E

Elaine J.

Jay, thanks so much for your response. I just knew that was what I needed .
Unfortunately I put it in my code and I am getting the same error as before.
The error that I am getting is: Wrong number of arguments or invalid
property assignment. It stops with "Format" highlighted.

This is the part of the code that I thinks affects this:


Dim odoc As Word.Document
Set odoc = Documents("allnotices.doc")
odoc.Bookmarks("currentDate").Range.Text = FORMAT(Now, "MMMM dd, yyyy")

I have several other variables and bookmarks and they are all working OK.
But a simple date has got me stumped. Can you tell what I am missing from
this?

There is one issue that might also affect it. I am working with a document
called Allnotices. Under certain circumstances, another document is
inserted into my current document (thus becoming part of Allnotices.) That
inserted document is the one that has the bookmarks. Would the fact that the
second part of the document is not there when it compiles have any effect on
this? It does not seem to affect any of the other variables, but they are
all inputs of one sort or another. Hopefully I just don't have something
declared and it will be a simple fix. (Or maybe there is a better way to do
this. What I am trying to avoid is having the user input the date.)

Thanks for your help.
 
J

Jay Freedman

I don't believe the location of the bookmarks has anything to do with the
error. If VBA couldn't find the bookmark, you'd get a different error, "The
requested member of the collection does not exist".

The Format function is built into VBA. If you put the cursor on the word
Format in your code and press F1, you should get the Help topic about the
function, which shows the proper syntax. It has one required argument and
three optional ones, so passing the first two ("expression" and "format" in
the syntax statement, corresponding to Now and "MMMM dd, yyyy" in your code)
is completely OK.

What could be happening -- though it's a low probability -- is that you have
another Format function defined by some code in your template or a global
add-in, and VBA is looking at the argument list in that function's
definition. Does the word FORMAT really appear in all caps in your code? If
so, that's a clue that something like that is wrong, as the built-in
function's name is automatically changed to Format.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top