Results 1 to 5 of 5
  1. #1
    Star Lounger
    Join Date
    May 2003
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Set Format for TxtBox that contains a date (VBA/Word 2003)

    I have a very basic memo template that populates a table in Word with memo data. Then, I have the recipient name and date on second page header.
    How do I add a date switch to a bookmark reference? I usually use doc variable fields and format the field itself but can't figure out how to do the same with a bookmark .
    I have the following code and don't know what I'm doing wrong.
    Selection.GoTo what:=wdGoToBookmark, Name:="Date"
    Selection.TypeText frmSettings.txtDate.Text
    txtDate = Format(Now(), "mmmm d, yyyy")
    Next item up, how can I reference the same bookmark in the 2nd page header? Herein lies the reason I always go back to Doc Variables!!
    Thanks!

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Set Format for TxtBox that contains a date (VBA/Word 2003)

    Your code does not set the text of the bookmark. If the bookmark already contained text, it is overwritten (i.e. the bookmark disappears), and if the bookmark was an insertion point, the text is inserted after the bookmark.
    Moreover, you set txtDate after inserting the value of txtDate into the document...

    See Inserting text at a bookmark without deleting the bookmark

    You can add a date switch to a bookmark reference:

    { REF Date @ "M/d/yy" }

    The { } are field braces, you shouldn't type them yourself. To do this in VBA:
    <code>
    ActiveDocument.Fields.Add Selection.Range, , "REF Date @ ""M/d/yy"""
    </code>
    Note the use of double double quotes within the field text.

  3. #3
    Star Lounger
    Join Date
    May 2003
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Set Format for TxtBox that contains a date (VBA/Word 2003)

    Thanks Hans! The reference on creating an enclosed bookmark was exactly what I needed.
    But, I'm still not sure about the date reference. I am trying to define the switch on the date bookmark. The following code encloses the date bookmark so i can use the ref field date switch in the header of the document, but not sure where to define the switch in the VBA code.

    Selection.GoTo what:=wdGoToBookmark, Name:="Date"
    Set BMRange = ActiveDocument.Bookmarks("Date").Range
    BMRange.Text = frmKHMemo.txtDate.Text
    ActiveDocument.Bookmarks.Add "Date", BMRange

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Set Format for TxtBox that contains a date (VBA/Word 2003)

    The bookmark itself doesn't have a format switch - it isn't a field. The text of the bookmark is just the literal text that you set it to, so if you want to use a specific date format, use

    BMRange.Text = Format(frmKHMemo.txtDate.Text, "mmmm, d yyyy")

    or similar.

  5. #5
    Star Lounger
    Join Date
    May 2003
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Set Format for TxtBox that contains a date (VBA/Word 2003)

    Perfect! Hans, you Rock!!
    One of these days I'll actually learn VBA!
    Have a good day.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •