Consulting

Results 1 to 16 of 16

Thread: I need some help with Date Coding ?

  1. #1

    I need some help with Date Coding ?

    HI guys - got a 2 part question.

    Part A:
    I've got a form letter (word doc) which I am using to merge an Access db with.

    October 15, 2005



    Mr. Joe Smith
    ABC Hospital, Inc.
    123 Easy Lane
    Anytown, MI 48231

    On page 2 & 3, I had inserted two bookmarks. At bookmark #1, is the current date at top of letter plus 14 days; at bookmark #2, is the current date plus 35 days.

    Now i've got a small tiny problem because the letters are not always going out that same day. Sometimes I have to wait upon information from other people before mailing them out. The VBA code I had is no longer meeting my needs because I was using an an AUTO insert field at the top of the letter and based on that date, the VBA was calculating dates for bookmark #1 and #2 accordingly.

    Now being the smart people you guys (& gals) are, I know you're sitting there wondering well why don't you just merge the documents again on the day the letters finally get mailed? The answer is that after I merge all the records, I customize each letter deleting certain paragraphs and adding certain sentences. I don't want to have to do that for each batch of 20-30 letters. It would be better for me to save the merged doc. say for ex: "Merged Documents Batch Run_A.doc" and then edit the dates accordingly.

    How do you tell VBA to:

    At bookmark #1: Insert whatever date that I type at the top of the letter.
    At bookmark #2: plus 14 days
    At bookmark #3: plus 35 days

    Part B:
    The merged doc. might have anywhere from 5 to 20 addressee's. So for example, the first merged record might be Mr. Joe Smith at ABC Hospital. I want VBA to create a subfolder under "C:\Letters Sent\" as "ABC Hospital" and then save his letter there. How can you get VBA to save each portion of the merged document and save it separately rather than the entire document?
    Last edited by JohnnyBravo; 10-15-2005 at 07:22 PM. Reason: Added further infromation.

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Johnny,

    How, exactly, are you creating your dates? If the "current date" is a date field, and the other two are in (calculated) fields based on it, Word should be able to sort it out. If you do the calculations up front, of course, you'll have to do them again.

    I'm not a mail merge expert so I don't know about the second question. I suspect you'll need special code to split out the sections from the merged document. I'll have a play later unless someone comes back with an easy way of doing it first.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Johny,

    I'm not sure this is the total sollution for you cause I don't know if your dates are bookmarked (Range) or the bookmark is in front of it.

    If they are range bookmarked then something like this could work:[vba]
    Sub myDates()
    Dim dDate As Date
    dDate = CDate(InputBox("The date", "Provide Date", Format$(Now, "DD MM YYYY")))

    With ActiveDocument
    .Bookmarks("bmDate").Range.Text = Format$(dDate, "DD MM YYYY")
    .Bookmarks("bmDate2").Range.Text = Format$(DateAdd("d", 14, dDate), "DD MM YYYY")
    .Bookmarks("bmDate3").Range.Text = Format$(DateAdd("d", 35, dDate), "DD MM YYYY")
    End With

    End Sub
    [/vba]

    Of course a field suggestion like Tony says would be possible of course depending on your document.

    Btw...keep in mind that my date format (Holland) is "DD MM YYYY" you should change that to your own dateformat.

    HTH.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    This page might help with the split and save part of your question:
    http://www.gmayor.com/individual_merge_letters.htm
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Quote Originally Posted by gmaxey
    This page might help with the split and save part of your question:
    Thanks for that link. I tried to run it but I get an error at the highlighted line - any clue as to why?? All I did was simply copy and paste exactly as he has it on his website.
    [VBA]
    Sub SplitMerge()
    ' splitter Macro
    ' Macro created 16-08-98 by Doug Robbins to save each letter created by a
    ' mailmerge as a separate file.
    ' with modifications by Graham Mayor 16-06-03 & 08-10-04

    Dim Title As String
    Dim Default As String
    Dim MyText As String
    Dim MyName As Variant
    Dim MyPath As String

    Selection.EndKey Unit:=wdStory
    Letters = Selection.Information(wdActiveEndSectionNumber)
    Selection.HomeKey Unit:=wdStory
    Counter = 1

    Default = "Merged"
    MyText = "Enter a filename. Long filenames may be used."
    Title = "File Name"
    MyName = InputBox(MyText, Title, Default)
    If MyName = "" Then
    End
    End If

    Default = "D:\My Documents\Test\"
    Title = "Path"
    MyText = "Enter path"
    MyPath = InputBox(MyText, Title, Default)
    If MyPath = "" Then
    End
    End If

    While Counter < Letters
    Application.ScreenUpdating = False
    Docname = MyPath & LTrim$(Str$(Counter)) & " " & MyName

    ActiveDocument.Sections.First.Range.Cut
    Documents.Add
    With Selection
    .Paste
    .EndKey Unit:=wdStory
    .MoveLeft Unit:=wdCharacter, Count:=1
    .Delete Unit:=wdCharacter, Count:=1
    End With
    ActiveDocument.SaveAs FileName:=Docname, _FileFormat:=wdFormatDocument
    ActiveWindow.Close
    Counter = Counter + 1
    Application.ScreenUpdating = True
    Wend
    End Sub
    [/VBA]

  6. #6
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Here's another that might help:
    http://vbaexpress.com/kb/getarticle.php?kb_id=139
    ~Anne Troy

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    All I can suggest is that there is no "_" character following the comma in that line of code that Graham has posted. Have you tried removing that?
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    Quote Originally Posted by gmaxey
    All I can suggest is that there is no "_" character following the comma in that line of code that Graham has posted. Have you tried removing that?
    Yes. And still no go.

    Steamboat, i'll take a look at that link shortly after I try something. Thanks.

    Edit: Steamboat - that did the trick. Thank you - but how do I change the directory to which the individual files are saved to??

  9. #9
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Steamboat???

    Are you related to Dave Hawley?? He calls me Tuggy.
    ~Anne Troy

  10. #10
    Quote Originally Posted by Dreamboat
    Steamboat???

    Are you related to Dave Hawley?? He calls me Tuggy.
    Oh bazookas....I'm sorry about that Dreamboat. My brain is fried today from wrestlin with this code all day. My mind was somewhere else when I typed that.

    Got any ideas on how I can change the directory?

  11. #11
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    LOL, no problem. I should say something now, like "No wonder you need help with date coding.

    Have you searched the KB? There's all kinds of code in there. And don't discount, for instance, Excel code 'cause the syntax and lots of code will be the same, just the objects change. Here's the KB: www.vbaexpress.com/kb

    Like the forum, you only need to log in the first time to be able to search it as long as you're a forum member. I'll go now to look, too.
    ~Anne Troy

  12. #12
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Johnny,

    Anne asked me to take a quick look, and I don't have much time here. There error you are getting... do you actually have a directory at "D:\My Documents\Test\"?

    If not, change this line:
    [vba]
    Default = "D:\My Documents\Test\"[/vba]

    This actually passes a path to an input box as a default, but update everything between the quotes to your path.

    Sorry if I missed the point. I'll check back in a bit.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  13. #13
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Oh! And Greg is abosultely right. You need to lose the _ in that line:

    [vba]
    ActiveDocument.SaveAs FileName:=Docname, FileFormat:=wdFormatDocument[/vba]
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  14. #14
    Quote Originally Posted by kpuls
    Oh! And Greg is abosultely right. You need to lose the _ in that line:
    That's weird - earlier tonight I did remove the underscore in that line and it wouldnt' work - but now all of a sudden it does! Go figure. Cool - now I got 2 different ways to split those merged documents. I appreciate everyone's help here - you guys are real helpful. I'm hittin the sack - this boy is dead tired. Thanks all.

  15. #15
    VBAX Regular
    Joined
    Sep 2004
    Posts
    65
    Location
    DOn't know if your date problem has been solved but, If the prob is re-calculating the date for when the letters are sent. Assuming you print them on the day then how about a customized Print command that updates the bookmarks before sending to the printer?

  16. #16
    Quote Originally Posted by Bilby
    DOn't know if your date problem has been solved but, If the prob is re-calculating the date for when the letters are sent. Assuming you print them on the day then how about a customized Print command that updates the bookmarks before sending to the printer?
    Hey that's totally cool - yes I would like something like that - but I've got bigger fish to fry - this RFI project is so manual intensive... there's so many other steps involved here that If i were to explain all the steps involved, the scroll wheel on your mouse might wear out! :lol:

Posting Permissions

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