Results 1 to 14 of 14
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Ohio, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Fields in a form

    I know how to put a field in my Word2000 doc to automatically print todays date. Is there an easy way to show todays date plus 6 months or other predetermined period of time?

    In other words, the document "date" field would be today, but further on in my document I want to list an expiration date of 6 months from "Today's date".

    If this could be in number of days or number of months, that would be the most helpful

  2. #2
    BAM
    Guest

    Re: Fields in a form

    If you want the date to be accurate then the best way is to use VBA and the DateAdd function.

    This article provides more information on DateAdd and how to format the result:
    <A target="_blank" HREF=http://www.mvps.org/word/FAQs/MacrosVBA/DateOfPrevMonth.htm>http://www.mvps.org/word/FAQs/Macros...fPrevMonth.htm</A>

    There are other methods out there, but from my own experience and tests I have found them to be fallible. Especially when it comes to a date that falls on the last day the month. So if you do use another method make sure you test this and include a test for leap year.
    ~~~~~~~~~~~~
    Cheers!

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Ohio, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fields in a form

    Thanks for the help. Why does everything have to be so difficult? You should be able to do "Today +180", but that would be too simple!

    Thanks again

  4. #4
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Ohio, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fields in a form

    I went to the link supplied and copied the prevmonth macro and with editing can make it go 'ahead'. But how can I tell it where to put it in my document. Right now it just dumps wherever my cursor is. Yes, I am new to VB!

  5. #5
    BAM
    Guest

    Re: Fields in a form

    It is that easy in Excel or Access, but not so simple in Word.

    Also note that in my reply I was referring to returring an accurate date, one that is exactly 6 months away. That is different than 180 days.

    For example six months from today is 11/2/2001, but 180 days is 10/29/2001.
    ~~~~~~~~~~
    Cheers!

  6. #6
    Star Lounger
    Join Date
    Jan 2001
    Location
    Adelaide, South Australia, Australia
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fields in a form

    To get the date where you want it, you can set a bookmark at that point, called say "SixMonthsHence" and go there before inserting the date.

    The VBA code to go to the bookmark is
    Selection.GoTo What:= wdGoToBookmark Name:="SixMonthsHence"

    Hope this helps

    Ian.

  7. #7
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Ohio, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fields in a form

    Yes it does. No it doesn't. I'm still learning VB, so I hope you understand my "greeness". Here's my code with your line copied in that doesn't work:
    -------------------------------------
    Sub Add_6_Months()
    Dim mbefore As Date
    mbefore = Format(DateAdd("m", 6, Date), "dd mmmm yyyy")
    Selection.InsertBefore mbefore
    wdGoToBookmark Name:="Six_Months"
    ----------------------------
    The "mbefore" and the rest of this code came from the web site referenced in a response above. I honestly don't understand the "Insert", as I would think that would do the insert.

    I've moved the statement you gave me around, but still get a "compile error, invalid use of property" error.

    More help please!

  8. #8
    Star Lounger
    Join Date
    Jan 2001
    Location
    Adelaide, South Australia, Australia
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fields in a form

    The following works for me. My apologies that I left a comma out of the line I sent before - I'm pretty new at this too!

    (You may want to format the date differently)

    Ian
    =================================

    Sub Add_6_Months()
    Selection.GoTo What:=wdGoToBookmark, Name:="Six_Months"
    Selection.InsertAfter DateAdd("m", 6, Date)
    End Sub

  9. #9
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Ohio, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fields in a form

    Cool! That works. I have three different date fields like that. Can I combine that into one macro?

    And can I make that macro run upon opening the form?

  10. #10
    BAM
    Guest

    Re: Fields in a form

    There are a couple methods you can use such as naming the macro AutoOpen or Document_Open().

    The one you need to use would be largely determined on whether or not using a template or a document.

    This article can help you determine which one to use and it provides the details on the "how tos":
    <A target="_blank" HREF=http://www.mvps.org/word/FAQs/MacrosVBA/DocumentEvents.htm>http://www.mvps.org/word/FAQs/Macros...mentEvents.htm</A>

    If you are new to macros I highly recommend this article as well:
    <A target="_blank" HREF=http://www.mvps.org/word/FAQs/MacrosVBA/CreateAMacro.htm>http://www.mvps.org/word/FAQs/Macros...eateAMacro.htm</A>

    It covers creating macros and contains links for:
    - Running a macro automatically when a document is created, opened or closed
    - Assigning it to a keyboard shortcut, toolbar, or a menu.
    - Intercepting events like Save and Print
    - Running a macro automatically when Word starts or quits
    - Creating a macro with no programming experience using the recorder
    - Getting To Grips With VBA Basics In 15 Minutes.
    ~~~~~~~~~~~
    Cheers!

  11. #11
    kelliel
    Guest

    Re: Fields in a form

    Yes and Yes.

    Insert three bookmarks where you want the information inserted.

    Open up the document you want this to be in. Go into the VB macro editor (you obviously have already been there as you have been modifying the code).

    In the Project Explorer window of the VB editor, which lists all the files you have open, expand the the project representing your form.

    In the "Microsoft Word Objects," double click on "ThisDocument."

    Paste this code in. (Notice it presupposes three bookmarks named certain names. Also be aware that if the bookmarks do not exist, the code will just jump over that part.)

    <pre>Private Sub Document_Open()

    'This supposes three bookmarks named:
    ' Six_Months1
    ' Six_Months2
    ' Six_Months3


    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Set rng1 = ActiveDocument.Bookmarks("Six_Months1").Range
    Set rng2 = ActiveDocument.Bookmarks("Six_Months2").Range
    Set rng3 = ActiveDocument.Bookmarks("Six_Months3").Range

    ' Setting the First Bookmark
    If ActiveDocument.Bookmarks.Exists("Six_Months1") Then
    rng1.Delete
    rng1.InsertAfter Format(DateAdd("m", 6, Date), "mmmm d, yyyy")
    rng1.Bookmarks.Add "Six_Months1"
    End If

    ' Setting the Second Bookmark
    If ActiveDocument.Bookmarks.Exists("Six_Months2") Then
    rng2.Delete
    rng2.InsertAfter Format(DateAdd("m", 6, Date), "mmmm d, yyyy")
    rng2.Bookmarks.Add "Six_Months2"
    End If

    ' Setting the Third Bookmark
    If ActiveDocument.Bookmarks.Exists("Six_Months3") Then
    rng3.Delete
    rng3.InsertAfter Format(DateAdd("m", 6, Date), "mmmm d, yyyy")
    rng3.Bookmarks.Add "Six_Months3"
    End If


    End Sub
    </pre>


    Save the document. You can see the attached document for exact placement of the macro. You will be better off copying the code from the attached document than from your browser screen.

    The main added step here is the reassigning of the bookmark to the date. In other words, if we did not reassign the bookmark (the part that states "Bookmarks.Add"), it would only work once. This way, every time you open the file, the dates will be updated.

    Now every time the document is opened, the dates will be updated to six months from today's date.

    Enjoy

  12. #12
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Ohio, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fields in a form

    Great resourses! Thanks. I got it working!
    The first link you gave me, in the upper section give specific instructions on "Two list boxes" I don't see those I'm running Office Prof2000.

    I got it to work using the second section on AutoMacros

    Thanks again!

  13. #13
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Ohio, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Fields in a form

    Lawerence, thanks for your great stuff too! I'll give it a rip!

  14. #14
    kelliel
    Guest

    Re: Fields in a form

    Thanks for asking. I had at work a procedure much like the one I wrote for you, however, I am at home. Therefore, I had to work out the steps myself. It was a great learning tool for me.

Posting Permissions

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