[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

need asssistance to create a VB Macro that can be accessed by a shortcut key to format text in Outlook Task

Hi All, Can you please assist to create a VB Macro that i access via a shortcut key in an Outlook Task, to Date / Time Stamp the current Date & Time in Bold, Blue text and then, resort to normal text, so information can be typed in there ?

THanks so much in advance !
0
troy_g
Asked:
troy_g
  • 13
  • 5
1 Solution
 
David LeeCommented:
Hi, troy_g.

What version of Outlook?
0
 
troy_gAuthor Commented:
Thanks so much for responding - Outlook 2007
0
 
troy_gAuthor Commented:

example of formatting required :

Fri24/4/09  9:19 AM -
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
troy_gAuthor Commented:
sorry, that didn't come out well, please see the attached file . . .
Date---Time-Stamp-Example.jpg
0
 
troy_gAuthor Commented:
further to this, i have another Macro program ( Macro Express ) that can type out the date and time, so if someone could please just assist with a macro, that would make the current text Bold & Blue and then another one to take off the Bold & Blue text, that would do me fine and be very much appreciated!

Thanks again in advance!
0
 
David LeeCommented:
This does it all.  Follow these instructions to add this code to Outlook.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects
4.  If not already expanded, expand Modules
5.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
7.  Edit the code as needed.  I included comments wherever something needs to or can change
8.  Click the diskette icon on the toolbar to save the changes
9.  Close the VB Editor

When you want to

1.  Open or create a task.
2.  Run the macro.  It will insert the date in bold and blue at the top of the task's body.
Sub InsertAndFormatText()
    Const wdColorBlue = 16711680
    Dim olkTask As Outlook.TaskItem, objDoc As Object, objSel As Object, strTime As String, intLen As Integer
    Set olkTask = Outlook.Application.ActiveInspector.CurrentItem
    Set objDoc = olkTask.GetInspector.WordEditor
    strTime = Format(Now, "ddd dd/mm/yy h:mm AM/PM") & vbCrLf
    intLen = Len(strTime)
    objDoc.Characters(1).InsertBefore strTime
    objDoc.Windows(1).Document.Range(0, intLen).Select
    Set objSel = objDoc.Windows(1).Selection
    With objSel
        .Font.Bold = True
        .Font.Color = wdColorBlue
    End With
    Set olkTask = Nothing
    Set objDoc = Nothing
    Set objSel = Nothing
End Sub

Open in new window

0
 
troy_gAuthor Commented:
Thanks so much for your assistance with this, I have done everything as you said, but unfortunately it doesn't appear to work ( appears to be problem on my end )

I created the macro from the main screen in outlook as you described and saved it.  Then, I added it to the quick access toolbar and have clicked on the macro, when in outlook task, but it doesn't type out any text.

What would you suggest I do to try and get this running?
0
 
troy_gAuthor Commented:
further to this, can i assign a shortcut key to this Macro ?
0
 
David LeeCommented:
Let's get this working first and then look at creating a shortcut key.  Here's what I'd like you to do.

1.  Open an existing task or create a new task.  Leave it open onscreen.
2.  Open Outlook's  VB editor (ALT+F11) and find the code.
3.  Place the insertion point anywhere within the code.
4.  Press F8.  This starts the built-in debugger.
5.  Step through the code one line at a time by pressing F8 once for each line of code.  Continue doing this until every line of the code has executed or you get an error.

Let me know what happens.  If Outlook does not generate an error, then did the code work?
0
 
troy_gAuthor Commented:
Thanks so much, that worked ! It initially said that Macroswere disabled and i am using Word as the Editor, so i allowed all Macros inWord Trust Centre and then restarted Outlook and it worked !
Now, for some fine tuning  you will see from the screenshot below, that once it has completed the Macro, it appears to just select allof the text and leave it.  Therefore, canyou please assist to point me to the code I should change, to :
<!--[if !supportLists]--><!--[endif]-->Have the code appear as : Wed 29/4/09  10:36 AM  which has a space, a dashand a space after the AM/PM part ?
  • <!--[if !supportLists]--><!--[endif]-->Then, take off the Blue & Bold Format ?  So, when I start typing, it appears as normaltext
  • <!--[if !supportLists]--><!--[endif]-->Further to this, sometimes in tasks, theformatting has changed back to a different default, so could you please alsoallow for the code to have the Font as Arial and Point Size 12 ?
  • <!--[if !supportLists]--><!--[endif]-->Finally, if you could show me how to create ashortcut key for this, I would be eternally grateful !
Thanks again in advance so much for your help.

Outlook-Time-Stamp.jpg
0
 
troy_gAuthor Commented:

Thanks so much, that worked ! It initially said that Macros were disabled and i am using Word as the Editor, so i allowed all Macros in Word Trust Centre and then restarted Outlook and it worked !

Now, for some fine tuning  you will see from the screen shot below, that once it has completed the Macro, it appears to just select all of the text and leave it.  Therefore, can you please assist to point me to the code I should change, to :

  • Have the code appear as : Wed 29/4/09  10:36 AM  which has a space, a dash and a space after the AM/PM part ?
  • Then, take off the Blue & Bold Format ?  So, when I start typing, it appears as normal text
  • Further to this, sometimes in tasks, the formatting has changed back to a different default, so could you please also allow for the code to have the Font as Arial and Point Size 12 ?
  • Finally, if you could show me how to create a shortcut key for this, I would be eternally grateful !
Thanks again in advance so much for your help.



Outlook-Time-Stamp.jpg
0
 
troy_gAuthor Commented:
one last thing, i don't need it to go in at the top of the tasks body, just where the current cursor is at.

thanks again in advance !
0
 
troy_gAuthor Commented:
Hi BlueDevilFan - do you need any further information from me, to assist to get this running ?
0
 
David LeeCommented:
Hi, troy_g.  

No, I just get busy with other things sometimes and am slow to respond.  Apologies for that.  

Have the code appear as : Wed 29/4/09  10:36 AM  which has a space, a dash and a space after the AM/PM part ?

You can change the text that's inserted at any time by editing line 6 of the code.  I think I've made all the changes you asked for.  Replace the original code with the version below.

Sub InsertAndFormatText()
    Const wdColorBlue = 16711680
    Dim olkTask As Outlook.TaskItem, _
        objDoc As Object, _
        objSel As Object, _
        strTime As String, _
        intStart As Integer, _
        intLen As Integer
    Set olkTask = Outlook.Application.ActiveInspector.CurrentItem
    Set objDoc = olkTask.GetInspector.WordEditor
    strTime = Format(Now, "ddd dd/m/yy h:mm AM/PM - ")
    intLen = Len(strTime)
    Set objSel = objDoc.Windows(1).Selection
    intStart = objSel.Start
    objSel.InsertAfter strTime
    objDoc.Windows(1).Document.Range(intStart, intStart + intLen).Select
    Set objSel = objDoc.Windows(1).Selection
    With objSel
        .Font.Bold = True
        .Font.Color = wdColorBlue
    End With
    objDoc.Windows(1).Document.Range(intStart + intLen - 1, intStart + intLen + 1).Select
    Set objSel = objDoc.Windows(1).Selection
    With objSel
        .Font.Name = "Arial"
        .Font.Size = 12
        .Font.Bold = False
        .Font.Color = wdColorAutomatic
    End With
    Set olkTask = Nothing
    Set objDoc = Nothing
    Set objSel = Nothing
End Sub

Open in new window

0
 
troy_gAuthor Commented:
very happy, quick response, professional and to the point, thanks so much !
0
 
troy_gAuthor Commented:
Hi BlueDevilFan, sorry, but you're going to hate me!

I had previously tested it just in a new task and it worked fine.

However, when I have gone to some existing tasks, where inadvertently ( not deliberately! ) the standard font is different, then when I run the macro, to insert the day and time, it inserts the day and time, but in the font that is at that point in the task and then changes it to Arial, 12 etc . . .

Is it possible to alter the code, to allow for this?
0
 
David LeeCommented:
"very happy, quick response, professional and to the point, thanks so much !"
That you for the very generous comment.  Glad I could help.

"the font that is at that point in the task and then changes it to Arial, 12"
I don't understand.  Isn't that what you asked for, i.e. "so could you please also allow for the code to have the Font as Arial and Point Size 12 "?  Are you saying that you want the code to check the font after the insertion and if it isn't Arial 12, to leave it set to whatever font is in use?  
0
 
troy_gAuthor Commented:
Sorry for the confusion and not clarifying.

Basically, for example if the font at the point of running the macro is Calibri and 72 point, then the date and time will be stamped in that font and then go to Arial, 12 point, not Bold etc . . .

However, I have been able to update this myself, by changing the details under the first 'With objSel' section and it works fantastically!

Thank you again so much for your help!
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 13
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now