Home

Macro help. Importing Table into Word

%3CLINGO-SUB%20id%3D%22lingo-sub-738902%22%20slang%3D%22en-US%22%3EMacro%20help.%20Importing%20Table%20into%20Word%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-738902%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Everyone%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20recently%20started%20using%20macros%20in%20excel%20as%20I%20wanted%20to%20streamline%20some%20of%20my%20business.%20I%20have%20created%20a%20word%20template%20that%20is%20a%20generic%20proposal%20document%20for%20my%20business.%20I%20am%20now%20trying%20to%20create%20a%20macro%20in%20my%20excel%20document%20that%20will%20automatically%20create%20a%20new%20word%20document%20from%20my%20template%20and%20populate%20some%20of%20the%20areas.%20I%20have%20managed%20to%20create%20the%20word%20document%20and%20get%20some%20of%20the%20text%20replaced%20with%20values%20from%20my%20excel%20document.%20I%20can%20not%2C%20however%2C%20automatically%20import%20a%20table%20into%20the%20document.%20I%20have%20followed%20online%20tutorials%20and%20adapted%20the%20macro%20as%20best%20I%20can%20but%20I%20can%20not%20get%20any%20further.%20The%20macro%20does%20not%20through%20up%20any%20faults%20when%20it%20runs%20and%20will%20complete.%20However%2C%20it%20just%20does%20not%20import%20the%20table%20over%20at%20all%20no%20matter%20what%20I%20change.%20I%20have%20tried%20to%20make%20it%20import%20based%20on%20Paragraph%20location%2C%20Page%20location%20and%20bookmark%20location%20and%20nothing%20seems%20to%20work.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBellow%20is%20the%20macro%20that%20I%20am%20currently%20running%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20CreateProposal()%3C%2FP%3E%3CP%3EDim%20wApp%20As%20Word.Application%3CBR%20%2F%3EDim%20wDoc%20As%20Word.Document%3CBR%20%2F%3EDim%20tbl%20As%20Excel.Range%3CBR%20%2F%3EDim%20WordTable%20As%20Word.Table%3CBR%20%2F%3ESet%20wApp%20%3D%20CreateObject(%22Word.Application%22)%3CBR%20%2F%3EwApp.Visible%20%3D%20True%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E'create%20new%20proposal%20document%3CBR%20%2F%3ESet%20wDoc%20%3D%20wApp.Documents.Add(Template%3A%3D%22C%3A%5CUsers%5CEdward%20Baker%5CDocuments%5CCustom%20Office%20Templates%5CBaiceir%20-%20Proposal%20Template2.dotx%22%2C%20NewTemplate%3A%3DFalse%2C%20DocumentType%3A%3D0)%3C%2FP%3E%3CP%3E'auto%20input%20total%20project%20value%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EWith%20wDoc%3CBR%20%2F%3E.Application.Selection.Find.Text%20%3D%20%22%26lt%3B%3CSYSTEM%20cost%3D%22%22%3E%26gt%3B%22%3CBR%20%2F%3E.Application.Selection.Find.Execute%3CBR%20%2F%3E.Application.Selection%20%3D%20Range(%22C20%22)%3CBR%20%2F%3E.Application.Selection.EndOf%3CBR%20%2F%3E%3CBR%20%2F%3E'Code%20optimisation%3CBR%20%2F%3EApplication.ScreenUpdating%20%3D%20False%3CBR%20%2F%3EApplication.EnableEvents%20%3D%20False%3C%2FSYSTEM%3E%3C%2FP%3E%3CP%3E'Copy%20Range%20from%20Excel%3CBR%20%2F%3ESet%20tbl%20%3D%20ThisWorkbook.Worksheets(%22Equipment%20Table%22).ListObjects(%22Table1%22).Range%3C%2FP%3E%3CP%3E'Create%20an%20Instance%20of%20MS%20Word%3CBR%20%2F%3EOn%20Error%20Resume%20Next%3CBR%20%2F%3E%3CBR%20%2F%3E'Is%20MS%20Word%20already%20opened%3F%3CBR%20%2F%3ESet%20WordApp%20%3D%20GetObject(Class%3A%3D%22Word.Application%22)%3C%2FP%3E%3CP%3E'Copy%20Excel%20Table%20Range%3CBR%20%2F%3Etbl.Copy%3CBR%20%2F%3E%3CBR%20%2F%3E'Paste%20Table%20into%20MS%20Word%3CBR%20%2F%3EmyDoc.Paragraphs(1).Range.PasteExcelTable%20_%3CBR%20%2F%3ELinkedToExcel%3A%3DFalse%2C%20_%3CBR%20%2F%3EWordFormatting%3A%3DTrue%2C%20_%3CBR%20%2F%3ERTF%3A%3DFalse%3CBR%20%2F%3E%3CBR%20%2F%3E'Autofit%20Table%20so%20it%20fits%20inside%20Word%20Document%3CBR%20%2F%3ESet%20WordTable%20%3D%20myDoc.Tables(1)%3CBR%20%2F%3EWordTable.AutoFitBehavior%20(wdAutoFitWindow)%3CBR%20%2F%3E%3CBR%20%2F%3EEndRoutine%3A%3CBR%20%2F%3E'Optimize%20Code%3CBR%20%2F%3EApplication.ScreenUpdating%20%3D%20True%3CBR%20%2F%3EApplication.EnableEvents%20%3D%20True%3C%2FP%3E%3CP%3E'Clear%20The%20Clipboard%3CBR%20%2F%3EApplication.CutCopyMode%20%3D%20False%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EEnd%20With%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20greatly%20appreciated%20in%20this%20matter.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-738902%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETraining%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-739033%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%20help.%20Importing%20Table%20into%20Word%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-739033%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F371370%22%20target%3D%22_blank%22%3E%40Smokemover%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3Etry%20this.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ESub%20ExcelZelleInWordDokument()%3CBR%20%2F%3EDim%20wsApp%20As%20Object%3CBR%20%2F%3EDim%20myDoc%20As%20Object%3CBR%20%2F%3EDim%20myRange%20As%20Object%3CBR%20%2F%3EDim%20myTab%20As%20Object%3CBR%20%2F%3EDim%20Vardat%20As%20Variant%3CBR%20%2F%3EDim%20intZeile%20As%20Integer%3CBR%20%2F%3EDim%20intSpalte%20As%20Integer%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20wdApp%20%3D%20CreateObject(%22Word.application%22)%3CBR%20%2F%3E%3CBR%20%2F%3EWith%20wdApp%3CBR%20%2F%3E.Visible%20%3D%20True%3CBR%20%2F%3E.WindowState%20%3D%20wdWindowStateMaximize%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20myDoc%20%3D%20wdApp.Documents.Add%3CBR%20%2F%3E%3CBR%20%2F%3EVardat%20%3D%20Tabelle1.Range(%22A1%3AD10%22).Value%3CBR%20%2F%3ESet%20myRange%20%3D%20myDoc.Range(0%2C%200)%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20myTab%20%3D%20myDoc.Tables.Add%20_%3CBR%20%2F%3E(Range%3A%3DmyRange%2C%20_%3CBR%20%2F%3ENumRows%3A%3DUBound(Vardat%2C%201)%2C%20_%3CBR%20%2F%3ENumColumns%3A%3DUBound(Vardat%2C%202))%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20intZeile%20%3D%201%20To%20UBound(Vardat%2C%201)%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20intSpalte%20%3D%201%20To%20UBound(Vardat%2C%202)%3CBR%20%2F%3EmyTab.Cell(intZeile%2C%20intSpalte).Range.Text%20%3D%20Vardat(intZeile%2C%20intSpalte)%3CBR%20%2F%3ENext%20intSpalte%3CBR%20%2F%3E%3CBR%20%2F%3ENext%20intZeile%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%3C%2FP%3E%3CP%3EBernd%3C%2FP%3E%3CP%3E%3CA%20title%3D%22VBA-Tanker%22%20href%3D%22https%3A%2F%2Fvba-tanker.com%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Evba-Tanker.com%20-%20a%20databse%20full%20of%20usefull%20macros%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Smokemover
Occasional Visitor

Hello Everyone,

 

I have recently started using macros in excel as I wanted to streamline some of my business. I have created a word template that is a generic proposal document for my business. I am now trying to create a macro in my excel document that will automatically create a new word document from my template and populate some of the areas. I have managed to create the word document and get some of the text replaced with values from my excel document. I can not, however, automatically import a table into the document. I have followed online tutorials and adapted the macro as best I can but I can not get any further. The macro does not through up any faults when it runs and will complete. However, it just does not import the table over at all no matter what I change. I have tried to make it import based on Paragraph location, Page location and bookmark location and nothing seems to work.

 

Bellow is the macro that I am currently running:

 

Sub CreateProposal()

Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim tbl As Excel.Range
Dim WordTable As Word.Table
Set wApp = CreateObject("Word.Application")
wApp.Visible = True


'create new proposal document
Set wDoc = wApp.Documents.Add(Template:="C:\Users\Edward Baker\Documents\Custom Office Templates\Baiceir - Proposal Template2.dotx", NewTemplate:=False, DocumentType:=0)

'auto input total project value


With wDoc
.Application.Selection.Find.Text = "<<system cost>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C20")
.Application.Selection.EndOf

'Code optimisation
Application.ScreenUpdating = False
Application.EnableEvents = False

'Copy Range from Excel
Set tbl = ThisWorkbook.Worksheets("Equipment Table").ListObjects("Table1").Range

'Create an Instance of MS Word
On Error Resume Next

'Is MS Word already opened?
Set WordApp = GetObject(Class:="Word.Application")

'Copy Excel Table Range
tbl.Copy

'Paste Table into MS Word
myDoc.Paragraphs(1).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=True, _
RTF:=False

'Autofit Table so it fits inside Word Document
Set WordTable = myDoc.Tables(1)
WordTable.AutoFitBehavior (wdAutoFitWindow)

EndRoutine:
'Optimize Code
Application.ScreenUpdating = True
Application.EnableEvents = True

'Clear The Clipboard
Application.CutCopyMode = False


End With

End Sub

 

Any help would be greatly appreciated in this matter.

 

Thank you.

1 Reply

@Smokemover 

Hi,

try this.

 

Sub ExcelZelleInWordDokument()
Dim wsApp As Object
Dim myDoc As Object
Dim myRange As Object
Dim myTab As Object
Dim Vardat As Variant
Dim intZeile As Integer
Dim intSpalte As Integer

Set wdApp = CreateObject("Word.application")

With wdApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With

Set myDoc = wdApp.Documents.Add

Vardat = Tabelle1.Range("A1:D10").Value
Set myRange = myDoc.Range(0, 0)

Set myTab = myDoc.Tables.Add _
(Range:=myRange, _
NumRows:=UBound(Vardat, 1), _
NumColumns:=UBound(Vardat, 2))

For intZeile = 1 To UBound(Vardat, 1)

For intSpalte = 1 To UBound(Vardat, 2)
myTab.Cell(intZeile, intSpalte).Range.Text = Vardat(intZeile, intSpalte)
Next intSpalte

Next intZeile


End Sub

 

Best regards

Bernd

vba-Tanker.com - a databse full of usefull macros

Related Conversations
Extentions Synchronization
Deleted in Discussions on
3 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
36 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies