View Single Post
  #3   Report Post  
Posted to microsoft.public.word.vba.general,microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.word.vba.userforms
[email protected] hornbecky83@gmail.com is offline
external usenet poster
 
Posts: 6
Default Link table from excel to word using word VBA

Thanks for your help Cindy, however I am at another similiar roadblock-

Since my posting, I have switched over to working in excel VBA. I am
copying tables in excel and pasting them linked in word with the
following code:

Set rng = Wdoc.Paragraphs.Last.Range
rng.PasteExcelTable linkedtoexcel:=True, wordformatting:=False,
RTF:=False
rng.Tables(1).Rows.Alignment = wdAlignRowCenter
rng.Tables(1).AllowAutoFit = True
rng.Tables(1).AutoFitBehavior wdAutoFitContent
rng.Tables(1).Range.InsertCaption Label:=wdCaptionTable, Title:= _
"Mission Design Timeline and Delta V Budget Table", _
Position:=wdCaptionPositionAbove

I have a new problem now, where it paste fine and links, but when word
updates the tables from excel, all the table formating I did (such as
alignment, autofitcontent, and caption) go away. I found an earlier
posting where you said to use \*MERGEFORMAT, by typing alt-F9 in the
word document and adding \*MERGEFORMAT. This works. However, since I
will making many different word versions with this same code and have
20 tables or more. I do not want to have to continuously open the word
document my code created, toggle alt-F9, and manually insert
\*MERGEFORMAT into the many LINKs. Is there a way I can code
\*MERGEFORMAT into my excel VBA or add another code so that when my
tables update the table formating i mentioned earlier won't go away.
At the very least I would like the wdautofitcontent to stay intact.

I tried writing code to add \*MERGEFORMAT with the previous VBA syntex
you wrote me, but it would not work. I kept getting a run-time error
'450': wrong number of arguments or invalid property assignment. I get
this error even when I change rng to Wdoc.Application.Selection, where
Wdoc = Wapp.ActiveDocument. Or when I add preserveformat = true. This
is the code I wrote:

rng.Fields.Add Range:=Selection.Range, Type:=xlWorksheet, _
Text:="LINK Excel.Sheet.8 C:\\Documents and
Settings\\shornbec\\Desktop\\sarah\\excel
templates\\Mission.FY07Q1_Sarah.xls MissionTimelineDeltaVBudgetTable \a
\f 4 \h \*MERGEFORMAT"

Can you or anyone else out there that knows my problem please help me!!

Thank you!

Sarah

Cindy M. wrote:
Hi Sarah

Rather than using copy and paste, it's usually better to generate the
link by inserting a LINK field (what Word does for you when you paste and
link). Prepare by doing this

1. In Excel, select the table. Assign it a Range name by clicking in the
"Name Box" (left of the formula bar) and typing a name. Press Enter.

2. Now copy the table

3. Swith to Word. Edit/Paste and activate the "Link" option. If you want
the table to look like a Word table, don't change the selection in the
format list.

4. In the Word document, press Alt+F9 to toggle on the field codes. You
should see something that starts with { LINK "Excel.Sheet which is how
Word manages the link to Excel.

5. Select the part that looks like this
Sheet1!R4C3:R6C4
and type in the Range name you entered in step (1). This is easier to
manage - especially if rows or columns are added within the range - and
to remember.

What you want to do in your is create such a field. Everything between
the { field brackets } is what you need to tell Word which table to
insert. No need to have Excel open, or to switch to it at all in order to
insert a table into Word.

The basic VBA syntax then would be
Selection.Fields.Add Range:=Selection.Range, _
Text:=" LINK Excel.Sheet.8 "C:\\Test\\Book1" RangeName \a \f 4 \h "

You can copy and paste what's between the brackets to the Text argument.

I am new to VBA. I am making an automated report by copying excel
tables and pasting them in word. I want to use word VBA instead of
excel. I can get the tables to paste into word, but I can not get them
to be linked, so that when a parameter changes in excel it
automatically updates in my word document. The PasteExcelTable command
isn't working and I have the LinkedToExcel = true. I pasted my code
below. I am also having difficulty getting the excel table to paste
into the right document. I tried running the code in a module in the
document and also running the code in the document itself. But if
another word document is open, it sometimes pastes the table in the
other document. Can someone please help me!! Thank you in advance-
Sarah

Sub MissionDesignTemplate()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim strLVParameters As String
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Documents and
Settings\shornbec\Desktop\sarah\excel
templates\Mission.FY07Q1_Sarah.xls")

xlBook.Sheets("Report Tables").Range("LVParameters").Copy
Selection.PasteExcelTable LinkedToExcel:=True, WordFormatting:=False,
RTF:=False

xlBook.Close
xlApp.Quit

Set xlBook = Nothing
Set xlApp = Nothing
End Sub


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)