Open Msg File using a specific Outlook Account

Saphirah

Active member
Local time
Today, 05:21
Joined
Apr 5, 2020
Messages
163
Hey everyone :D
I created a system that automatically links emails from outlook to orders by storing the emails as a .msg file on our server.

You can then open the msg file again by pressing a button in a form. To open the Msg File i am using the following code:
Code:
Public Declare PtrSafe Function ShellExecute Lib "shell32.dll" _
  Alias "ShellExecuteA" (ByVal hWnd As Long, _
  ByVal lpOperation As String, ByVal lpFile As String, _
  ByVal lpParameters As String, ByVal lpDirectory As String, _
  ByVal nShowCmd As Long) As Long
Dim FileName As String: FileName = "Path/To/Folder/Email_Orders/" & EmailID & ".msg"
Call ShellExecute(0&, vbNullString, FileName, vbNullString, vbNullString, vbNormalFocus)

This simply opens the file using the default program for Msg Files (which is outlook) via Shell.

But when i now press "Reply" on the opened email, outlook will create a reply using the standart account of the PC.
We do have a custom account for order replies though. So i want to make outlook use this account instead.

So the question is, how can i open the msg file using a specific account, so that it automatically replies with the correct account? Or, if that is impossible, how can i make outlook change account whenever i hit the reply button? Do i need to change the ribbon for that?

Thank you very much for your help!
 
You do not say how you are replying?

I have used this code in the past to send via various accounts.
You can amend to suit.?

Code:
Public Function ListEMailAccounts(AcctToUSe As String) As Integer
    Dim outApp As Object
    Dim i As Integer
    Dim AccNo As Integer
    Dim emailToSendTo As String
    
    Set outApp = CreateObject("Outlook.Application")
    'emailToSendTo = "xxxxx@gmail.com"                    'put required email address
    AccNo = 1
    'if smtp address=email we want to send to, acc no we are looking for is identified
    For i = 1 To outApp.Session.Accounts.Count
        'Uncomment the Debug.Print command to see all email addresses that belongs to you
'Debug.Print "Acc name: " & OutApp.Session.Accounts.Item(i) & " Acc number: " & i & " , email: " & OutApp.Session.Accounts.Item(i).SmtpAddress
        'If OutApp.Session.Accounts.Item(i).SmtpAddress = emailToSendTo Then
        If outApp.Session.Accounts.Item(i).DisplayName = AcctToUSe Then

            AccNo = i
            Exit For
        End If
    Next i
    ListEMailAccounts = AccNo
    Set outApp = Nothing

Then used as
Code:
  intAccount = ListEMailAccounts("AccountToUse")
 With objOutlookMsg
    .SendUsingAccount = objOutlook.Session.Accounts.Item(intAccount)

You can also hardcode the index if you want to. Amend the above function to find out which it should be, however it could change, so as long as you do not rename the account, the function will return the correct account.

HTH
 
I am sorry, i think you misunderstood me. Let me clarify.
I store Emails as Msg files on my hard drive. These Files contain everything from the source email, including attachments etc...
Whenever the user wants to open a linked email i open this Msg File. The following window opens:
1623065482136.png


This is a preview of the saved Msg file. Now when i hit reply a basic reply form pops up:
1623065583195.png

But the sender is the default account from the computer. I want Outlook to automatically send from a specific account whenever i do this procedure.
I do know how to set the .SendUsingAccount using VBA, i just do not know HOW or WHEN to call the vba procedure, given the described workflow.
 
The easiest way is to have the emails sent to that account in the first place?
Then a reply automatically uses that account?. You could even set up a rule to forward such message to the errant account and use that email to save and reply to.?
How are you actually sending the email?
You could have a button on the ribbon to run the code to change account?, but then you already pretty much have that at the top left?
 
did you try mr.gasman's code first then call the shellexecute after?
 
I think you should combine the code in Gasman's post with the one in this one to change the way you open the .msg files.
So instead of using Shell which loads the default account you use automation to open Outlook with the right account then in that instance open the .msg file.
Cheers,
 

Users who are viewing this thread

Back
Top Bottom