Difficulty putting 2 macros together using CreateObject & GetOject

MHO044

New Member
Joined
Mar 6, 2016
Messages
1
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

Sheets("invuldata").Select
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:

Code:
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
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,944
Messages
5,834,515
Members
430,293
Latest member
Blind_Man

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top