• 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.

'Als dan' verwerken in een macro

Status
Niet open voor verdere reacties.
Als ik met F8 er doorheen loop dan geeft VBA geen melding weer. De fout controle verspringt met geel gearceerde delen door de code zonder melding.

Het enige wat verschilt is dat in de 4e en de laatste IF regel het dik gedrukte deel ook geel wordt gearceerd als ik met F8 er doorheen loop. Bij de andere regels wordt alleen het eerste deel van de regel geel gearceerd (vb: If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C30] = True Then). Betekent dit iets?

Sub Document_Openen()
On Error GoTo Oeps

Dim objWordApp As Object, objWordDoc As Object

If [B26] <> "Bilateral Refund" And _
[B26] <> "Bilateral Reduction" And _
[B26] <> "EU-Treaty" And _
[B26] <> "Domestic law" Then
MsgBox "No Possibilities !": Exit Sub
End If

If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C30] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C30] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C31] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C31] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C32] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C32] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C33] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C33] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C34] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C34] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C35] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C35] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C36] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C36] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C37] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C37] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C38] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C38] = True Then bestand = "D:\bla\bla\word document"
Set objWordApp = CreateObject("Word.application")
objWordApp.Visible = True
Set objWordDoc = objWordApp.Documents.Open(bestand)
Oeps:
End Sub
 
Ben je zeker dat de selectievakjes naar resp. C31 en C38 verwijzen?
 
Yes, 100% zeker. Ik heb even alle bedrijven afzonderlijk getest en dan werkt het prima; excel laat de gewenste brief zien vor dat bedrijf. Het probleem is dat zodra ik bij meerdere bedrijven een vinkje zet excel ze niet allemaal opent..?..
 
Kun je svp VBA code altijd tussen code-markeringen (code tags) zetten ?

Zovaak dezelfde cel testen lijkt me een beetje moeizaam.

Code:
y=left([B26],1)="B")*application.match(true,[C30:C38],0)
 
Laatst bewerkt:
Beste SNB, Dank voor je reactie en hulp!

Het zou mooi zijn om de code in te korten echter heb ik niet genoeg kennis om te weten welke code ik moet vervangen met jouw regel en waar precies tussenin deze moet komen te staan. Zou je me kunnen voorzien van een iets completer beeld?

De huidige code werkt als ik maar 1 bedrijf aanvink; als ik meerdere bedrijven aanvink dan opent excel niet alle brieven. Zal jouw regel dit oplossen?

Bij voorbaat dank voor je hulp!
 
Bestand krijgt in jouw code altijd de waarde van het laatst gevonden bestand. En er zal dus maar één document geopend worden. Wat je kan zien als je met <F8> door de code loopt.

Bestand zal dan zoiets moeten worden: bestand = bestand & "|" & pad + naam

Om de bestanden vervolgens te openen zal je een lusje moeten gebruiken zoiets
Code:
For j = 0 to ubound(split(mid(bestand,2),"|"))
   msgbox split(mid(bestand,2),"|")(j)
next j
 
Beste VenA,

Top! Het klopt helemaal wat je zegt. Bij elke test kwam het laatst gekozen bedrijf naar voren.. Dank voor je hulp!

Waar precies moet ik dit stukje code plakken? En is dit ter vervanging van een ander stukje code? Alvast bedankt!

Huidige code:

Sub Document_Openen()
On Error GoTo Oeps

Dim objWordApp As Object, objWordDoc As Object

If [B26] <> "Bilateral Refund" And _
[B26] <> "Bilateral Reduction" And _
[B26] <> "EU-Treaty" And _
[B26] <> "Domestic law" Then
MsgBox "No Possibilities !": Exit Sub
End If

If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C30] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C30] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C31] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C31] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C32] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C32] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C33] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C33] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C34] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C34] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C35] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C35] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C36] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C36] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C37] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C37] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C38] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C38] = True Then bestand = "D:\bla\bla\word document"
Set objWordApp = CreateObject("Word.application")
objWordApp.Visible = True
Set objWordDoc = objWordApp.Documents.Open(bestand)
Oeps:
End Sub
 
Blijkbaar de eerste zin uit #24 gemist. Probeer eens wat uit met de aangereikte suggesties ipv van steeds hetzelfde te plaatsen. bestand = "D:\bla\bla\word document 2" had bv al aangepast moeten zijn. Ik zie daar niets van terug.
 
Excuus, ik begrijp nu wat wordt bedoelt met code tags. De bestandsnamen zijn in mijn bestand per regel uniek; ik mag deze helaas niet openbaar plaatsen ivm privacy. Ik zal voor de duidelijkheid de regels wel uniek maken hier. Zoals ik eerder heb aangegeven is VBA code voor mij nog nieuw en wil mij daar meer in verdiepen. Helaas is deze kennis er op dit moment nog niet. Zoals ik al aangaf weet ik niet waar ik jouw code tussen moet plaatsen en/of met welke code ik het moet vervangen. Wellicht kan je me daarmee helpen om het geheel werkend te maken? Overigens ben ik de hele dag al aan het knutselen om dit werkend te krijgen maar helaas zonder resultaat, ik probeer dus wel maar kom niet verder. Bij voorbaat dank.

Hieronder dan nu de aangepaste code tussen de code tags:

Code:
Sub Document_Openen()
On Error GoTo Oeps

Dim objWordApp As Object, objWordDoc As Object
 
  If [B26] <> "Bilateral Refund" And _
     [B26] <> "Bilateral Reduction" And _
     [B26] <> "EU-Treaty" And _
     [B26] <> "Domestic law" Then
         MsgBox "No Possibilities !": Exit Sub
 End If
 
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C30] = True Then bestand = "D:\bla\bla\word document 1A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C30] = True Then bestand = "D:\bla\bla\word document 1B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C31] = True Then bestand = "D:\bla\bla\word document 2A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C31] = True Then bestand = "D:\bla\bla\word document 2B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C32] = True Then bestand = "D:\bla\bla\word document 3A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C32] = True Then bestand = "D:\bla\bla\word document 3B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C33] = True Then bestand = "D:\bla\bla\word document 4A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C33] = True Then bestand = "D:\bla\bla\word document 4B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C34] = True Then bestand = "D:\bla\bla\word document 5A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C34] = True Then bestand = "D:\bla\bla\word document 5B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C35] = True Then bestand = "D:\bla\bla\word document 6A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C35] = True Then bestand = "D:\bla\bla\word document 6B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C36] = True Then bestand = "D:\bla\bla\word document 7A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C36] = True Then bestand = "D:\bla\bla\word document 7B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C37] = True Then bestand = "D:\bla\bla\word document 8A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C37] = True Then bestand = "D:\bla\bla\word document 8B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C38] = True Then bestand = "D:\bla\bla\word document 9A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C38] = True Then bestand = "D:\bla\bla\word document 9B"
  Set objWordApp = CreateObject("Word.application")
  objWordApp.Visible = True
 Set objWordDoc = objWordApp.Documents.Open(bestand)
Oeps:
End Sub
 
Hallo,

Is er niemand die mij zou kunnen helpen met mijn post? Het voelt alsof alleen het stukje code uit post #24 en/of post #26 in de huidige code geplaatst moet worden. Echter weet ik niet waar en na proberen lukt het me ook niet. Bij voorbaat dank.
 
Zet achter elke regel na Then deze opdracht:
Code:
bestand = bestand & "D:\bla\bla\word document 2" &"|"

en op het einde voeg je de splitactie van post #24 toe.
Dan zou het moeten functioneren.

PS. Wel elke keer de bestandsnaam aanpassen.
 
Waar heb je het geprobeerd? En wat lukt er niet? Krijg je een foutmelding of gebeurt er niets?

Het lijkt mij dat het zoiets moet worden
Code:
For j = 0 to ubound(split(mid(bestand,2),"|"))
   Set objWordDoc = objWordApp.Documents.Open(split(mid(bestand,2),"|")(j))
next j
 
Wederom dank voor jullie reactie. Ik heb de code aangepast maar helaas nog niet het gewenste resultaat: MS Word wordt wel geopend maar zonder enig document. Ik heb achter elke THEN de code van Cobe gevoegd:
Code:
bestand = bestand & "D:\bla\bla\word document 2" &"|"

en onderaan aan het einde de code van VenA:
Code:
For j = 0 to ubound(split(mid(bestand,2),"|"))
   Set objWordDoc = objWordApp.Documents.Open(split(mid(bestand,2),"|")(j))
next j

Heb ik de code op de juiste plek toegevoegd?

De code is nu als volgt:

Code:
Sub Document_Openen()
On Error GoTo Oeps

Dim objWordApp As Object, objWordDoc As Object
 
  If [B26] <> "Bilateral Refund" And _
     [B26] <> "Bilateral Reduction" And _
     [B26] <> "EU-Treaty" And _
     [B26] <> "Domestic law" Then
         MsgBox "No Possibilities !": Exit Sub
 End If
 
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C30] = True Then bestand = bestand & "D:\bla\bla\word document 1A" & "|"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C30] = True Then bestand = bestand & "D:\bla\bla\word document 1B" & "|"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C31] = True Then bestand = bestand & "D:\bla\bla\word document 2A" & "|"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C31] = True Then bestand = bestand & "D:\bla\bla\word document 2B" & "|"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C32] = True Then bestand = bestand & "D:\bla\bla\word document 3A" & "|"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C32] = True Then bestand = bestand & "D:\bla\bla\word document 3B" & "|"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C33] = True Then bestand = bestand & "D:\bla\bla\word document 4A" & "|"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C33] = True Then bestand = bestand & "D:\bla\bla\word document 4B" & "|"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C34] = True Then bestand = bestand & "D:\bla\bla\word document 5A" & "|"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C34] = True Then bestand = bestand & "D:\bla\bla\word document 5B" & "|"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C35] = True Then bestand = bestand & "D:\bla\bla\word document 6A" & "|"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C35] = True Then bestand = bestand & "D:\bla\bla\word document 6B" & "|"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C36] = True Then bestand = bestand & "D:\bla\bla\word document 7A" & "|"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C36] = True Then bestand = bestand & "D:\bla\bla\word document 7B" & "|"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C37] = True Then bestand = bestand & "D:\bla\bla\word document 8A" & "|"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C37] = True Then bestand = bestand & "D:\bla\bla\word document 8B" & "|"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C38] = True Then bestand = bestand & "D:\bla\bla\word document 9A" & "|"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C38] = True Then bestand = bestand & "D:\bla\bla\word document 9B" & "|"
  Set objWordApp = CreateObject("Word.application")
  objWordApp.Visible = True
  For j = 0 To UBound(Split(Mid(bestand, 2), "|"))
   Set objWordDoc = objWordApp.Documents.Open(Split(Mid(bestand, 2), "|")(j))
Next j
Oeps:
End Sub
 
Om te debuggen kan je een msgbox gebruiken

Code:
For j = 0 To UBound(Split(Mid(bestand, 2), "|"))
  msgbox split(mid(bestand,2),"|")(j)
  Set objWordDoc = objWordApp.Documents.Open(Split(Mid(bestand, 2), "|")(j))
Next j

De pipeline "|" moet toegepast worden zoals in #26 aangegeven en niet zoals in #31
 
Ik heb de code aangepast en krijg bij het uitvoeren van de macro voor elk bedrijf dat ik heb aangevinkt om de brief te tonen een message box met daarin vermeld de bestand locatie van de brief (D:bla/bla/.....)

Code als volgt aangepast:

Code:
Sub Document_Openen()
On Error GoTo Oeps

Dim objWordApp As Object, objWordDoc As Object
 
  If [B26] <> "Bilateral Refund" And _
     [B26] <> "Bilateral Reduction" And _
     [B26] <> "EU-Treaty" And _
     [B26] <> "Domestic law" Then
         MsgBox "No Possibilities !": Exit Sub
 End If
 
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C30] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 1A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C30] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 1B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C31] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 2A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C31] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 2B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C32] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 3A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C32] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 3B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C33] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 4A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C33] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 4B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C34] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 5A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C34] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 5B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C35] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 6A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C35] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 6B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C36] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 7A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C36] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 7B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C37] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 8A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C37] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 8B"
 If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C38] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 9A"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C38] = True Then bestand = bestand & "|" & "D:\bla\bla\word document 9B"
  Set objWordApp = CreateObject("Word.application")
  objWordApp.Visible = True
  For j = 0 To UBound(Split(Mid(bestand, 2), "|"))
  MsgBox Split(Mid(bestand, 2), "|")(j)
  Set objWordDoc = objWordApp.Documents.Open(Split(Mid(bestand, 2), "|")(j))
Next j
Oeps:
End Sub
 
excuus ik zie dat naast de messagebox de bestanden wel gewoon geopend worden! :-)) Heel erg blij mee!

Ik neem aan dat ik de messagebox weer kan verwijderen?

Super bedankt!
 
Ook al gelukt! De code werkt perfect! Super bedankt allemaal voor de moeite! :-))
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan