PDA

View Full Version : Error 5174



KarenB2
02-21-2020, 12:50 PM
Hi -I am a first time poster

I have a word document with many bookmarks. I created the macro below to simply go to a bookmark in my file and insert it to a new file. If I open the file and click on the macro right away, it works. But after that, it throws the error 5174. Any help is appreciated. I am not very practiced in exploring these things in word

Private Sub CommandButton2_Click()
Windows("Master Documents file").Activate
Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
Selection.InsertFile FileName:="Master Documents file.docm", Range:= _
"Charter", ConfirmConversions:=False, Link:=False, Attachment:=False
Windows("Master Documents file").Activate


End Sub

Thanks for even reading and help would be sooooooooooo appreciated

gmayor
02-21-2020, 10:17 PM
Do we take it that the macro is in the document 'Master Documents File.docm'? That being the case


Private Sub CommandButton2_Click()
Dim oDoc As Document, oSource As Document
Dim oRng As Range
Set oSource = ThisDocument
Set oDoc = Documents.Add(Template:="Normal")
Set oRng = oDoc.Range
oRng.FormattedText = oSource.Bookmarks("Charter").Range.FormattedText
oSource.Activate
Set oDoc = Nothing
Set oSource = Nothing
Set oRng = Nothing
End Sub

KarenB2
02-24-2020, 09:13 AM
That did it - Thanks Graham!:yes