PDA

View Full Version : format date in word 2013



dickep
12-29-2012, 08:51 PM
OK, I have the line in my vba module
mDate = Format(DateValue(Now()), "yyyymmdd")
but I get a compile error that says "Cannot find project or library" and cannot figure out what to do with this to fix it.

I tried the line
mDate = Format(Date, "yyyymmdd") but the error above was triggered by the Date word.

Thanks

macropod
12-30-2012, 01:09 AM
Try:
mDate = Format(Now(), "yyyymmdd")

gmaxey
12-30-2012, 07:57 AM
The problem here is not a missing library, but a type mismatch. "yyyymmdd" is not a valid date format. Try:

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim mDate As Date
mDate = Format(DateValue(Now), "yyyy mm dd")
'or just
mDate = Format(Now(), "yyyy mm dd")
MsgBox mDate
End Sub



OK, I have the line in my vba module
mDate = Format(DateValue(Now()), "yyyymmdd")
but I get a compile error that says "Cannot find project or library" and cannot figure out what to do with this to fix it.

I tried the line
mDate = Format(Date, "yyyymmdd") but the error above was triggered by the Date word.

Thanks

macropod
12-30-2012, 03:16 PM
The problem here is not a missing library, but a type mismatch. "yyyymmdd" is not a valid date format.
Hi Greg,

Whilst "yyyymmdd" is not a valid date format, it is a valid string or long variable. You have assumed mDate is intended to be a date format, but the OP hasn't said that's what mDate is.

dickep
12-30-2012, 04:05 PM
mDate is to be a string as it is to be appended to other string items to make up a file name.

macropod, your example did not work with the quotes but gave out the full time - hour included.

So am still having to figure this out.

P.S. this worked fine in Word 2003 but now 2013 broke something???

macropod
12-30-2012, 06:11 PM
macropod, your example did not work with the quotes but gave out the full time - hour included.
I don't have Word 2013 to play with but, in Word 2010, 'Format(Now(), "yyyymmdd")' returns '20121231' (without the quotes). That said, you may get better results with Format(Now(), "YYYYMMDD") as, 'hh', in particular might be interpreted as minutes.

gmaxey
12-30-2012, 07:07 PM
I have 2013 and all four of the following variations work just fine. Have you went to Tools>References and check if you have in missing references identified?

Sub ScratchMacro()
Dim mDate1 As String, mDate2 As String, mDate3 As String, mDate4 As String
mDate1 = Format(DateValue(Now()), "yyyymmdd")
MsgBox mDate1
mDate2 = Format(Now(), "yyyymmdd")
MsgBox mDate2
mDate3 = Format(DateValue(Now), "yyyymmdd")
MsgBox mDate3
mDate4 = Format(Now, "yyyymmdd")
MsgBox mDate4
End Sub

dickep
12-30-2012, 09:19 PM
I have not set up this for a while so was wondering what references could/should be checked.

Thanks

macropod
12-30-2012, 10:04 PM
You shouldn't need to set any references - the standard set (Office, VBA, Word) will do.

Are you sure the "Cannot find project or library" error applies to the mDate line - with Format(Now(), "yyyymmdd") and Format(Now(), "YYYYMMDD")?

gmaxey
12-30-2012, 10:18 PM
I agree with Paul. However, if wrong, the references checked in all new documents here (including the one I used to test your code) are:

OLE Automation
Visual Basic for Applications
MS Word 15.0 Object Library
MS Office 15.0 Object Library