Difficulty putting 2 macros together using CreateObject & GetOject


New Member
Mar 6, 2016
I have created 2 macro's in excel that open a word .docm document and need to do 2 things:

1. do a mail merge (sub creëer contract)
2. delete bookmarks (call word macro's based on excel values)

Seperately they work as should, but I have difficulty putting them together.
I tried calling one from the other and just pasting part of the code of the second one to the first one, but without succes.
Any help in the right direction would be much appreciated, I'm clearly missing something obvious.:confused:

Code is below:

First Macro

Sub Creëer_contract()
' Creëer_contract Macro

On Error Resume Next
Dim wdOutputNaam, wdInputnaam As String
Dim wdDoc As Object

wdOutputNaam = Range("namefull").Value & "_" & Range("creationdate").Value & ".docx"
wdInputnaam = ThisWorkbook.Path & "\templates\AGR arbeidsovereenkomst\Arbeidsovereenkomst.docm"

' open the mail merge layout file

Set wdDoc = GetObject(wdInputnaam, "Word.document")

With wdDoc.mailmerge
.MainDocumentType = wdFormLetters
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.Execute Pause:=False
End With

wdDoc.Application.Visible = True
wdDoc.Application.ActiveDocument.SaveAs Filename:="c:\HRapplicatie\" & wdOutputNaam

' cleanup
wdDoc.Close Savechanges:=False
Set wdDoc = Nothing
wdDoc.Application.wdOutputName.Visible = True

End Sub

And the second one:

Sub deletewordbookmarks()

    Dim wdInputnaam As String
    Dim wdOutputNaam As String
    Dim WD As Object

    wdInputnaam = "C:\HRapplicatie\templates\AGR arbeidsovereenkomst\Arbeidsovereenkomst.docm"
    wdOutputNaam = Range("namefull").Value & "_" & Range("creationdate").Value & ".docx"

      Set WD = CreateObject("Word.Application")
      WD.Documents.Open "C:\HRapplicatie\templates\AGR arbeidsovereenkomst\Arbeidsovereenkomst.docm"
 ' call word macros based on excel values
      If Range("advnumber").Value = " " Then
      WD.Run "Project.verwijderbookmarks.verwijderadv"
      End If
      If Range("expenses").Value = "0" Then
      WD.Run "Project.verwijderbookmarks.verwijderforfait"
      End If
      If Range("car").Value <> "ja" Then
      WD.Run "Project.verwijderbookmarks.verwijderbedrijfswagen"
      WD.Application.Visible = True
      End If
      If Range("noncompete").Value = "nee" Then
      WD.Run "Project.verwijderbookmarks.verwijdernoncompete"
      WD.Application.Visible = True
      End If
      If Range("bonus").Value = "0" Then
      WD.Run "Project.verwijderbookmarks.verwijderbonus"
      WD.Application.Visible = True
      End If
 ' show and save output file
 WD.Application.Visible = True
 WD.Application.ActiveDocument.SaveAs Filename:="c:\HRapplicatie\" & wdOutputNaam

End Sub

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics