Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Export Excel sheet into Word template file

0

Hello,

Seems i got stuck somewhere or did something wrong

My table in word file didn't fill out my result from my sheet

The idea is when we create new data we want to create a new word file

With data from sheet2.range("Z3:AC export to the custome template word file.

How can i write my result data to my Template word file please

My result file i made has the table empty some how...

You can find this file in the sheet "My result file"

Thank you

Answer
Discuss

Answers

0
Selected Answer

Ghost

I've modified your sub Open_Word_Document to show a way to add data from Excel into the (only) table in your Word document. The changes are in bold (apart from where I've deleted comments/ unnecessary lines) with some comments added for others:

Sub Open_Word_Document2()
    Set AppWord = CreateObject("Word.Application")
    AppWord.Visible = True

    Set WordDoc1 = AppWord.Documents.Open("c:\temp\WordTemplate.docx") 'document in sheet "word file"

    'write to table
    With WordDoc1.Tables(1)
        'Loop rows
        For iRow = 1 To .Rows.Count
            'Loop columns
            For iCol = 1 To .Columns.Count
                ' Clear cell and replace with text from offset cell
                With .Cell(iRow, iCol).Range
                    .Delete
                    .InsertAfter Text:=Sheet2.Range("Y2").Offset(iRow, iCol).Value
                End With
            Next iCol
        Next iRow
    End With
    'save new file
    WordDoc1.SaveAs Filename:="c:\temp\WordTemplate8.docx" 'new filename

    AppWord.Activate

AppWord.ActiveDocument.Close
AppWord.Quit

End Sub

Note that the result may look odd since the Excel cells for CustomerName  incldee a line return after the name. Also the Word template may need an extra column (to accommodate CustID but perhaps your customers don't need to see that). Actually I noticed that you wanted to export z:AC only so you need to change the code above to read:

'Loop first 4 columns only

For iCol = 1 To 4
For another improvement, you might want to check that the number of Excel rows to be copied  does not exceed the rows in the Word template.

I have not attached a file (since my test version used local file locations to check that the method worked) but the macro above is named ...2 so you can just paste it into your module and try.

VBA methods in Word are somewhat different to those in Excel so I suggest you investigate Word methods (I'm no expert!).

Hope this helps.

Discuss

Discussion

Hello John_Ru,
Thanks for this part, i will look into it
Yeah you are absolute correct i need to investigate in word also
Didn't think i will needed word ever in my excel codes
But i guess i was wrong.

Thank again John_Ru
GhostofWanted (rep: 46) Jan 17, '22 at 7:52 am
Thanks for selecting my Answer, Ghost. It's been ages since I controlled Word for anything meaningful but I'm pretty sure there's a range method to populate tables (but I coulldn't recall it sadly). I much prefer controlling Excel!
John_Ru (rep: 6142) Jan 17, '22 at 7:55 am
Yeah, i understand
Even for me is excel hard but i start learning it with the help of Teachexcel
And his great members :)

1 question John_Ru
It seems the table design has been deleted
How can i prevent that part so i can still see those lines of it
GhostofWanted (rep: 46) Jan 17, '22 at 8:02 am
Not sure what you mean by  "It seems the table design has been deleted". The Word file from your question file had a table with no borders. I set all borders on the table (saved the file, without table entries) and ran my version of the macro. The saved file had the table (with borders, as expected) containing the values from the cells Z:AD.  
John_Ru (rep: 6142) Jan 17, '22 at 8:46 am
Okay, weird
I will try that ;)
Thanks
GhostofWanted (rep: 46) Jan 17, '22 at 9:56 am
Good luck. Another suggestion is to add headers to the first row of the Word table then run the loop:
For iRow = 2 To .Rows.Count
and set the write line to read:
.InsertAfter Text:=Sheet2.Range("Y1").Offset(iRow, iCol).Value
John_Ru (rep: 6142) Jan 17, '22 at 10:02 am
Awesome John_Ru
It worked by adding borders around the table in the template
And it keeps it :D
Thanks for the headers too
GhostofWanted (rep: 46) Jan 17, '22 at 2:15 pm
Great, glad you got it sorted.
John_Ru (rep: 6142) Jan 17, '22 at 2:44 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login