Help!

PC-Problemen?
De vrijwilligers van Oplossing.be zoeken gratis met u mee!

Hulp bij posten

Recente topics

Auteur Topic: Commandbar aanroepen  (gelezen 1195 keer)

0 leden en 1 gast bekijken dit topic.

Offline Richard2000

  • Lid
  • *
  • Berichten: 24
  • Geslacht: Man
  • Oplossing.be
Commandbar aanroepen
« Gepost op: 15 april 2021, 11:12:41 »
Hallo,

Ik probeer een commandbar te maken en dat lijkt te lukken maar krijg deze niet aangeroepen met een cmd knop, ik ben nog steeds een beetje een leek op excel VBA en een commandbar is helemaal nieuw en kan er helaas weinig over vinden op het net, voor zover ik begrijp moet je een commandbar in een klasse module bouwen en gaat het oproepen van de sub anders dan in een form of module, ik heb dezelfde commandbar ook in een norm. module gezet maar dan de variable's zonder event's en dan lukt het oproepen wel maar dan werken de buttons zelf niet, en in de klasse module krijg ik een variable niet gedefineerd maar welke?
Dus nou twijfel ik of het nu in een normale of een klasse module moet? Googlen levert niets op en wat ik vind krijg ik ook niet aan de praat met dezelfde variable fout dus wat doe ik nou verkeerd?

Hieronder de code en het gaat om menubestand, hoe roep ik die aan?

Option Explicit

Public WithEvents Opslaan As CommandBarButton
Public WithEvents OpslAls As CommandBarButton
Public WithEvents Opvul1 As CommandBarButton
Public WithEvents Export As CommandBarButton
Public WithEvents Import As CommandBarButton
Public WithEvents Opvul2 As CommandBarButton
Public WithEvents Printer As CommandBarButton
Public WithEvents Opvul3 As CommandBarButton
Public WithEvents Afsluiten As CommandBarButton

Public DateString As String
Public FolderName As String
Public wsExp As Worksheet
Public Map As String

Function MenuBestand()

    Public oCmbar As CommandBar
    Set oCmbar = Application.CommandBars.Add(Position:=msoBarPopup, temporary:=True)
           
        Set Opslaan = oCmbar.Controls.Add(msoControlButton)
        Set OpslAls = oCmbar.Controls.Add(msoControlButton)
        Set Opvul1 = oCmbar.Controls.Add(msoControlButton)
        Set Export = oCmbar.Controls.Add(msoControlButton)
        Set Import = oCmbar.Controls.Add(msoControlButton)
        Set Opvul2 = oCmbar.Controls.Add(msoControlButton)
        Set Printer = oCmbar.Controls.Add(msoControlButton)
        Set Opvul3 = oCmbar.Controls.Add(msoControlButton)
        Set Afsluiten = oCmbar.Controls.Add(msoControlButton)
               
        With Opslaan
            .Style = msoButtonIconAndCaption
            .Height = 30
            .Width = 120
            .FaceId = 19
            .Caption = "Opslaan"
        End With
       
        With OpslAls
            .Style = msoButtonIconAndCaption
            .Height = 30
            .Width = 120
            .FaceId = 21
            .Caption = "Opsl. Als"
        End With
       
         With Opvul1
            .Height = 15
            .Width = 120
        End With
       
        With Export
            .Style = msoButtonIconAndCaption
            .Height = 30
            .Width = 120
            .FaceId = 22
            .Caption = "Export"
        End With
       
        With Import
             .Style = msoButtonIconAndCaption
             .Height = 30
            .Width = 120
             .FaceId = 39
             .Caption = "Import"
        End With
       
         With Opvul2
            .Style = msoButtonIcon
            .Height = 15
            .Width = 120
        End With
       
         With Printer
            .Style = msoButtonIconAndCaption
            .Height = 30
            .Width = 120
            .FaceId = 22
            .Caption = "Printer"
        End With
       
         With Opvul3
            .Style = msoButtonIcon
            .Height = 15
            .Width = 120
        End With
       
         With Afsluiten
            .Style = msoButtonIconAndCaption
            .Height = 30
            .Width = 120
            .FaceId = 22
            .Caption = "Afsluiten"
        End With
               
        oCmbar.ShowPopup (20), (60)
        oCmbar.Delete
   
End Function

Private Sub Opslaan_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)

      ProgOpslaan
End Sub

Private Sub OpslALs_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)

      ProgOpslAls
End Sub

Private Sub Export_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
      BestandExport
End Sub

Private Sub Import_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
     
End Sub

Sub ProgOpslaan(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
Application.ThisWorkbook.Save
End Sub
Sub ProgOpslAls(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
Map = Application.ThisWorkbook.Path
Application.Dialogs(xlDialogSaveAs).Show (Map)
End Sub
Sub BestandExport(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
'Updateby20200806
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim xWs As Worksheet
Dim xWb As Workbook
Dim xNWb As Workbook

Dim xFile As String


Application.ScreenUpdating = False
Set xWb = Application.ThisWorkbook
 
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = xWb.Path & "\" & "export" & "\" & xWb.Name & wsExp.Name & " " & DateString
FileExtStr = ".xlsb": FileFormatNum = 50
 
MkDir FolderName
 
    wsExp.Select
    wsExp.Copy
    xFile = FolderName & "\" & wsExp.Name & FileExtStr
    Set xNWb = Application.Workbooks.Item(Application.Workbooks.Count)
    xNWb.SaveAs xFile, FileFormat:=FileFormatNum
    xNWb.Close False, xFile

   
    Application.ScreenUpdating = True
   
End Sub


Offline Montagnard

  • Ambassadeur
  • *****
  • Berichten: 2.385
  • Geslacht: Man
  • To Windows or not to Windows , that's the question
Re: Commandbar aanroepen
« Reactie #1 Gepost op: 16 april 2021, 08:40:24 »
sry , mijn kennis reikt niet ver genoeg , hopelijk kan een andere "oplosser/helper" U wel helpen...
Mvg,
Montagnard.
HP ProBook 450 G3 15 inch
OS : Windows 11 Pro NLD 64bit
Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
16 GB  Vengeance 2666 MHz
KINGSTON SA400S37480G - 447,13 GB als bootdisk
WD Blue Solid state 2 TB - intern - M.2 2280 - SATA als datadisk
AV: Windows Defender
FW: Windows Firewal

Offline Richard2000

  • Lid
  • *
  • Berichten: 24
  • Geslacht: Man
  • Oplossing.be
Re: Commandbar aanroepen
« Reactie #2 Gepost op: 16 april 2021, 11:34:19 »
Geen probleem, iig bedankt voor het meedenken :thumbsup:, ik pruts ondertussen gewoon lekker verder  :)

Offline dizzl

  • Ambassadeur
  • *****
  • Berichten: 2.345
  • I've Upped my standards,now,up yours!
Re: Commandbar aanroepen
« Reactie #3 Gepost op: 16 april 2021, 15:21:12 »
Nu open je een formulier bij knop klik.

Geen idee wanneer je het menu wilt zichtbaar maken, maar als het het op knopklik wilt dan moet je dit oproepen:

Private Sub CommandButton1_Click()
   'Open het menu
   MenuBestand
   'open het formulier
   UserForm1.Show
End Sub
Systeem  : Intel Core i5-4440 3.1Ghz, 3.1GHz 16Giga Ram 64Bits
Software : Windows 10 professional, Avira, Office 2016 Professional

Offline Richard2000

  • Lid
  • *
  • Berichten: 24
  • Geslacht: Man
  • Oplossing.be
Re: Commandbar aanroepen
« Reactie #4 Gepost op: 17 april 2021, 12:41:56 »
Nee helaas, dat is hem ook niet, daarmee open je de sub in de gewone module en het moet die zijn die in de klasse module staat, maar iig bedankt voor je meedenken  :thumbsup:

Maar ik heb het gevonden, het moet vanuit een variable aangeroepen worden en het lukt mij alleen via de userforms controls, dus of het zo hoort durf ik niet te zeggen, daar moeten de experts zich maar over buigen  :)

De code is iig wel lekker kort en makkelijk in te planten, hieronder wat in je userform gaat,

Option Explicit

Public MenBes As kmdBestand

Private Sub UserForm_Initialize()
 
   Set MenBes = New kmdBestand
   Set MenBes.MenuBestand = CommandButton1 ' naam van je commandbutton
   
  End Sub
 
Private Sub CommandButton1_click()

End Sub

Bij je commandbutton is geen code nodig, alles op de public na zit onder initialize

En hier de code die in je klasse module gaat voor 4 knoppies in het midden van je scherm,

Option Explicit

Private WithEvents CopyCtl As CommandBarButton
Private WithEvents CutCtl As CommandBarButton
Private WithEvents PasteCtl As CommandBarButton
Private WithEvents OpenLink As CommandBarButton
Public WithEvents MenuBestand As CommandButton

Private Sub MenuBestand_click()

Dim oCmbar As CommandBar
     
Set oCmbar = Application.CommandBars.Add(Position:=msoBarPopup, temporary:=True)
Set CopyCtl = oCmbar.Controls.Add(msoControlButton)
Set CutCtl = oCmbar.Controls.Add(msoControlButton)
Set PasteCtl = oCmbar.Controls.Add(msoControlButton)
Set OpenLink = oCmbar.Controls.Add(msoControlButton)
               
        With CopyCtl
            .Style = msoButtonIconAndCaption
            .FaceId = 19
            .Caption = "Copy"
    End With
       
        With CutCtl
            .Style = msoButtonIconAndCaption
            .FaceId = 21
            .Caption = "Cut"
    End With
       
        With PasteCtl
            .Style = msoButtonIconAndCaption
            .FaceId = 22
            .Caption = "Paste"
    End With
       
        With OpenLink
             .Style = msoButtonIconAndCaption
             .FaceId = 39
             .Caption = "Open"
    End With
               
        oCmbar.ShowPopup (550), (200)
        oCmbar.Delete

End Sub

Private Sub CopyCtl_Click(ByVal ctrl As Office.CommandBarButton, CancelDefault As Boolean)
 
    'code
 
End Sub

Private Sub CutCtl_Click(ByVal ctrl As Office.CommandBarButton, CancelDefault As Boolean)

    'code

End Sub

Private Sub PasteCtl_Click(ByVal ctrl As Office.CommandBarButton, CancelDefault As Boolean)
   
    'code
   
End Sub

Private Sub OpenLink_Click(ByVal ctrl As Office.CommandBarButton, CancelDefault As Boolean)

    'code
     
End Sub

Klasse module wel omnamen naar kmdBestand, en de 550 en 200 na popup is de positie.

Hoop dat iemand er wat aan heeft, en allemaal bedankt voor het meedenken!

Offline Montagnard

  • Ambassadeur
  • *****
  • Berichten: 2.385
  • Geslacht: Man
  • To Windows or not to Windows , that's the question
Re: Commandbar aanroepen
« Reactie #5 Gepost op: 19 april 2021, 13:49:16 »
Fijn dat je zelf de oplossing hebt gevonden !
al doende leert men  ;)....
Mvg,
Montagnard.
HP ProBook 450 G3 15 inch
OS : Windows 11 Pro NLD 64bit
Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
16 GB  Vengeance 2666 MHz
KINGSTON SA400S37480G - 447,13 GB als bootdisk
WD Blue Solid state 2 TB - intern - M.2 2280 - SATA als datadisk
AV: Windows Defender
FW: Windows Firewal

 


www.combell.com