Export to a Word document

nokia6100

New Member
Joined
Mar 24, 2014
Messages
1
Hello my friends.

I have an Excel document which acts as a small database. A report button is incorporated which has to extract the database data and export it to a Word file.

I first want to filter data in the excel database with the standard Filter capability of Excel and export that data to a template Word document with bookmarks. However, I do not have the capabilities to create such code.

Can someone help me out? I am puzzling with the following code, but it creates separate Word files and my goal is one Word file.

Code:
Option Explicit'change this to where your files are stored
Const FilePath As String = "C:\reporttemplate.docx"
Dim wd As New Word.Application
Dim IDCell As Range


Sub Sheet5_Button2_Click()
'create copy of Word in memory
Dim doc As Word.Document
wd.Visible = True
Dim IDRange As Range
'create a reference to all the ids
Range("A1").Select
Set IDRange = Range( _
ActiveCell, _
ActiveCell.End(xlDown))
'for each person in list �
For Each IDCell In IDRange
'open a document in Word
Set doc = wd.Documents.Open("C:\reporttemplate.docx")
'go to each bookmark and type in details
CopyCell "id", 1
CopyCell "date", 2
CopyCell "risk", 3
'save and close this document
doc.SaveAs2 FilePath & "report " & IDCell.Value & ".docx"
doc.Close
Next IDCell
wd.Quit
MsgBox "Created files in " & FilePath & "!"
End Sub
Sub CopyCell(BookMarkName As String, ColumnOffset As Integer)
'copy each cell to relevant Word bookmark
wd.Selection.Goto What:=wdGoToBookmark, Name:=BookMarkName
wd.Selection.TypeText IDCell.Offset(0, ColumnOffset).Value
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top