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:UsersUserDocumentenWordnewWdDoc”
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
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:UsersUserDocumentenWordnewWdDoc”
newWdDoc.Close False
wdApp.Quit
Set wdApp = Nothing
Set wdDoc = Nothing
Set newWdDoc = Nothing
End SubThank you in advance for taking time to look at this probably overcomplicated code.-Bram Read More