Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[XL07, Word07]Macro om mailmerge te maken vanuit Excel

Pagina: 1
Acties:

  • Paultje3181
  • Registratie: November 2002
  • Laatst online: 19:06
Ik probeer een mailmerge te maken vanuit een Excel-bestand. Het lukt me om het bestand te openen waar het om gaat en ik moet nu de koppeling leggen met het Excel-bestand. Ik heb hiervoor een macro opgenomen in word en deze gekopieerd in mijn Excel-macro.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'Starten Microsoft Word
Const wdFormLetters = 0, wdOpenFormatAuto = 0
Const wdSendToNewDocument = 0, wdDefaultFirstRecord = 1, wdDefaultLastRecord = -16
Dim wd As Object
    Dim wdocSource As Object
    Dim strWorkbookName As String
    On Error Resume Next
    Set wd = GetObject(, "Word.Application")
    If wd Is Nothing Then
        Set wd = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    Set wdocSource = wd.Documents.Open("c:\word.docx")
    strWorkbookName = "c:\bestand.csv"
    wdocSource.MailMerge.MainDocumentType = wdFormLetters
    wdocSource.MailMerge.OpenDataSource _
            Name:=strWorkbookName, _
            AddToRecentFiles:=False, _
            Revert:=False, _
            Format:=wdOpenFormatAuto, _
            Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
            SQLStatement:="SELECT * FROM `Sheet1$`"
    With wdocSource.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = wdDefaultFirstRecord
            .LastRecord = wdDefaultLastRecord
        End With
        .Execute Pause:=False
    End With
    wd.Visible = True
    wdocSource.Close SaveChanges:=False
    Set wdocSource = Nothing
    Set wd = Nothing
'Sluiten Excel
Application.Quit

Ik heb via http://stackoverflow.com/...executing-word-mail-merge een soort van oplossing gevonden, maar deze genereerd losse pagina's vanuit het bestand. De bedoeling is dus dat deze gemaild worden naar het mailadres in het excel-bestand.
Ik heb ook al geprobeerd om
code:
1
2
3
4
5
6
7
    With wdocSource.MailMerge
        .MailAddressFieldName = "Email_adres"
        .MailSubject = "Onderwerp"
        .Destination = wdSendToEmail
        .SuppressBlankLines = True
        .Execute
    End With

erin te zetten, maar nog steeds geen mail. De constante wdSendToEmail is ook als 0 gedefinieerd.

[ Voor 58% gewijzigd door Paultje3181 op 06-05-2014 16:36 ]


  • LnC
  • Registratie: Juni 2005
  • Laatst online: 03-08 11:16

LnC

The offending line...


  • Paultje3181
  • Registratie: November 2002
  • Laatst online: 19:06
LnC: Die constanten worden gedefinieerd in regels 2 en 3. Ik heb mijn post even aangepast. Ik heb ondertussen ook een stukje verder zitten zoeken en de eerdere foutmelding over de object vereist is opgelost. Nu alleen nog dat er een mail gestuurd wordt.

  • LnC
  • Registratie: Juni 2005
  • Laatst online: 03-08 11:16

LnC

The offending line...

Hopelijk zit ik in de goede richting:
Try changing the following line:


.Destination = wdSendToEmail


to:


.Destination = 2 ' 2 = wdSendToEmail


Since you're doing late binding, wdSendToEmail is undefined. When that
line of code is hit (I'm assuming that Option Explicit is turned off), VB
will create a new variable named wdSendToEmail and assign it a value of 0.
This value is returned to the Destination property. Since 0 is
wdSendToNewDocument, you'll see a new document appear instead of an email.


You may also want to modify the following line:


WrdApl.Documents.Open Filename:="BlrPlt1.doc"


to:


WrdApl.Documents.Open "BlrPlt1.doc"


Since we're doing late binding, named parameters aren't used. The above
two lines produce the same code, but if you're passing any parameter other
than the first, you'll run into a problem.


Thanks,


Mark


Mark Durrett, MCSD, MCAD
Microsoft Developer Support


This posting is provided "AS IS" with no warranties, and confers no rights.
Bron.

  • Paultje3181
  • Registratie: November 2002
  • Laatst online: 19:06
Const wdSendToEmail = 2, wdDefaultFirstRecord = 1, wdDefaultLastRecord = -16

werkt wel. Ik had dus Const wdSendToEmal = 0 staan = new Document.

Het werkt nu dus zoals ik wil!

  • LnC
  • Registratie: Juni 2005
  • Laatst online: 03-08 11:16

LnC

The offending line...

Goed om te horen. :P
Pagina: 1