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.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
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.
Top