Forum Discussion

brambennink's avatar
brambennink
Copper Contributor
Oct 10, 2024

Won't make and paste over in word document

Im trying to make a macro to copy the content of existing word documents into a newly made one. When I run it in seperate pieces it works, but when i put it together it won't make a new document and only opens a blank word aplication (and I get error -2147417848(80010108) ). When the first document it copies from is already open it does, but then it won't copy the text anymore (and i get error 4198). I can't find a solution and I'm at my limit of knowlage about VBA.

Sub button1()
    Dim wdApp As Object
    Dim wdDoc As Object
    Dim newWdDoc As Object
    Dim path As String
    Dim docContent As Object
    
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True
    
    Set newWdDoc = wdApp.Documents.Add
    newWdDoc.Activate
    
    If Sheets("Keuze").Cells(2, 2).Value = True Then
        
        path = Sheets("Verwijzing").Cells(1, 1).Value
        Set wdDoc = wdApp.Documents.Open(path)
        
        wdDoc.content.Copy
        newWdDoc.Activate
        newWdDoc.content.Paste
        
        wdDoc.Close False
    End If
    
    If Sheets("Keuze").Cells(2, 3).Value = True Then
        
        path = Sheets("Verwijzing").Cells(1, 2).Value
        Set wdDoc = wdApp.Documents.Open(path)
        
        wdDoc.content.Copy
        newWdDoc.Activate
        newWdDoc.content.Paste
        
        wdDoc.Close False
    End If
    
    If Sheets("Keuze").Cells(2, 4).Value = True Then
        path = Sheets("Verwijzing").Cells(1, 3).Value
        Set wdDoc = wdApp.Documents.Open(path)
        
        wdDoc.content.Copy
        newWdDoc.Activate
        newWdDoc.content.Paste
        
        wdDoc.Close False
    End If
    
    newWdDoc.SaveAs2 "C:\Users\User\Documenten\Word\newWdDoc"
    
    newWdDoc.Close False
    wdApp.Quit
    
    Set wdApp = Nothing
    Set wdDoc = Nothing
    Set newWdDoc = Nothing
End Sub

Thank you in advance for taking time to look at this probably overcomplicated code.
-Bram

4 Replies

  • brambennink 

    Does it work better if you don't activate the documents?

    Sub button1()
        Dim wdApp As Object
        Dim wdDoc As Object
        Dim newWdDoc As Object
        Dim path As String
        Dim docContent As Object
    
        Set wdApp = CreateObject("Word.Application")
        wdApp.Visible = True
    
        Set newWdDoc = wdApp.Documents.Add
    
        If Sheets("Keuze").Cells(2, 2).Value = True Then
    
            path = Sheets("Verwijzing").Cells(1, 1).Value
            Set wdDoc = wdApp.Documents.Open(path)
    
            wdDoc.Content.Copy
            newWdDoc.Content.Paste
    
            wdDoc.Close False
        End If
    
        If Sheets("Keuze").Cells(2, 3).Value = True Then
    
            path = Sheets("Verwijzing").Cells(1, 2).Value
            Set wdDoc = wdApp.Documents.Open(path)
    
            wdDoc.Content.Copy
            newWdDoc.Content.Paste
    
            wdDoc.Close False
        End If
    
        If Sheets("Keuze").Cells(2, 4).Value = True Then
            path = Sheets("Verwijzing").Cells(1, 3).Value
            Set wdDoc = wdApp.Documents.Open(path)
    
            wdDoc.Content.Copy
            newWdDoc.Content.Paste
    
            wdDoc.Close False
        End If
    
        newWdDoc.SaveAs2 "C:\Users\User\Documenten\Word\newWdDoc"
    
        newWdDoc.Close False
        wdApp.Quit
    
        Set wdApp = Nothing
        Set wdDoc = Nothing
        Set newWdDoc = Nothing
    End Sub

    P.S. If more than one of the cells on the Keuze sheet is TRUE, each paste will overwrite the previous one.

    • brambennink's avatar
      brambennink
      Copper Contributor
      Hey Hans,
      It does the same thing as before.
      And thanks for the heads up about the override, ill look into that.

Resources