Showing posts with label Outlook. Show all posts
Showing posts with label Outlook. Show all posts

Friday, April 25, 2014

VBA code for Outlook - Reply Mail With Attachment

May times it happens that you want to Reply some mail with attachment but when you click reply, it removes the attachment from your mail, and you need to attach it manually. Following function will help you to reply mail with attachment.



'''''''''''''''''''''' Reply Module ''''''''''''''''''''''''''
Sub ReplyWithAttachments()  ' For Only single Reply
    Dim rpl As Outlook.MailItem
    Dim itm As Object
     
    Set itm = GetCurrentItem()
    If Not itm Is Nothing Then
        Set rpl = itm.Reply
        CopyAttachments itm, rpl
        rpl.Display
    End If
     
    Set rpl = Nothing
    Set itm = Nothing
End Sub
 
Sub ReplyALLWithAttachments()   ' For Reply ALL
    Dim rpl As Outlook.MailItem
    Dim itm As Object
     
    Set itm = GetCurrentItem()
    If Not itm Is Nothing Then
        Set rpl = itm.ReplyAll
        CopyAttachments itm, rpl
        rpl.Display
    End If
     
    Set rpl = Nothing
    Set itm = Nothing
End Sub

Sub ReplyOnly()  ' For Only Reply without Body message
    Dim rpl As Outlook.MailItem
    Dim itm As Object
    Set itm = GetCurrentItem()
    If Not itm Is Nothing Then
        Set rpl = itm.Reply
        rpl.subject = itm.subject
        rpl.Display
'        rpl.HTMLBody = rpl.HTMLBody

    SendKeys "^f"
    SendKeys "From:"
    SendKeys "{Enter}"
    SendKeys "{Esc}"
    SendKeys "{Home}"
    SendKeys "^+{End}"
    SendKeys "{Del}"
    SendKeys "^{Home}"
    
    End If
     
    Set rpl = Nothing
    Set itm = Nothing
End Sub

Function GetCurrentItem() As Object ' Will Create object for the current selected One mail
    Dim objApp As Outlook.Application
         
    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select
     
    Set objApp = Nothing
End Function
 
Sub CopyAttachments(objSourceItem, objTargetItem)   ' Coping the Attachment if exist.
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
   strPath = fldTemp.Path & "\"
   For Each objAtt In objSourceItem.Attachments
      strFile = strPath & objAtt.FileName
      objAtt.SaveAsFile strFile
      objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
      fso.DeleteFile strFile
   Next
 
   Set fldTemp = Nothing
   Set fso = Nothing
End Sub

'''''''''''''''''''''' End of Reply Module '''''''''''''''''''''''''''''''''''''''''''

VBA code for Outlook - Forward Mail Without Attachment

May times it happens that you want to forward some mail without attachment that you received with attachments, and you need to delete manually. Following function will help you to remove the attachment while you send mail to someone.

Sub ForwardMailWithoutAttachment() 
 On Error GoTo ErrorHandler
    Dim obj As Object
    Dim msg As Outlook.MailItem
    Dim newMsg As Outlook.MailItem
    Dim subject As String
    Dim myattachments As Outlook.Attachments
    
     ' check for multiple selections
    If ActiveExplorer.Selection.Count > 1 Then
        MsgBox "please select one email only"
        GoTo ProgramExit
    End If
     
    Set obj = ActiveExplorer.Selection.Item(1)
    If Not obj Is Nothing Then
        If TypeName(obj) = "MailItem" Then
            Set msg = obj
            Set newMsg = msg.Forward
            subject = obj.subject ' Copy the selected Mail Subject
            If Len(subject) = 0 Then
                GoTo ProgramExit
            End If
            
            '########### To Remove the Attachment ##############
            Set myattachments = newMsg.Attachments
            While myattachments.Count > 0
                       myattachments.Remove 1
            Wend
            '###########################################
            With newMsg
                .subject = subject
                .Display
            End With
        Else
            MsgBox "Cannot Run this Macro. Invalid Selection of Mail."
            GoTo ProgramExit
        End If
    End If
ProgramExit:
    Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ProgramExit

End Sub

Friday, November 8, 2013

Add Dynamic Text in Outlook Mail


This code will help to create a Macro that will be use for repeated input text.

Sub myMacro1()

Dim sText As String
sText = "Hi," & Chr(10) & Chr(10) & _
"This is a Macro Use in Outlook Mail" & Chr(10) & Chr(10) & _
"Enjoy Coding:" & Chr(10) 

PrintTextToBody (sText)
End Sub

Sub PrintTextToBody(ByVal sText As String)
On Error GoTo ErrHandler
If TypeName(ActiveWindow) = "Inspector" Then
    If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
        ActiveInspector.WordEditor.Application.Selection.TypeText sText
    End If
End If
Exit Sub
ErrHandler:
Beep
End Sub

Wednesday, July 3, 2013

SendKeys Method

This method places keystrokes in a key buffer. In some cases, you must call this method before you call the method that will use the keystrokes. For example, to send a password to a dialog box, you must call the SendKeys method before you display the dialog box.
The Keys argument can specify any single key or any key combined with ALT, CTRL, or SHIFT (or any combination of those keys). Each key is represented by one or more characters, such as "a" for the character a, or "{ENTER}" for the ENTER key.
To specify characters that aren't displayed when you press the corresponding key (for example, ENTER or TAB), use the codes listed in the following table. Each code in the table represents one key on the keyboard.

eyCode
BACKSPACE{BACKSPACE} or {BS}
BREAK{BREAK}
CAPS LOCK{CAPSLOCK}
CLEAR{CLEAR}
DELETE or DEL{DELETE} or {DEL}
DOWN ARROW{DOWN}
END{END}
ENTER (numeric keypad){ENTER}
ENTER~ (tilde)
ESC{ESCAPE} or {ESC}
HELP{HELP}
HOME{HOME}
INS{INSERT}
LEFT ARROW{LEFT}
NUM LOCK{NUMLOCK}
PAGE DOWN{PGDN}
PAGE UP{PGUP}
RETURN{RETURN}
RIGHT ARROW{RIGHT}
SCROLL LOCK{SCROLLLOCK}
TAB{TAB}
UP ARROW{UP}
F1 through F15{F1} through {F15}

You can also specify keys combined with SHIFT and/or CTRL and/or ALT. To specify a key combined with another key or keys, use the following table.
To combine a key withPrecede the key code with
SHIFT+ (plus sign)
CTRL^ (caret)
ALT% (percent sign)

Example

This example uses the SendKeys method to quit Microsoft Excel.
Application.SendKeys("%fx")


Reference: MSDN.Microsoft.Com 

Saturday, March 30, 2013

Auto Complete mail address Outlook


In case of move to new PC/Laptop, if you want to move your Auto Complete mail address from Outlook then you just need to do following.

Locate the .nk2 file is C:\Users\[User Profile]\AppData\Roaming\Microsoft\Outlook 

Copy where your new outlook is configured at the same location mention above. and your have done. 

Load your outlook and see Auto Complete is working now !!!