Opgelost Commandbutton in Userform

Dit topic is als opgelost gemarkeerd
Status
Niet open voor verdere reacties.

keb

Gebruiker
Lid geworden
20 feb 2011
Berichten
133
In Outlook heb ik een userform met twee besturingselementen: een listbox en een commandbutton.
Ik lees een cSV-bestand in om een 2dim matrix te vullen.
De listbox vul ik met de eerste kolom van deze 2dim matrix (dit zijn projecten).

Als ik op de commandbutton klik moet de positie van de listbox (ListinIndex) doorgegeven worden zodat ik de corresponderende waarde uit de tweede kolom kan bepalen (opslaglocatie voor de emails in die projectmap).

Hoe kan ik detecteren of de Commandbutton is ingedrukt en de juiste ListIndex wordt doorgegeven?
In het programma weergegeven met "Wait until the command button is pressed", regel 45
Code:
'This program uses an Userform with Listbox and Commandbutton

'=======================================================================================
'Declaration of parameters
Dim myItem As Object
Dim olMail As Outlook.MailItem
Dim Selection As Selection
    
Dim n As Integer
Dim myFolder As Folder

Dim strSender, strReceiver As String
Dim strDate As Long
Dim strPath As String
Dim choice As Integer

Dim file As String, strLine As String
Dim strDest(1 To 2, 1 To 5) As String                   '2dim matrix with finally destination for e-mails

Public intChoice As Integer


'=======================================================================================
Sub MailtoFile()
'Read setup file/parameters for dropbox
file = "D:\Setup.csv"
Open file For Input As #1
n = 0
Userform1.ListBox2.Clear
Do Until EOF(1)
    n = n + 1
    Line Input #1, strLine: 'Debug.Print strLine
    pos1 = InStr(strLine, ";")
    strDest(1, n) = Left(strLine, pos1 - 1)
    Userform1.ListBox2.AddItem (strDest(1, n)) 'Listbox will be filled
    strDest(2, n) = Mid(strLine, pos1 + 1)
    Loop
    Close #1
    
Debug.Print strDest(1, 4) & "   " & strDest(2, 4)
intChoice = 0
Userform1.Show

'Wait until the command button is pressed
If intChoice > 0 Then
    'Show value in second column
    strPath = strDest(2, Userform1.ListBox2.ListIndex + 1)
    MsgBox strPath
        
   End If
End Sub
 

Bijlagen

  • Setup.csv
    258 bytes · Weergaven: 4
Probeer het eens zo met een listbox met twee kolommen:
Code:
Private Sub UserForm_Initialize()
    'Read setup file/parameters for dropbox
    file = "D:\Setup.csv"
    i = 0
    UserForm1.ListBox2.Clear
    Open file For Input As #1
    Do Until EOF(1)
        Line Input #1, strLine: 'Debug.Print strLine
        i = i + 1
        ListBox2.AddItem
        ListBox2.List(i- 1, 0) = Split(strLine, ";")(0)
        ListBox2.List(i - 1, 1) = Split(strLine, ";")(1)
    Loop
    Close #1
End Sub

Private Sub ListBox2_Click()
    MsgBox ListBox2.List(ListBox2.ListIndex, 1)
End Sub
 

Bijlagen

  • UserForm1.zip
    1,1 KB · Weergaven: 5
Yes... bedankt A.Hulpje.
Belangrijke wijzigingen waren het verplaaatsen van de code naar de objecten en het gebruik van een 2dim Listbox.
Het hoofdprogramma ziet er nu als volgt uit

Code:
'This routine will store selected e-mails with a specified name in the specified map (selection in a dropbox)
'Date 2023-11-13
Sub MailtoFile()
Userform1.Show
End Sub

De code binnen Userform1 luidt
Code:
Private Sub Commandbutton1_Click()
    Dim myItem As Object
    Dim olMail As Outlook.MailItem
    Dim Selection As Selection
    
    Dim strSender As String 'Names will be shortened of extented to fix number of characters
    'Dim strDate As String ', strYear, strMonth, strDayr, strHour, strMinute As String 'myItem.CreationTime shall be converter to yyyy-mm-dd hh:mm
    Dim strDate As Long
    Dim strPath As String

    strPath = ListBox2.List(ListBox2.ListIndex, 1)
    MsgBox strPath
    'Action with loop for all selected e-mails
  
    Set myNamespace = Application.GetNamespace("MAPI")
    Set Selection = Application.ActiveExplorer.Selection
    n = 0
    For Each myItem In Selection
        n = n + 1
        Set olMail = Application.ActiveExplorer().Selection(n)
        MsgBox olMail.Subject & vbCrLf & myItem.CreationTime & vbCrLf & myItem.Sendername
        strDate = myItem.CreationTime
        'ToDo: Convert strDate in the format "yyyy-mm-dd hh:mm"
        strSender = myItem.Sendername
        'ToDo: Convert the length of strSender to a fixed figures (that can be shorten or extend)
        strSubject = myItem.Subject
    
        'Store each selected e-mail in a specified map
        'Mailobject.SaveAs strPath & "\" & strDate & " | " & strSender & "|" & olMail.Subject & ".msg", olMSG
        
        'Delete the selected e-mail
        'olMail.Delete
        
        Next  'Next mail
        
        Userform1.hide
        
End Sub


Private Sub UserForm_Initialize()
    'Read setup file/parameters for dropbox
    file = "D:\Setup.csv"
    i = 0
    Userform1.ListBox2.Clear
    Open file For Input As #1
    Do Until EOF(1)
        Line Input #1, strLine: 'Debug.Print strLine
        i = i + 1
        ListBox2.AddItem
        ListBox2.List(i - 1, 0) = Split(strLine, ";")(0)
        ListBox2.List(i - 1, 1) = Split(strLine, ";")(1)
    Loop
    Close #1
End Sub
 
In een filenaam mag geen : voorkomen, dus datum converteer je zo:
strDate = Format(myItem.CreationTime, "yyyymmdd hhmm")
Vaste lengte voor een string, in dit geval 20 lang:
Left("abcde" & String(20,"x"),20)
 
Opnieuw bedankt aHulpje

Ook in "strSubject = myItem.Subject" kunnen lllegale tekens voorkomen, die niet in de bestandsnamen mogen voorkomen.
Ik ga zoeken hoe ik die illegale tekens verwijder.
 
Je kan er deze functie voor gebruiken:
Code:
Function StripChar(str As String) As String
    na = "<>:\/|?*" & Chr(34)
    For i = 1 To Len(str)
        If InStr(1, na, Mid(str, i, 1)) = 0 Then
            StripChar = StripChar & Mid(str, i, 1)
        End If
    Next i
End Function

Voorbeeld gebruik:
Code:
Sub Voorbeeld()
    strSubject = StripChar(myItem.Subject)
End Sub
 
Ik raak in verwarring betreft ontoelaatbare tekens is bestandsnamen.

Ik heb in mijn archief een bestand met de volgende naam: "Niet leesbaar [«Jan Groot][2023-10-06 10.41.25].msg", dus toch met "-" en ":". Dit bestand is gewoon te openen.
Als je googled op "illegal characters in filename" krijg je een nog langer lijst met verboden tekens in bestandsnamen dan die van Ed.

Graag wil ik wat meer achtergrondinformatie.
 
Een - teken is toegestaan.
Waar in je voorbeeld bestandsnaam zie je een dubbele punt?

Tekens.png
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan