Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Insert signature in Outlook 2003 at users cursor via VBA

Posted on 2008-10-15
8
Medium Priority
?
2,425 Views
Last Modified: 2011-10-03
I have found how to insert a signature using VBA and it works fine.  But, now I was wondering if it is possible to insert the signature at the users cursor in the email.  Currently the signature appends to the bottom of the email, but my users are now having difficulty with it when they reply or forward emails.  So, I would like to see if I can insert the signature where the user has but the cursor instead of appending it at the bottom of the email.

Below is the current code I have for the signatures.  One method I have used to insert text in the path was the "SendKey" function in VBA, but I am not sure how to do this with signatures.
Sub InsertSignature()
    Dim objFSO As Object, _
        objShell As Object, _
        objSignatureFile As Object, _
        olkMsg As Outlook.MailItem, _
        strSigFilePath As String, _
        strBuffer As String
    Set olkMsg = Application.ActiveInspector.CurrentItem
    Set objShell = CreateObject("Wscript.Shell")
    strSigFilePath = objShell.SpecialFolders("Desktop")
    If InStr(1, WinVer(), "Vista") Then
        strSigFilePath = Replace(strSigFilePath, "Desktop", "AppData\Roaming\Microsoft\Signatures\")
    Else
        strSigFilePath = Replace(strSigFilePath, "Desktop", "Application Data\Microsoft\Signatures\")
    End If
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Edit the signature file name on the following line as needed'
    Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & "NXW-FIRM.htm")
    strBuffer = objSignatureFile.ReadAll
    objSignatureFile.Close
    olkMsg.HTMLBody = olkMsg.HTMLBody & strBuffer
    Set objSignatureFile = Nothing
    Set objFSO = Nothing
    Set objShell = Nothing
    Set olkMsg = Nothing
End Sub
 
Function WinVer() As String
    Dim objShell As Object, strOS As String, strKey As String
    Set objShell = CreateObject("WScript.Shell")
    strOS = objShell.ExpandEnvironmentStrings("%OS%")
    If strOS = "Windows_NT" Then
        strKey = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
        WinVer = objShell.RegRead(strKey & "ProductName") & " " & objShell.RegRead(strKey & "CurrentVersion") & "." & objShell.RegRead(strKey & "CurrentBuildNumber")
    Else
        strKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\"
        WinVer = objShell.RegRead(strKey & "ProductName") & " " & objShell.RegRead(strKey & "VersionNumber")
    End If
    Set objShell = Nothing
End Function

Open in new window

0
Comment
Question by:navid86
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 22721625
Hi, navid86.

You can't use this kind of script to insert text at the cursor location in Outlook 2003 because Outlook doesn't expose the cursor location.  SendKeys will work, but it limits you to plain text, no HTML or graphics.  There might be another way to accomplish this, but I'll have to test my theory before I know for sure.
0
 
LVL 2

Author Comment

by:navid86
ID: 22721657
Okay, thanks!

Hope to hear from you soon!
0
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 22723794
Ok, I figured a way to do this.  First, add the code below to that you already have.  Second, go to this Microsoft page (http://support.microsoft.com/kb/274326) and copy all the code under step 4.  Create a new module in Outlook and paste the code into it.  You're ready to go.

Now when you're editing a message you can insert the signature at the current cursor location by running the InsertSigAtCursor macro.  This works by copying the signature to the clipboard, then using paste to insert it.  Paste always inserts at the current cursor location, so we don't need to know where it's at to get this to work.
Sub InsertSigAtCursor()
    CopySigToClipboard
    SendKeys "+{INSERT}"
End Sub
 
Sub CopySigToClipboard()
    Dim objFSO As Object, _
        objShell As Object, _
        objSignatureFile As Object, _
        strSigFilePath As String, _
        strBuffer As String
    Set olkMsg = Application.ActiveInspector.CurrentItem
    Set objShell = CreateObject("Wscript.Shell")
    strSigFilePath = objShell.SpecialFolders("Desktop")
    If InStr(1, WinVer(), "Vista") Then
        strSigFilePath = Replace(strSigFilePath, "Desktop", "AppData\Roaming\Microsoft\Signatures\")
    Else
        strSigFilePath = Replace(strSigFilePath, "Desktop", "Application Data\Microsoft\Signatures\")
    End If
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Edit the signature file name on the following line as needed'
    Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & "NXW-FIRM.htm")
    strBuffer = objSignatureFile.ReadAll
    objSignatureFile.Close
    PutHTMLClipboard strBuffer, "", ""
    Set objSignatureFile = Nothing
    Set objFSO = Nothing
    Set objShell = Nothing
    Set olkMsg = Nothing
End Sub

Open in new window

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 2

Author Comment

by:navid86
ID: 22724038
Sweet man!!! Works like a Charm!! I wish I could give you more than the 500 points for this!!

I do have a simple question though.  The code that I copied from Microsoft into a new module comes up as a macro name, "CopySigtoClipboard".  Is there anyway I can hide this from the Macro list?

Thanks again for everything!!
0
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 2000 total points
ID: 22724180
Thanks for the thought.

Actually, CopySigToClipboard is part of my code.  Yes, if you want to get rid of that, then we can combine it and InsertSigAtCursor.  Replace those two routines with the one combined routine below.
Sub InsertSigAtCursor()
    Dim objFSO As Object, _
        objShell As Object, _
        objSignatureFile As Object, _
        strSigFilePath As String, _
        strBuffer As String
    Set olkMsg = Application.ActiveInspector.CurrentItem
    Set objShell = CreateObject("Wscript.Shell")
    strSigFilePath = objShell.SpecialFolders("Desktop")
    If InStr(1, WinVer(), "Vista") Then
        strSigFilePath = Replace(strSigFilePath, "Desktop", "AppData\Roaming\Microsoft\Signatures\")
    Else
        strSigFilePath = Replace(strSigFilePath, "Desktop", "Application Data\Microsoft\Signatures\")
    End If
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Edit the signature file name on the following line as needed'
    Set objSignatureFile = objFSO.OpenTextFile(strSigFilePath & "NXW-FIRM.htm")
    strBuffer = objSignatureFile.ReadAll
    objSignatureFile.Close
    PutHTMLClipboard strBuffer, "", ""
    Set objSignatureFile = Nothing
    Set objFSO = Nothing
    Set objShell = Nothing
    Set olkMsg = Nothing
    SendKeys "+{INSERT}"
End Sub

Open in new window

0
 
LVL 2

Author Comment

by:navid86
ID: 22724492
Works AMAZING!
Thank you sooo much for your help!!

0
 
LVL 2

Author Closing Comment

by:navid86
ID: 31506313
Thanks a lot for your help!  I have noticed that you have helped on some of my past issues and I really like how you follow through till the issue is complete!!!

Thanks

0
 
LVL 76

Expert Comment

by:David Lee
ID: 22724559
Thanks and you're welcome.  Always happy when I can help out.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question