Share via

Issues creating Macro hiding text

Anonymous
2023-08-17T22:02:22+00:00

I'm trying to create a macro that inserts a timestamp and then applies hidden formatting to it.

I am experiencing two issues.

  1. Even though the macro successfully records ".Hidden = TRUE", the timestamp it inserts is not hidden.

(I thought perhaps this was because I was de-applying hidden formatting afterwards, so I could continue typing, but even without this extra convenience the hidden formatting would not apply.)

  1. I assigned the shortcut Ctrl+D+A to it. However, when I do, I cannot use the shortcut Ctrl+D to open the Font formatting box anymore (contrary to expectations). It only makes it say Ctrl+D in the little gray banner at the bottom that shows up occasionally.
Microsoft 365 and Office | Word | For education | 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

7 answers

Sort by: Most helpful
  1. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2023-08-18T00:49:29+00:00

    As I suspected, the code adds the date/time and leaves the Selection (the insertion point) at the right end of the inserted text. All of the formatting that follows is applied only at that insertion point and doesn't affect the date/time at all. When the nonprinting and hidden text is displayed by clicking the ¶ button on the Home ribbon, it looks like this, where I typed the large text to show where the formatting wound up:

    Furthermore, all of the formatting added by the macro recorder -- except for the Hidden setting and the font size -- is the same as the default format of the Normal style and doesn't change anything. (This is a common failing of the recorder, which I wrote up many years ago in this article.)

    To get the desired result

    you can use the code below. It saves the location of the start of the date/time before inserting it; when the Selection winds up at the right end of the date/time, the next statement resets the Start (the left end of the Selection) back to the original location. That causes the Selection to cover the entire date/time, and the rest of the formatting -- only the two pieces that are not default values -- applies to the extended Selection.

    Sub Time_v2() 
    
        Dim SelStart As Long 
    
        With Selection 
    
            SelStart = .Start 
    
            .InsertDateTime DateTimeFormat:="M/d/yyyy h:mm am/pm", InsertAsField:=False 
    
            .Start = SelStart 
    
            .Font.Hidden = True 
    
            .Font.Size = 16 
    
        End With 
    
    End Sub
    

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments
  2. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2023-08-19T13:03:37+00:00

    Thanks. Do you have any idea how I could add a hidden space before the date?

    Insert the statement

              .TypeText Text:=" " 
    

    after the SelStart = .Start statement and before the .InsertDateTime statement. That space will be included in the hidden text. (I didn't include it in the macro because I thought it wasn't necessary.)

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Charles Kenyon 167.7K Reputation points Volunteer Moderator
    2023-08-18T02:04:24+00:00

    I'm not sure that it needis the line:

           .Font.Size = 16 
    

    unless the font size is supposed to be changed to 16.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  4. Stefan Blom 342.4K Reputation points MVP Volunteer Moderator
    2023-08-17T22:57:28+00:00
    1. Post the code that you recorded; otherwise it will be difficult for anyone to diagnose the error in detail. If you provide some more detail, someone may also/alternatively be able to suggest a different way to add the time stamp.
    2. The shortcut Ctrl+D+A assumes that you press and hold Ctrl, then press and hold D and press and hold A, so that finally all three keys are depressed. (If you hold Ctrl and press and release D, the Font dialog box will display instead.)

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  5. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2023-08-17T22:55:39+00:00

    Issue #1: It isn't possible to be sure without seeing the code of the macro, but the most likely reason is that the Hidden formatting isn't being applied to the time stamp. Maybe it's applied to the single location immediately to the right of the time stamp, if the Selection is collapsed there after the insertion. Post the code for a more definite diagnosis.

    Issue #2: Word has two varieties of shortcuts. One of them consists of a single letter (or number or function key) modified by some combination of the Ctrl, Alt, and Shift keys, all pressed together. The other consists of a shortcut of the first kind followed by releasing those keys and pressing a letter. In the Customize Keyboard dialog, this second kind is shown with the two parts separated by a comma.

    The problem is that you chose the second kind but selected an already assigned shortcut of another command as the first part. That reassigned the Ctrl+D. You could instead use Ctrl+Alt+D (which is currently assigned to the rarely used InsertEndnoteNow command) or Ctrl+Alt+Shift+D (which isn't assigned to anything by default) with or without the A as a second part.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments