I've got to set make a xls macro to open a specific word template (.dot) a copy some cells to the Word document. Can anybody give me a help how i work this out?
Thxs
Will you always be putting the data from Excel into the same place in the Word document?
If so, something like the following might do the trick...
Note. first you need to have set up bookmarks in Word at each place where you want to insert excel data. Give each bookmark a different name and keep a note of them to hand when you write your code
Private Sub cmdCreateInvoice_Click()
Dim objWord As Object
Dim strFileName As String
strFileName = cboName & "_" & Format(Date, "ddmmmyy"
strFileName = "a:\" & strFileName 'this part creates a unique filename to use later as a save-as for your newly-created word document - you might not need this bit!!
'cboName is from a user form (I took this bit of code from a larger procedure)
Set objWord = CreateObject("Word.Application"
objWord.Documents.Open FileName:="""a:\Mum letter.doc""", 'opens up a template - put your template's filename in instead
ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto 'just some stuff from the macro recorder to get the file set up right
objWord.Selection.GoTo What:=wdGoToBookmark, Name:="Address"
objWord.Selection.TypeText Text:=Me.txtAddress.Text 'the important bit - this goes to your bookmark (in this case called "address" then pastes in your selected excel data (in this case a value txtAddress from my userform.) NB: "Me" refers to the userform
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.