Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing Cell from Xls to Word Macro 2

Status
Not open for further replies.

varela

IS-IT--Management
Nov 13, 2002
3
PT
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
 
Varela,

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

 
It works just fine.

Thanks.
 
No worries, glad to help

Liz (Thanks for the star!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top