Share via

Hotkey to insert static time

Anonymous
2012-04-18T14:56:27+00:00

I need to insert a static time into a Word 2010 document as I'm taking notes. Then I want to make that timestamp hidden text so that it doesn't appear in my official version.

I managed to cobble together a macro and assign it to Ctrl-1, but I noticed an odd side effect. When I run this macro, suddenly Word stops finding spelling as I type. I checked all my settings, and they're okay.

What I discovered was that this macro is setting the Language proofing to do not check spelling or grammar. If I select everything in the document and clear that setting, everything's okay.

Using the technique of commenting out lines in my macro until I found the one that caused the problem, I found that it's this statement:

    Selection.Fields.Unlink

Here's my macro. Can anyone help? I'm new to macros, but I'm familiar with the concept from previous work with VBScript.

Sub InsertStaticTime()

    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldTime

    Selection.MoveLeft Unit:=wdCharacter, Count:=1

    Selection.Fields.Unlink

    Selection.Font.Hidden = True

    Selection.MoveRight Unit:=wdCharacter, Count:=1

    Selection.Font.Hidden = False

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2012-04-18T18:46:10+00:00

You could also use my macro changing it as follows:

Sub InsertStaticTime()

'

' InsertStaticTime Macro

'

'

     Dim datetext As String

     datetext = Format$(Time, "hh:mm")

     Selection.TypeText Text:=datetext

     Selection.MoveLeft Unit:=wdCharacter, Count:=5, Extend:=wdExtend

     Selection.Font.Hidden = True

     Selection.MoveRight Unit:=wdCharacter, Count:=5

     Selection.Font.Hidden = False

End Sub

Was this answer helpful?

0 comments No comments

18 additional answers

Sort by: Most helpful
  1. Anonymous
    2012-04-18T21:34:40+00:00

    Aha!  I even understand what you're doing -- putting the value of the current time into a variable, and then writing the contents of the variable, which isn't going to change.

    I made only one tweak. I changed the variable name from "datetext" to "timetext" because that's what's going in there :-)

    Thanks so much for the solution!

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-04-18T17:30:20+00:00

    I think I might have found the solution.  The statement to make it static is InsertAsField:=False

    Sub InsertStaticTime()

        Selection.Font.Hidden = True

        Selection.InsertDateTime DateTimeFormat:="h:mm am/pm", _

        InsertAsField:=False

        Selection.Font.Hidden = False

    End Sub

    I'm going to test it a few times before I mark this as the answer. I'd be interested in hearing feedback.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2012-04-18T15:54:43+00:00

    This looks good, but I need the time, not the date. The format I prefer is hh:mm, in 12-hour clock. (I don't need the AM/PM, though, because the time will always be during the regular workday.)

    Also, I need to make sure that when I save the file and open it again later, all those times won't be converted to the time at which I'm opening the file. That's the problem that the Selection.Fields.Unlink statement was supposed to solve (at least that's what I understood), but it had the side effect of messing up the language proofing setting.

    As I said, I'm new to VBA, but I don't see anything there that would make this be static.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2012-04-18T15:15:04+00:00

    Try this instead

    Sub InsertStaticTime()

        Dim datetext As String

        datetext = Format$(Date, "dd/mm/yyyy")

        Selection.TypeText Text:=datetext

        Selection.MoveLeft Unit:=wdCharacter, Count:=10, Extend:=wdExtend

        Selection.Font.Hidden = True

        Selection.MoveRight Unit:=wdCharacter, Count:=10

        Selection.Font.Hidden = False

    End Sub

    Was this answer helpful?

    0 comments No comments