PDA

View Full Version : Excel to Word table edit - is it possible using VBA?



Bokka
02-23-2012, 02:09 AM
Hello Everybody,

I have been trying for some time now with my code and looking online to see if it is possible to format a Word table that has been created using a VBA macro in Excel - I have not found an answer.

I tried the following two options and neither worked (I got merged cells in excel but not in wrdTable1):

' MERGING CELLS IN TABLE 1

' Selection.wrdTable1.Select
' ActiveDocument.wrdTable1.Cell(1, 1).Range.Select
' Selection.SetRange Start:=Selection.wrdTable1.Cell(5, 1).Range.Start, End:=Selection.wrdTable1.Cell(5, 2).Range.End
' Selection.Cells.Merge



' With wrdTable1
' ActiveCell.Offset(7, 2).Select
' Range(ActiveCell.Offset(7, 2), ActiveCell.Offset(0, 3)).Merge
' ActiveCell.Select
' End With
From there I used macro recorder directly in Word and tried to incorporate it into the Excel code which did not work.

The formatting that I am trying to do in the Word tables are bold the title, merge and center some cells and put in top and bottom borders.

Is what I am trying to do possible?

Many thanks for your help.

Bokka
02-23-2012, 04:09 AM
I am now thinking to start over and make the tables in Excel and format them there and then copy them over to Word.

Learning by doing as they say as creating the tables in Word from Excel was now not the best of ideas...

Kenneth Hobs
02-23-2012, 06:07 AM
If your Excel table is small, a copy and paste method might work.

I would not shy away from doing it as you wanted though. If it can be recorded, it can be coded. I usually record in MSWord and then prefix the word object to do it in Excel. I also set the word object reference to use early binding to make coding easy and constants available.

See some of these links for some ideas.

'TypeText method
' http://www.excelforum.com/excel-programming/650672-populate-word-document-from-excel.html#post1946784
' http://www.excelforum.com/showthread.php?p=1946784
' http://vbaexpress.com/forum/showthread.php?p=169877
' http://vbaexpress.com/forum/showthread.php?t=24693
' http://www.excelforum.com/excel-programming/791302-excel-to-word-paragraph-and-page-setup.html

'Copy from Excel, paste to Word
'Lucas, http://vbaexpress.com/forum/showthread.php?p=178364

'FormFields
' http://www.mrexcel.com/forum/showthread.php?p=1639696
' http://www.mrexcel.com/forum/showthread.php?t=333200
' http://www.excelforum.com/excel-programming/799070-import-text-fields-from-word.html
' Content Controls
' http://www.vbaexpress.com/forum/showthread.php?t=39654

'Add Hyperlink to Bookmark
' http://www.excelforum.com/excel-programming/664078-use-excel-vba-to-add-a-hyperlink-to-a-word-document.html#post2006430
'Steiner, http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
'Colin_L, http://www.mrexcel.com/forum/showthread.php?t=358054

'Save OLEObject as MSWord Document
' http://vbaexpress.com/forum/showthread.php?t=21619

'Add Table to MSWord
' http://vbaexpress.com/forum/showthread.php?t=23975
' http://vbaexpress.com/forum/showthread.php?p=168731

'Import Word Tables
'vog, http://www.mrexcel.com/forum/showthread.php?t=382541

'Save OLEObject as MSWord DOC
' http://vbaexpress.com/forum/showthread.php?t=21619

'Get Optionbutton info from MSWord DOC
' http://vbaexpress.com/forum/showthread.php?t=22454

'FindReplace Text
' http://www.excelforum.com/excel-programming/682014-replace-word-in-ms-word-with-varable-from-ms-excel.html
' http://www.vbaexpress.com/forum/showthread.php?t=38958
' http://www.excelforum.com/excel-programming/794297-struggling-with-a-find-replace-macro-to-word.html

'Bookmarks
' http://vbaexpress.com/forum/showthread.php?p=185718
'Colin_L, http://www.mrexcel.com/forum/showthread.php?t=358054
' http://www.vbaexpress.com/forum/showthread.php?p=253277

'Mail Merge
' http://www.excelforum.com/excel-programming/796614-mail-merge-from-excel.html
' http://www.excelforum.com/excel-programming/798299-print-mail-merge-document.html
'Word 's Catalogue/Directory Mailmerge facility (the terminology depends on the Word version). _
To see how to group records with any mailmerge data source supported by Word, _
check out my Microsoft Word Catalogue/Directory Mailmerge Tutorial at:
' http://lounge.windowssecrets.com/index.php?showtopic=731107
' or
' http://www.gmayor.com/Zips/Catalogue%20Mailmerge.zip

Bokka
02-23-2012, 06:28 AM
Thank you for your reply. I just found the following two pieces of code and they seem to be quite useful but still not the complete answer:

Sub CopyTableToWordDocument()
‘Example of Word automation using early binding
‘Copies range from workbook and appends it to existing Word document
Dim wdApp As Word.Application
‘Copy A1:B6 in Table sheet
ThisWorkbook.Sheets(“Table”).Range(“A1:B6”).Copy
‘Establish link to Word
Set wdApp = New Word.Application
With wdApp
‘Open Word document
.Documents.Open Filename:=”C:\VBA_Prog_Ref\Chapter19\Table.docx”
With .Selection
‘Go to end of document and insert paragraph
.EndKey Unit:=wdStory
.TypeParagraph
‘Paste table
.Paste
End With
.ActiveDocument.Save
‘Exit Word
.Quit
End With
‘Release object variable
Set wdApp = Nothing
End Sub



and


Sub CopyTableToOpenWordDocument()
‘Example of Word automation using late binding
‘Copies range from workbook and appends it to
‘ a currently open Word document
Dim wdApp As Word.Application
‘Copy Range A1:B6 on sheet named Table
ThisWorkbook.Sheets(“Table”).Range(“A1:B6”).Copy
‘Establish link to open instance of Word
Set wdApp = GetObject(, “Word.Application”)
With wdApp.Selection
‘Go to end of document and insert paragraph
.EndKey Unit:=wdStory
.TypeParagraph
‘Paste table
.Paste
End With
‘Release object variable
Set wdApp = Nothing
End Sub