adding multiple messgeboxes values to single messa

2020-05-07 06:40发布

问题:

I have this code with me where i can display the message when every outer loop ends. I want to catch all these messages in suppose array or soome list like structure and then at the end want to display each of these messages into one msgbox. Would appreciate if someone could help me. Thanks.

For Each objNavFolder In objNavGroup.NavigationFolders
    SkippedItemCounter = 0
    If oItems.Count = 0 Then
        MsgBox "No Appointments items in " & objNavFolder.DisplayName & "'s folder"
    Else
        NextRow = NextRow + 1
        For Each MyItem In oItems
             If MyItem = "" Then
                 SkippedItemCounter = SkippedItemCounter + 1
             End If
             'some code here
        Next 
        Set objExpl = _colExpl.Add(objFolder, olFolderDisplayNormal)
        NextRow = NextRow - 1
    End If
    MsgBox "No. of items= "&SkippedItemCounter&"skipped from"&objNavFolder.DisplayName&""
Next 
End If
End If
End If

回答1:

instead of calling msgboxes, create a String and keep adding the messages - at the end of code msgbox(yourString)

for example decalare a string before the main sub

Dim yourFinalMessage As String ' or Dim yourFinalMessage$

instead of

MsgBox "No Appointments items in " & objNavFolder.DisplayName & "'s folder"

say

yourFinalMessage = yourFinalMessage & vbCrLf & & _ 
             "No Appointments items in " & objNavFolder.DisplayName & "'s folder"

keep doing this until the loop ends.

at the end of loop say

msgbox YourFinalMessage 


回答2:

Not sure to exactly understand what you want, but you might try to add this to a module:

Option Explicit

Dim globalMsg as String
globalMsg = ""

Function customMsg(msg as String)
    MsgBox msg
    globalMsg = globalMsg & VbCrLf & msg
End Function

Just call customMsg("Your Message") to display a MsgBox and at the end, call MsgBox globalMsg to display all the messages as a single message (one per line). There are a lot of other ways to do this, it depends on you. Please be more explicit if you want any further help.