• Privacywetgeving
    Het is bij Helpmij.nl niet toegestaan om persoonsgegevens in een voorbeeld te plaatsen. Alle voorbeelden die persoonsgegevens bevatten zullen zonder opgaaf van reden verwijderd worden. In de vraag zal specifiek vermeld moeten worden dat het om fictieve namen gaat.

commandobutton dat verwijst naar andere button op ander werkblad

Status
Niet open voor verdere reacties.

Tine83

Gebruiker
Lid geworden
6 okt 2014
Berichten
25
Hier ben ik weer met een volgende vraag:
Ik zou graag op blad 1 een commandobutton plaatsen, als ik hierop klik, deze doorklikt op een andere commandoknop op een ander werkblad.

Ik heb een paar vba's uitgeprobeerd, maar de juiste nog iet gevonden.

Code:
Sub Macro1()

Sheets("Outlook").CommandButton("CommandButton20")_Click()
    
End Sub
 
Laatst bewerkt:
Ik ben geen VBA specialist, maar ik zou dezelfde code achter deze commandobutton plaatsen.
 
Hallo Willem, bedankt voor je antwoordje. De vba is zeer complex en gericht op het andere werkblad, ik heb deze via de site gehaald en voor mij is het chinees, maar werkt perfect (stuurt mails in PDFvorm naar verschillende mailadressen). Dus ik zou het 'simpel' willen houden, omdat ik ook geen specialist ben.. ;)
 
Zorg dat de code achter die tweede knop eerst het juiste werkblad actief maakt, dan kun je achter die eerste knop gewoon die click routine van de tweede knop aanroepen.
 
Nee, dat gaat anders. Ik zal ergens vandaag wel een voorbeeldje voor je maken.
 
Hallo edmoor

Dit werkt momenteel al:

Code:
Private Sub CommandButton16_Click()
ThisWorkbook.Worksheets("Outlook").Commandbutton20.Value = True
End Sub

Maar nu opent hij het andere werkblad, maar eigenlijk zou hij op blad "invullen!" moeten blijven. Enig idee?

Dank je voor je hulp!
 
Oplossing is simpel (wat je in bericht #7 schrijft, NIET toepassen).
- Knip van de macro die in het blad "Outlook" bij commandbutton20 hoort, alles wat tussen de eerste en laatste regel staat en schrijf ervoor in de plaats: macro1 (je hebt dan daar 3 regels).
- Kies in het menu: Invoegen - Module.
- Typ daar als eerste regel Sub macro1() en druk op Enter (als het goed is wordt "End Sub" automatisch toegevoegd).
- Plak boven "End Sub" de tekst die je zojuist geknipt hebt.
- Plaats op blad1 een commandbutton, klik er met de rechtermuisknop op en kies "Programmacode weergeven".
- Tussen de eerste en laatste regel typ je: macro1
- Klaar.
 
Laatst bewerkt:
Deze werkt:

Code:
Private Sub CommandButton16_Click()
ThisWorkbook.Worksheets("Outlook").Commandbutton20.Value = True
Worksheets("invullen!").Select
End Sub

Zapatr: De macro zelf kopiëren werkt hier niet. Waarom zou ik bovenstaande code niet kunnen gebruiken?

Bedankt voor jullie hulp!
 
Dat zou ook moeten werken, maar waaraan de voorkeur wordt gegeven, daarover kun je van mening verschillen. Over smaak valt niet te twisten. Vreemd overigens dat je aan een macro niets kunt wijzigen, maar er blijkbaar wel 1 kan toevoegen.
 
Ik ken er niets van, dus smaak en mening kan ik niet geven ;)

Ik heb de macro van RDB (pdf via mail versturen). Als ik deze kopieer zoals in jou voorbeeld, geeft het telkens foutmelding. Iets over ME..
 
Over die fout kan ik niets zeggen als ik het bestand/macro niet kan inzien.
Wat ik hierboven schreef heb ik zelf al tientallen keren gebruikt en dat werkt.
 
Code:
Option Explicit

Private Sub CommandButton20_Click()
Outlook.Select

    Dim StringTo As String, StringCC As String, StringBCC As String
    Dim ShArr() As String, FArr() As String, strDate As String
    Dim myCell As Range, cell As Range, Rng As Range, FName As String, Fname2 As String
    Dim wb As Workbook, sh As Worksheet
    Dim DefPath As String
    Dim olApp As Object
    Dim olMail As Object
    Dim FileExtStr As String

    Dim ToArray As Variant
    Dim CCArray As Variant
    Dim BCCArray As Variant

    Dim StringFileNames As String
    Dim StringSheetNames As String
    Dim FileNamesArray As Variant
    Dim SheetNamesArray As Variant
    Dim i As Long, S As Long, F As Long
    Dim WrongData As Boolean

    If Len(ThisWorkbook.Path) = 0 Then
        MsgBox "This macro will only work if the file is Saved once", 48, "RDBMailPDFOutlook"
        Exit Sub
    End If

    If Me.ProtectContents = True Or ActiveWindow.SelectedSheets.Count > 1 Then
        MsgBox "This macro will not work if the RDBMailOutlook worksheet is " & _
               "protected or if you have more then sheet selected(grouped)", 48, "RDBMailPDFOutlook"
        Exit Sub
    End If

    'Set folder where we save the temporary files
    DefPath = Application.DefaultFilePath
    If Right(DefPath, 1) <> "\" Then
        DefPath = DefPath & "\"
    End If

    'Set reference to Outlook and turn of ScreenUpdating and Events
    Set olApp = CreateObject("Outlook.Application")
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Set cells with Red interior color to no fill(cells with wrong data)
    Range("A6").ListObject.DataBodyRange.Interior.Pattern = xlNone

    'Set rng to the first column of the table
    Set Rng = Me.Range("A6").ListObject.ListColumns(1).Range

    For Each myCell In Rng

        'Create mail if "Yes " in column A
        If LCase(myCell.Value) = "yes" Then

            StringTo = "": StringCC = "": StringBCC = ""
            S = 0: F = 0
            Erase ShArr: Erase FArr

            'Set Error Boolean to False
            WrongData = False

            'Check if there are Sheet names in column B

            'If B is empty S = 0 so you not want to send a sheet or sheets as pdf
            If Trim(Me.Cells(myCell.Row, "B").Value) = "" Then S = 0

            'If there are sheet names in the B column S is the number of sheets it add to the Array
            If LCase(Trim(Me.Cells(myCell.Row, "B").Value)) <> "workbook" Then
                StringSheetNames = Me.Cells(myCell.Row, "B").Value
                SheetNamesArray = Split(StringSheetNames, Chr(10), -1)

                For i = LBound(SheetNamesArray) To UBound(SheetNamesArray)
                    On Error Resume Next
                    If SheetNamesArray(i) <> "" Then
                        If SheetExists(CStr(SheetNamesArray(i))) = False Then
                            Me.Cells(myCell.Row, "B").Interior.ColorIndex = 3
                            WrongData = True
                        Else
                            S = S + 1
                            ReDim Preserve ShArr(1 To S)
                            ShArr(S) = SheetNamesArray(i)
                        End If
                    End If
                    On Error GoTo 0
                Next i
            Else
                'If you only enter "workbook" in colomn B to mail the whole workbook S = -1
                S = -1
            End If

            'Check to Mail addresses in column D
            If Trim(Me.Cells(myCell.Row, "D").Value) <> "" Then
                StringTo = Me.Cells(myCell.Row, "D").Value
                ToArray = Split(StringTo, Chr(10), -1)
                StringTo = ""

                For i = LBound(ToArray) To UBound(ToArray)
                    If ToArray(i) Like "?*@?*.?*" Then
                        StringTo = StringTo & ";" & ToArray(i)
                    End If
                Next i
            End If

            'Check to Mail addresses in column E
            If Trim(Me.Cells(myCell.Row, "E").Value) <> "" Then
                StringCC = Me.Cells(myCell.Row, "E").Value
                CCArray = Split(StringCC, Chr(10), -1)
                StringCC = ""

                For i = LBound(CCArray) To UBound(CCArray)
                    If CCArray(i) Like "?*@?*.?*" Then
                        StringCC = StringCC & ";" & CCArray(i)
                    End If
                Next i
            End If

            'Check to Mail addresses in column F
            If Trim(Me.Cells(myCell.Row, "F").Value) <> "" Then
                StringBCC = Me.Cells(myCell.Row, "F").Value
                BCCArray = Split(StringBCC, Chr(10), -1)
                StringBCC = ""

                For i = LBound(BCCArray) To UBound(BCCArray)
                    If BCCArray(i) Like "?*@?*.?*" Then
                        StringBCC = StringBCC & ";" & BCCArray(i)
                    End If
                Next i
            End If

            If StringTo = "" And StringCC = "" And StringBCC = "" Then
                Me.Cells(myCell.Row, "D").Resize(, 3).Interior.ColorIndex = 3
                WrongData = True
            End If

            'Check the other files that you want to attach in column H
            If Trim(Me.Cells(myCell.Row, "H").Value) <> "" Then
                StringFileNames = Me.Cells(myCell.Row, "H").Value
                FileNamesArray = Split(StringFileNames, Chr(10), -1)

                For i = LBound(FileNamesArray) To UBound(FileNamesArray)
                    On Error Resume Next
                    If FileNamesArray(i) <> "" Then
                        If Dir(FileNamesArray(i)) <> "" Then
                            If Err.number = 0 Then
                                F = F + 1
                                ReDim Preserve FArr(1 To F)
                                FArr(F) = FileNamesArray(i)
                            Else
                                Err.Clear
                                Me.Cells(myCell.Row, "H").Interior.ColorIndex = 3
                                WrongData = True
                            End If
                        Else
                            Me.Cells(myCell.Row, "H").Interior.ColorIndex = 3
                            WrongData = True
                        End If
                    End If
                    On Error GoTo 0
                Next i
            End If

            'Not create the mail if there are Errors in the row (wrong sheet or file names or no mail addresses)
            If WrongData = True Then GoTo MailNot


            'Create PDF and Mail

            'Create Date/time string for the file name
            strDate = Format(Now, "dd-mmm-yyyy hh-mm-ss")

            'Copy the sheet(s)to a new workbook
            If S > 0 Then
                ThisWorkbook.Sheets(ShArr).Copy
                Set wb = ActiveWorkbook
            End If

            'You enter only "workbook" in colomn B to mail the whole workbook
            'Use SaveCopyAs to make a copy of the workbook
            If S = -1 Then
                FileExtStr = "." & LCase(Right(ThisWorkbook.Name, _
                                               Len(ThisWorkbook.Name) - InStrRev(ThisWorkbook.Name, ".", , 1)))
                Fname2 = DefPath & "TempFile " & strDate & FileExtStr

                ThisWorkbook.SaveCopyAs Fname2
                Me.Activate
                Set wb = Workbooks.Open(Fname2)
                Application.DisplayAlerts = False
                wb.Sheets(Me.Name).Delete
                Application.DisplayAlerts = True
                If wb.Sheets(1).Visible = xlSheetVisible Then wb.Sheets(1).Select
            End If


            'Now we Publish to PDF
            If S <> 0 Then
                FName = DefPath & Trim(Me.Cells(myCell.Row, "C").Value) & _
                      " " & strDate & ".pdf"

                On Error Resume Next
                wb.ExportAsFixedFormat _
                        Type:=xlTypePDF, _
                        Filename:=FName, _
                        Quality:=xlQualityStandard, _
                        IncludeDocProperties:=True, _
                        IgnorePrintAreas:=False, _
                        OpenAfterPublish:=False
                On Error GoTo 0
                wb.Close False
                Set wb = Nothing
            End If

            On Error Resume Next
            Set olMail = olApp.CreateItem(0)
            With olMail
                .To = StringTo
                .CC = StringCC
                .BCC = StringBCC
                .Subject = Me.Cells(myCell.Row, "G").Value
                .Body = Me.Cells(myCell.Row, "I").Value
                If S <> 0 Then .Attachments.Add FName

                If F > 0 Then
                    For i = LBound(FArr) To UBound(FArr)
                        .Attachments.Add FArr(i)
                    Next i
                End If

                'Set Importance  0 = Low, 2 = High, 1 = Normal
                If LCase(Me.Cells(myCell.Row, "J").Value) = "yes" Then
                    .Importance = 2
                End If

                'Display the mail or send it directly, see cell C3
                If LCase(Me.Range("C3").Value) = "yes" Then
                    .Display
                Else
                    .Send
                End If


            End With

            If S = -1 Then Kill Fname2
            Kill FName
            On Error GoTo 0

            Set olMail = Nothing

        End If
MailNot:
    Next myCell

    If LCase(Me.Range("C3").Value) = "no" Then
        MsgBox "The macro is ready and if correct the mail or mails are created." & vbNewLine & _
               "If you see Red cells in the table then the information in the cells is " & vbNewLine & _
               "not correct. For example there is a sheet or filename that not exist." & vbNewLine & _
               "Note: It will not create a Mail of the information in a row with a " & vbNewLine & _
               "Red cell or cells.", 48, "RDBMailPDFOutlook"
    End If


    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

    Set olApp = Nothing
End Sub


Function SheetExists(wksName As String) As Boolean
    On Error Resume Next
    SheetExists = CBool(Len(ThisWorkbook.Sheets(wksName).Name) > 0)
    On Error GoTo 0
End Function

Private Sub BrowseAddFiles_Click()
    Dim FName As Variant
    Dim fnum As Long

    If ActiveCell.Column = 8 And ActiveCell.Row > 6 Then
        FName = Application.GetOpenFilename(FileFilter:="All Files (*.*), *.*", _
                                            MultiSelect:=True)
        If IsArray(FName) Then
            For fnum = LBound(FName) To UBound(FName)
                If fnum = 1 And ActiveCell.Value = "" Then
                    ActiveCell.Value = ActiveCell.Value & FName(fnum)
                Else
                    If Right(ActiveCell, 1) = Chr(10) Then
                        ActiveCell.Value = ActiveCell.Value & FName(fnum)
                    Else
                        ActiveCell.Value = ActiveCell.Value & Chr(10) & FName(fnum)
                    End If
                End If
            Next fnum

            With Me.Range("J1").EntireColumn
                .ColumnWidth = 255
                .AutoFit
            End With
            With Me.Rows
                .AutoFit
            End With
        End If
    Else
        MsgBox "Select a cell in the ""Attach other files"" column", 48, "RDBMailPDFOutlook"
    End If
End Sub



Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column > 3 And Target.Column < 7 And Target.Row > 6 Then
        With Range(Target.Address)
            .Hyperlinks.Delete
        End With
    End If
End Sub
 
@Tine,
Om de werking van die macro na te gaan zonder de gegevens van het bestand er bij, dat is lastig en kost veel tijd, tijd die ik nu niet heb. Maar bovenaan zie ik staan: Outlook.select, moet dat niet zijn: Sheets("Outlook").Select ?
Los daarvan, als je macro (zoals je schrijft) normaal wel werkt, dan moet die dat ook nog doen na hetgeen ik schreef, want aan de macro wijzigde ik niets. Dus dan heb je toch, denk ik, niet exact uitgevoerd wat ik schreef. Maar hierboven schreef je dat je het werkend had, wat is dan nog het probleem?
 
Status
Niet open voor verdere reacties.

Nieuwste berichten

Terug
Bovenaan Onderaan