Forum Discussion
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 SubThank you in advance for taking time to look at this probably overcomplicated code.
-Bram
4 Replies
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 SubP.S. If more than one of the cells on the Keuze sheet is TRUE, each paste will overwrite the previous one.
- brambenninkCopper ContributorHey Hans,
It does the same thing as before.
And thanks for the heads up about the override, ill look into that.I'm afraid I don't know why the code causes an error.