Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Apr 2001
    Posts
    304
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Specifying insertion location in a macro (Word 2003)

    I'd like to create a macro to insert the current date in the second cell upon opening. I know how to create a macro to insert the date into whatever the current cursor position is, but I'm not sure how to specify to insert this into that second cell. Also, I was hoping to put fill-in fields in the "details" section (the main part) of this form. However, I don't know of any way to make a field wrap down to the next cell when you run out of typing room. And it looks funny when you have three lines of detail and Word changes the height of the row to accomodate. Take a look and see if you have any suggestions.
    Attached Files Attached Files

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Specifying insertion location in a macro (Word 2003)

    <P ID="edit" class=small>(Edited by jscher2000 on 25-Mar-04 14:52. Inserted second example; edited third example.)</P>Here's one method, it selects the second cell in the first row of the first table in the document, then collapse the selection to the beginning of the cell, then uses your recorded code to insert the date:

    <pre>ActiveDocument.Tables(1).Cell(Row:=1, Column:=2).Select
    With Selection
    .Collapse Direction:=wdCollapseStart
    .InsertDateTime _
    DateTimeFormat:="M/d/yyyy", InsertAsField:=False, _
    DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
    InsertAsFullWidth:=False
    End With</pre>

    If you want to clear out whatever is in the cell, rather than collapse the selection, you could just "back it off" one character so that the end-of-cell marker is not selected. The insertion should replace whatever is selected in the cell:
    <pre>ActiveDocument.Tables(1).Cell(Row:=1, Column:=2).Select
    With Selection
    .MoveEnd Unit:=wdCharacter, Count:=-1
    .InsertDateTime _
    DateTimeFormat:="M/d/yyyy", InsertAsField:=False, _
    DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
    InsertAsFullWidth:=False
    End With</pre>


    A third approach is to not use the selection object at all. This will leave the placement of the insertion point undisturbed, but it is more complicated to code up. This is one example:

    <pre>Dim rngTemp As Range
    Set rngTemp = ActiveDocument.Tables(1).Cell(Row:=1, Column:=2).Range

    With rngTemp
    .MoveEnd Unit:=wdCharacter, Count:=-1
    .InsertDateTime _
    DateTimeFormat:="M/d/yyyy", InsertAsField:=False, _
    DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
    InsertAsFullWidth:=False
    End With
    Set rngTemp = Nothing</pre>

    Hope this helps.

  3. #3
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Specifying insertion location in a macro (Word 2003)

    I don't know how to wrap as you're creating the text in the cell (although I don't think it looks so bad).

    However, for an "after the fact" solution (once all is done), you can:
    - select the cell (I think you don't want the end of cell marker so Jefferson's 2nd example to back off of the marker is needed)
    - use the Tools | Word Count to find the number of lines (I think you can get the info inside VBA)
    - set up a loop that goes thru the lines less 1:
    --inserts a row below the one you're in (at least this is available with XP; the algorithm can be modified appropriately if you can only insert above)
    --selects the last line
    --cuts it and pastes it in the row below (the one you just inserted)

    ** Unless I'm missing something, I didn't see a line property for a selection (there's a word and character count but not for lines). So have to modify above to proceed line by line of the selection. When all is done, you may end up with a blank row, depending on how this is coded if you moved the 1st line down to a new row.

    Your VBA skills seem superior to mine (which are pretty much non existent in Word) so I won't even try to create VBA for this.

    Fred

  4. #4
    3 Star Lounger
    Join Date
    Apr 2001
    Posts
    304
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Specifying insertion location in a macro (Word 2003)

    Hey guys - thank you both for your input. I think that, for now, I'm not going to worry about the text wrapping and adjusting the row height. I'm probably being to anal about that little factor.

    The insert date works great, and if I put it in an AutoOpen macro, then it runs automatically. I have one more question, though. I wanted to put a Fill In field beside the Name box, and I wanted the user to be prompted when they opened the file to enter their name. I believe that if I put a Refresh code in the AutoOpen macro, this will accomplish this ... but my VBA stinks and I'm not sure of the syntax (and not sure where to put it in relation to the InsertDate part of the AutoOpen macro). I did this before, eons ago, and have just forgottten how. Can you help?

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Specifying insertion location in a macro (Word 2003)

    They need to be reminded to enter their name? What kind of users are these? <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

    I'm not very familiar with the FILLIN field code, but if you want to refresh fields, there are a variety of ways to do that. I don't have time to work on the code, but if you search here you probably will find some. Key words would include update, which is the most probable method name for this. Maybe you can update all your fields in one go with something like ActiveDocument.Fields.Update???

  6. #6
    Lounger
    Join Date
    Mar 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Specifying insertion location in a macro (Word 2003)

    If I understand that you want to automatically put the current date in somewhere, you could put the Date Field Code in the second cell of the template of the form, e.g. DATE @ "dddd, MMMM dd, yyyy" * MERGEFORMAT (but in brackets {}), (if I do it here it turns to the date). that would put in the current date when you update the fields, which includes printing the document, etc., you could alter the switches (things in quotations) to get your exact preferred format. You can get this from the Insert Date on the menu, but put it in as a field. If you want to look at it, run the ViewFields Macro. Depending on what sort of document this is though, you might have to have something built into the macro that would unlink the field, and preserve it at the current date. Or you might want to leave it linked, so it doesn't update. I don't know if this is helpful, but I hope it will be
    Arlene

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Specifying insertion location in a macro (Word 2003)

    <hr>DATE @ "dddd, MMMM dd, yyyy" * MERGEFORMAT (but in brackets {}), (if I do it here it turns to the date).<hr>
    Hi Arlene:
    It shouldn't. You can type the brackets & with your format like so:
    { DATE @ "dddd, MMMM dd, yyyy" * MERGEFORMAT }

    However, if you try to copy field codes from Word, you only get the result.
    Cheers,

Posting Permissions

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