È soltanto un Pokémon con le armi o è un qualcosa di più? Vieni a parlarne su Award & Oscar!

Excel Forum Per condividere esperienze su Microsoft Excel

MessageBox personalizzato

  • Messaggi
  • OFFLINE
    BG66
    Post: 365
    Registrato il: 13/12/2015
    Città: MILANO
    Età: 58
    Utente Senior
    2010
    00 30/08/2022 10:52
    Ciao a tutti.
    Vorrei avere un messageBox con scelta personalizzate (Importa DB - Importa Elenco - Importa Entrambi).
    In passato NON era fattibile ( le scelte YES/NO e similari erano obbligate) e quindi ho sempre usato un surrogato molto brutto esteticamente.

    E' cambiato qualcosa?

    Grazie in anticipo.
    BG66
    Excel 2010
  • OFFLINE
    alfrimpa
    Post: 4.843
    Registrato il: 21/06/2013
    Città: NAPOLI
    Età: 70
    Utente Master
    Excel 365
    00 30/08/2022 11:23
    No non è cambiato nulla.

    Se il MsgBox classico non ti va bene devi creare una userform che gestirai con il VBA quindi potrai fare quello che vuoi.

    Alfredo
  • OFFLINE
    BG66
    Post: 365
    Registrato il: 13/12/2015
    Città: MILANO
    Età: 58
    Utente Senior
    2010
    00 30/08/2022 13:01
    Ciao Alfredo.
    Grazie per la risposta.
    A tuo avviso c'è un modo per copiare lo stile della message box standard?

    [Modificato da BG66 30/08/2022 13:04]
    BG66
    Excel 2010
  • OFFLINE
    alfrimpa
    Post: 4.845
    Registrato il: 21/06/2013
    Città: NAPOLI
    Età: 70
    Utente Master
    Excel 365
    00 30/08/2022 13:49
    Assolutamente si.

    Dall’esempio che hai fatto devi creare una userform con una textbox e tre commandbutton ad ognuno dei quali deve essere associato il codice per compiere l’azione che devono eseguire.

    Alfredo
  • OFFLINE
    BG66
    Post: 366
    Registrato il: 13/12/2015
    Città: MILANO
    Età: 58
    Utente Senior
    2010
    00 30/08/2022 14:08
    Ciao Alfredo,
    nel frattempo sono andato avanti.
    Ora ho bisogno di te per capire dove nello script Main devo inserire gli IF .
    In pratica vorrei schiacciando:
    a) Lavoratori ( quindi 1) esegui questo script ( per semplicità; simuliamo un messaggebox"Caricato DB Lavorati"
    b) Documenti( quindi 2) esegui questo script ( per semplicità; simuliamo un messaggebox"Caricato DB Documenti"
    c) Entrambi ( quindi 3) esegui questo script ( per semplicità; simuliamo un messaggebox"Caricato Tutti DB"
    d) Chiudi ( quindi e) esegui questo esci dal messaggio.

    Mi basta la cdritta su dove posizionare il primo if e poi continuo io.

    Grazie se puoi.

    PS Non inserisco lo script perchè non mi tiene l'impaginazione ma nel file è presente.
    [Modificato da BG66 30/08/2022 14:12]
    BG66
    Excel 2010
  • OFFLINE
    alfrimpa
    Post: 4.846
    Registrato il: 21/06/2013
    Città: NAPOLI
    Età: 70
    Utente Master
    Excel 365
    00 30/08/2022 14:56
    Sono in vacanza e non ho un pc.

    Se pubblichi il codice che hai scritto con il cellulare riesco a vederlo

    Alfredo
  • OFFLINE
    BG66
    Post: 367
    Registrato il: 13/12/2015
    Città: MILANO
    Età: 58
    Utente Senior
    2010
    00 30/08/2022 16:02
    Eccolo.

     
    Option Explicit
    '
    Private Type MSGBOXPARAMS
        cbSize As Long
        hwndOwner As Long
        hInstance As Long
        lpszText As Long
        lpszCaption As Long
        dwStyle As Long
        lpszIcon As Long
        dwContextHelpId As Long
        lpfnMsgBoxCallback As Long
        dwLanguageId As Long
    End Type
    '
    Private Type MSGBOXDATA_Earlier
        params As MSGBOXPARAMS
        pwndOwner As Long
        wLanguageId As Long
        pidButton As Long
        ppszButtonText As Long
        cButtons As Long
        DefButton As Long
        CancelId As Long
        Timeout As Long
    End Type
    '
    Private Type MSGBOXDATA_Later
        params As MSGBOXPARAMS
        unk1 As Long
        pwndOwner As Long
        wLanguageId As Long
        pidButton As Long
        ppszButtonText As Long
        cButtons As Long
        DefButton As Long
        CancelId As Long
        Timeout As Long
        unk2(5) As Long
    End Type
    '
    Private Declare Function SoftModalMessageBox_Earlier Lib "user32" Alias "SoftModalMessageBox" (pMsgBoxParams As MSGBOXDATA_Earlier) As Long
    Private Declare Function SoftModalMessageBox_Later Lib "user32" Alias "SoftModalMessageBox" (pMsgBoxParams As MSGBOXDATA_Later) As Long
    '
    
    Public Sub Main()
        Dim md_Earlier      As MSGBOXDATA_Earlier
        Dim md_Later        As MSGBOXDATA_Later
        Dim lBtn(3)         As Long
        Dim sTxt(3)         As String
        Dim iRet            As Long
        '
    
    
        sTxt(0) = "Lavoratori"
        lBtn(0) = 1
        sTxt(1) = "Documenti"
        lBtn(1) = 2
        sTxt(2) = "Entrambi"
        lBtn(2) = 3
        sTxt(3) = "Nessuno"
        lBtn(3) = 4
        '
        If WindowsVersi0n() < 61 Then ' Vista and below use earlier structure.
            With md_Earlier
                With .params
                    .cbSize = LenB(md_Earlier.params)
                    .lpszText = StrPtr("Quali dati vuoi aggiornare?")
                    .lpszCaption = StrPtr("VBforums")
                    .dwStyle = vbQuestion Or vbYesNoCancel
                End With
                .DefButton = 0
                .CancelId = 2
                .cButtons = 4
                .pidButton = VarPtr(lBtn(0))
                .ppszButtonText = VarPtr(sTxt(0))
                .Timeout = -1
            End With
            iRet = SoftModalMessageBox_Earlier(md_Earlier)
            MsgBox "Our custom message box returned: " & Format$(iRet)
        Else
            With md_Later
                With .params
                    .cbSize = LenB(md_Later.params)
                    .lpszText = StrPtr("Quali dati vuoi aggiornare?")
                    .lpszCaption = StrPtr("VBforums")
                    .dwStyle = vbQuestion Or vbYesNoCancel
                End With
                .DefButton = 0
                .CancelId = 2
                .cButtons = 4
                .pidButton = VarPtr(lBtn(0))
                .ppszButtonText = VarPtr(sTxt(0))
                .Timeout = -1
            End With
            iRet = SoftModalMessageBox_Later(md_Later)
            MsgBox "Our custom message box returned: " & Format$(iRet)
        End If
    
    End Sub
    
    Public Function WindowsVersi0n() As Long
        '    OS              WindowsVersion return
        ' Windows 10                100
        ' Windows 8.1                63
        ' Windows 8.0                62
        ' Windows 7                  61
        ' Windows Vista              60
        ' Windows XP                 51
        ' Windows 2000               50
        Dim SystemSet As Object
        Dim System As Object
        Dim s As String
        Dim i As Long
        '
        Set SystemSet = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
        For Each System In SystemSet: s = System.Version: Next
        '
        i = InStr(s, ".")
        s = Left$(s, i - 1) & Mid$(s, i + 1)
        i = InStr(s, ".")
        If i Then s = Left$(s, i - 1)
        WindowsVersion = Val(s)
    End Function
    
    [Modificato da BG66 30/08/2022 16:03]
    BG66
    Excel 2010
  • OFFLINE
    dodo47
    Post: 3.339
    Registrato il: 06/04/2013
    Utente Master
    2010
    00 30/08/2022 16:43
    ciao Gene
    perchè ti complichi la vita? Alfredo aveva suggerito una uForm nella quale inserire i vari pulsanti.

    Comunque verso la fine della Public Sub Main, hai:
    MsgBox "Our custom message box returned: " & Format$(iRet)

    pertanto la variabile iRet ( secondo la tua scelta) conterrà 1,2,3 oppure 4, corrispondenti ai 4 pulsanti da te inseriti.

    A questo punto, al posto del msgBox potresti utilizzare un Select case iRet....

    saluti



    Domenico
    Win 10 - Excel 2016
  • OFFLINE
    alfrimpa
    Post: 4.847
    Registrato il: 21/06/2013
    Città: NAPOLI
    Età: 70
    Utente Master
    Excel 365
    00 30/08/2022 17:02
    @bg66

    Ma cosa fa il codice che hai postato?

    Puoi spiegarlo?


    Alfredo
  • OFFLINE
    BG66
    Post: 368
    Registrato il: 13/12/2015
    Città: MILANO
    Età: 58
    Utente Senior
    2010
    00 30/08/2022 20:43
    Ciao Domenico,
    sai che la mia capa tosta non mi permette di fare le cose facili.
    Credo di aver capito il tuo suggerimento. Questo è il risultato:
    Select Case iRet
      
            Case Is = 1
            MsgBox "Hai scelto l'opzione Lavoratori"
        
            Case Is = 2
            MsgBox "Hai scelto l'opzione Documenti"
     
            Case Is = 3
            MsgBox "Hai scelto l'opzione Entrambi"
     
            'Case Is = 4
            'MsgBox "Hai scelto di interrompere lo script"
    
            End Select
            'MsgBox "Our custom message box returned: " & Format$(iRet)
        End If
    



    Ciao Alfredo
    spero che mi permetterà di usare quattro pulsanti/opzioni con la veste grafica standard Microsoft.

    Buona serata.
    [Modificato da BG66 30/08/2022 21:05]
    BG66
    Excel 2010
  • OFFLINE
    rollis13
    Post: 1.264
    Registrato il: 16/08/2015
    Città: CORDENONS
    Età: 67
    Utente Veteran
    Excel 2016-32bit Win11
    00 30/08/2022 23:07
    Un saluto a tutti.
    Esatto @BG66, adesso devi solo sostituire i 3 MsgBox con le 3 Call alle rispettive macro.

    ______________________________________________________________
    C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)