C
cat2102
Hello-
I am using the code pasted below to link Word docs which contain field links
to Excel files. In selecting the Excel file to link to Word doc, the code
opens up the default file location (My Documents), however I would prefer
that it open up the file folder in which the Word document itself is located.
Is there a way to change the code for that to happen? Any assistance is
making that change would be appreciated.
Best,
-Cathy
Sub ChangeLinkedSpreadsheet()
Dim vExistingSpreadsheet As String
Dim vNewSpreadsheet As String
Dim vNumFields As Long
Dim fd As FileDialog
Dim i As Long
vNumFields = ActiveDocument.Fields.Count
If vNumFields = 0 Then
MsgBox "No fields found, exiting."
Exit Sub
End If
vExistingSpreadsheet = ActiveDocument.Fields(1).LinkFormat.SourceFullName
MsgBox "Existing linked spreadsheet is " & vExistingSpreadsheet
' Filepicker dialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
.Title = "Select the spreadsheet you want to link this document to"
.AllowMultiSelect = False
.ButtonName = "Link"
.Filters.Add "Excel Template", "*.xlt", 1
.Filters.Add "Excel Files", "*.xls", 2
.Filters.Add "Excel Workbooks", "*.xlw", 3
.FilterIndex = 2
'Use the Show method to display the File Picker dialog box
'The user pressed the action button.
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
vNewSpreadsheet = vrtSelectedItem
Next vrtSelectedItem
Else
Exit Sub
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
For i = 1 To vNumFields
' ActiveDocument.Fields(i).Select
' Selection.copyformat
If ActiveDocument.Fields(i).Type = wdFieldLink Then
'ActiveDocument.Fields(i).Select
'Selection.copyformat
ActiveDocument.Fields(i).LinkFormat.SourceFullName =
vNewSpreadsheet
'ActiveDocument.Fields(i).Select
'Selection.PasteFormat
End If
ActiveDocument.Fields(i).Update
' ActiveDocument.Fields(i).Select
' Selection.PasteFormat
Next i
End Sub
I am using the code pasted below to link Word docs which contain field links
to Excel files. In selecting the Excel file to link to Word doc, the code
opens up the default file location (My Documents), however I would prefer
that it open up the file folder in which the Word document itself is located.
Is there a way to change the code for that to happen? Any assistance is
making that change would be appreciated.
Best,
-Cathy
Sub ChangeLinkedSpreadsheet()
Dim vExistingSpreadsheet As String
Dim vNewSpreadsheet As String
Dim vNumFields As Long
Dim fd As FileDialog
Dim i As Long
vNumFields = ActiveDocument.Fields.Count
If vNumFields = 0 Then
MsgBox "No fields found, exiting."
Exit Sub
End If
vExistingSpreadsheet = ActiveDocument.Fields(1).LinkFormat.SourceFullName
MsgBox "Existing linked spreadsheet is " & vExistingSpreadsheet
' Filepicker dialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
.Title = "Select the spreadsheet you want to link this document to"
.AllowMultiSelect = False
.ButtonName = "Link"
.Filters.Add "Excel Template", "*.xlt", 1
.Filters.Add "Excel Files", "*.xls", 2
.Filters.Add "Excel Workbooks", "*.xlw", 3
.FilterIndex = 2
'Use the Show method to display the File Picker dialog box
'The user pressed the action button.
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
vNewSpreadsheet = vrtSelectedItem
Next vrtSelectedItem
Else
Exit Sub
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
For i = 1 To vNumFields
' ActiveDocument.Fields(i).Select
' Selection.copyformat
If ActiveDocument.Fields(i).Type = wdFieldLink Then
'ActiveDocument.Fields(i).Select
'Selection.copyformat
ActiveDocument.Fields(i).LinkFormat.SourceFullName =
vNewSpreadsheet
'ActiveDocument.Fields(i).Select
'Selection.PasteFormat
End If
ActiveDocument.Fields(i).Update
' ActiveDocument.Fields(i).Select
' Selection.PasteFormat
Next i
End Sub