Calling one module from another

Status
Not open for further replies.

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I have the code below sitting on a module:

Code:
Sub MsgSaver(strPath As String, msgItem As Outlook.MailItem, lngCounter As Long, blnMulti As Boolean)

  Dim intC As Integer
  Dim intD As Integer
  Dim strMsgSubj As String
  Dim strMsgTo As String
 
'Set name to save message to
   If UserForm1.CheckBox1.Value = True Then
            If UserForm1.g_blnOutgoing = True Then
            strMsgSubj = Format(msgItem.senton, "yyyy-mm-dd Hh.Nn.Ss") & " " & "[To " & Left(msgItem.To, 25) & "]" & " " & UserForm1.TextBox9.Value & "_0" & lngCounter & ".msg"
            Else 'incoming mail so use from field
            strMsgSubj = Format(msgItem.senton, "yyyy-mm-dd Hh.Nn.Ss") & " " & "[From " & msgItem.SenderName & "]" & " " & UserForm1.TextBox9.Value & "_0" & lngCounter & ".msg"
            End If
   ElseIf blnMulti = True Then
      strMsgSubj = msgItem.Subject
      If UserForm1.g_blnOutgoing = True Then
        strMsgSubj = Format(msgItem.senton, "yyyy-mm-dd Hh.Nn.Ss") & " " & "[To " & Left(msgItem.To, 25) & "]" & " " & strMsgSubj & ".msg"
      Else 'incoming mail so use from field
        strMsgSubj = Format(msgItem.senton, "yyyy-mm-dd Hh.Nn.Ss") & " " & "[From " & msgItem.SenderName & "]" & " " & strMsgSubj & ".msg"
      End If
   Else
      strMsgSubj = UserForm1.TextBox8.Value & ".msg"
   End If
 ' ##################################
  ' Clean out characters from Subject which are not permitted in a file name

  For intC = 1 To Len(strMsgSubj)
    If InStr(1, ":<&>""", Mid(strMsgSubj, intC, 1)) > 0 Then
      Mid(strMsgSubj, intC, 1) = "-"
    End If
  Next intC

  For intC = 1 To Len(strMsgSubj)
    If InStr(1, "\/|*?", Mid(strMsgSubj, intC, 1)) > 0 Then
      Mid(strMsgSubj, intC, 1) = "_"
    End If
  Next intC
' ##################################
 
'Save new msg file to defined filename and location
  msgItem.SaveAs strPath & strMsgSubj
  Set msgItem = Nothing
  UserForm1.hide

End Sub

the code that sits between the ########## is repeated in several of the modules I have and I would like to put this into its own module that I can then call this new module from each of the other modules. I tried cutting and pasting the block of code into a new module called clean_char (module 2) and replacing this block of code in the module above with the code Call module2.clean_char. However this didn't work. This is the first time I have tried to call one module from another so a bit unsure how to get it working.
 
The fist step is ok, move the part you want to move to the new module. Then feed the new function with all the values it needs, for instance strMsgSubject. As strMsgSubject is filled in the old function, it is unkown or empty in the new one. So, to pass that one value the function declaration could look like this one:
Code:
sub whatever(ByRef strMsgSubject as String)
end sub
'ByRef' means modifications will be passed back. Call it with:
whatever strMsgSubject

This will pass the value of strMsgSubject from the old function to the variable of the new function. If you need to pass more values, add variables within the parantheses.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J Calling a Public sub-routine from the script editor via VB script Outlook VBA and Custom Forms 4
Wayne Dardis Calling Ribbon Button in Outlook 2016 Outlook VBA and Custom Forms 6
S VSTO calling unmanged function Outlook VBA and Custom Forms 3
H Auto-Dialer for calling contact Using Outlook 2
T PR_MESSAGE_CLASS is reset when calling Save() on item Outlook VBA and Custom Forms 4
petunia Outlook tasks module sunsetting? Exchange Server Administration 3
J Want to create a button on the nav bar (module add-in) to run code Outlook VBA and Custom Forms 2
230Grains iCloud COM Module Keeps de-selecting in Outlook 2013 Using Outlook 1
E VBScript to replace module? Using Outlook 3
R Addins4Outlook TagIt! addin script or VBA module? Using Outlook 2
L Script to Macro in Module Using Outlook 36
S Exam 77-884: Managing Contact Module Options Using Outlook 4
L Can't find procedure or module to run Outlook VBA and Custom Forms 4
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
C Copy from one Profile to another Using Outlook 0
P Tweak vba so it can target another mailbox Outlook VBA and Custom Forms 1
B Move emails from one account to another Outlook VBA and Custom Forms 2
R Clear one category when another is chosen Outlook VBA and Custom Forms 1
N Item cannot be saved because it was modified by another user or window, and, Item could not be moved... Using Outlook 0
e_a_g_l_e_p_i Is it possible to transfer things from one calendar to another Using Outlook 2
S Email Generated from another program and then edited sends original email. Using Outlook 2
R Time for another article on contact cards? Using Outlook 0
C Copy Outlook contact field value to another field Outlook VBA and Custom Forms 1
J Message search and edit, another way? Outlook VBA and Custom Forms 4
F Copy and replace not update contact in another pst Using Outlook 0
D Importing Outlook Categories from another domain (Exchange 2016/Outlook 2016) Using Outlook 4
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
R "Can't store Outlook data files under the AppData folder. Please choose another folder." Using Outlook 6
X Unable to send an email from one account to another on same PC Using Outlook 2
M Moving mail to another folder is much slower than before (Office365) Using Outlook 0
CWM030 Another Quarantine question Exchange Server Administration 0
D Any updates or fixes that would make this code stop working just moving emails to another folder Outlook VBA and Custom Forms 1
P when i move inbox mails to another folder in outlook the mail disappears Using Outlook 1
B Moving account, contacts & emails to another H/D with MSOutlook Using Outlook 5
M Shortcut to another outlook item Using Outlook 0
Sabastian Samuel HOW DO I FORWARD AN EMAIL WITH MACRO using an email that in the body of another email Outlook VBA and Custom Forms 3
P Unread Until Click Another Message? Using Outlook 2
e_a_g_l_e_p_i Another reinstall of Outlook 2010 issue Using Outlook 10
M code to move selected Outlook contacts to another folder Using Outlook 3
D Help with code to move mail on receipt to another folder based on time received Outlook VBA and Custom Forms 2
M FW Email via Outlook and another requirement Outlook VBA and Custom Forms 1
J Outlook - 2013 - Error msg when copying folders from Online Archives to another user's mailbox Using Outlook 0
Diane Poremsky Set Another Data File as Default When Using an Exchange Account Using Outlook 0
Diane Poremsky Copy New Appointments to Another Calendar using VBA Using Outlook 0
D Unable to Send On Behalf of Another User Exchange Server Administration 0
D EXPORTING MS EXCHANGE EMAILS to another platform Exchange Server Administration 10
mrmmickle1 UserProperties VBA to move message to another folder Outlook VBA and Custom Forms 7
P Office 365 manage another person's calendar Using Outlook 0
TheDavidSlight Daylight savings - another subtle bug ... Using Outlook 3
Q Outlook 2016\365 export specific rules to import in another system Exchange Server Administration 1

Similar threads

Back
Top