Garth Wells said:
My client would like to create a Word and Excel template that are linked.
The Word doc will get a few calculations from Excel. The big challenge is
they want to be able to save versions of each Word/Excel combo that
are dynamically linked.
For example, assume you start off with Temp1.doc & Temp1.xls. The first
thing you would do is copy each file to a new subdirectory and rename to
something like: CusPro1.doc & CusPro1.xls. What I would like to do is
make the reference to the linked Excel dynamic...a function of the .doc's
name. So, when the Word file is opened it will replace all linked references
to
<MyFileName> & ".xls"
and open and update the linked fields accordingly.
Is this possible? If yes, could you give some general direction?
Thanks
Garth
The following seems to work OK. Create a Bookmark in the .doc and define a
Name in the .xls. The files must have the same base name (e.g., File1.doc & File1.xls)
Sub UpdateFromExcel()
'
' UpdateFromExcel Macro
' Macro created 6/19/2005 by GW
'
' Get base file name
Dim FileName As String
FileName = FileName & Left((ActiveDocument.FullName), (Len(ActiveDocument.FullName) - 4))
FileName = FileName & ".xls"
' MsgBox (FileName)
' Create Excel object, open handle to spreadsheet
Dim myWB As Excel.Workbook
Set myWB = GetObject(FileName)
' Replace bookmarks w/ values...this method does not remove bookmark
If ActiveDocument.Bookmarks.Exists("DocumentTitle") Then
Set BMRange = ActiveDocument.Bookmarks("DocumentTitle").Range
BMRange.Text = (myWB.Sheets("Distribution Questions").Range("DocumentTitle"))
ActiveDocument.Bookmarks.Add "Title", BMRange
End If
' Clear reference to bookmark
Set myWB = Nothing
End Sub