Group:  Microsoft Word ยป microsoft.public.word.oleinterop
Thread: Word 03 link field relative file path or custom document property?

Geek News

Word 03 link field relative file path or custom document property?
Frank <fkern12[ at ]gmail.com> 5/10/2007 6:18:37 PM
Hi, I need to edit the link fields on numerous Word 2003 docs to
change to a relative file path. After unsuccessfully trying and then
reading that it's impossible, I came across the following that
mentions a workaround involving using a custom document property to
store the base filepath:

http://word.mvps.org/FAQS/TblsFldsFms/includetextfields.htm

Can someone give me some help on how to accomplish this? Specifically,
I have numerous Word docs sitting in 6 directories. Each word doc
contains a link field to an Excel spreadsheet sitting in the directory
above the word documents. I need to move all of this to another
computer and probably move fairly often, so absolute links are a lot
of work.

I also read about using find-replace for the link fields, is that a
better solution that using the custom document property workaround?

Thanks for any help.

Frank

Re: Word 03 link field relative file path or custom document property?
"macropod" <invalid[ at ]invalid.invalid> 5/11/2007 4:51:25 AM
Hi Frank,

If you add the following macro to your document, it will update all link paths to point to the parent of the folder where the Word
document is located any time it is opened.

Option Explicit
Dim TrkStatus As Boolean ' Track Changes flag

Private Sub AutoOpen()
' This routine runs whenever the document is opened.
' It calls on the others to do the real work.
' Prepare the environment.
Call MacroEntry
' Most of the work is done by this routine.
Call UpdateFields
' Set the saved status of the document to true, so that changes via
' this code are ignored. Since the same changes will be made the
' next time the document is opened, saving them doesn't matter.
ActiveDocument.Saved = True
' Go to the start of the document
Selection.HomeKey Unit:=wdStory
' Clean up and exit.
Call MacroExit
End Sub

Private Sub MacroEntry()
' Store current Track Changes status, then switch off temporarily.
With ActiveDocument
TrkStatus = .TrackRevisions
.TrackRevisions = False
End With
' Turn Off Screen Updating temporarily.
Application.ScreenUpdating = False
End Sub

Private Sub MacroExit()
' Restore original Track Changes status
ActiveDocument.TrackRevisions = TrkStatus
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub

Private Sub UpdateFields()
' This routine sets the new path for external links.
Dim oRange As Word.Range
Dim oField As Word.Field
Dim OldPath As String
Dim TmpPath 'Array
Dim NewPath As String
Dim i As Integer
NewPath = ""
TmpPath = Split(ActiveDocument.Path, "\")
For i = LBound(TmpPath) To UBound(TmpPath) - 1
NewPath = NewPath & TmpPath(i) & "\\"
Next i
' Go through all story ranges in the document, including shapes,
' headers & footers.
For Each oRange In ActiveDocument.StoryRanges
' Go through the fields in the story range.
For Each oField In oRange.Fields
With oField
' Skip over fields that don't have links to external files
If Not .LinkFormat Is Nothing Then
' Get the old path
OldPath = Replace(.LinkFormat.SourcePath, "\", "\\")
' Replace the link to the external file
.Code.Text = Replace(.Code.Text, OldPath, NewPath)
End If
End With
Next oField
Next oRange
End Sub

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

"Frank" <fkern12[ at ]gmail.com> wrote in message news:1178821117.153774.239080[ at ]e65g2000hsc.googlegroups.com...
[Quoted Text]
> Hi, I need to edit the link fields on numerous Word 2003 docs to
> change to a relative file path. After unsuccessfully trying and then
> reading that it's impossible, I came across the following that
> mentions a workaround involving using a custom document property to
> store the base filepath:
>
> http://word.mvps.org/FAQS/TblsFldsFms/includetextfields.htm
>
> Can someone give me some help on how to accomplish this? Specifically,
> I have numerous Word docs sitting in 6 directories. Each word doc
> contains a link field to an Excel spreadsheet sitting in the directory
> above the word documents. I need to move all of this to another
> computer and probably move fairly often, so absolute links are a lot
> of work.
>
> I also read about using find-replace for the link fields, is that a
> better solution that using the custom document property workaround?
>
> Thanks for any help.
>
> Frank
>

Re: Word 03 link field relative file path or custom document property?
Frank <fkern12[ at ]gmail.com> 5/11/2007 3:50:33 PM


Thanks, I will try that when I return home on Sunday....

Frank

Re: Word 03 link field relative file path or custom document property?
Frank <fkern12[ at ]gmail.com> 5/14/2007 3:56:05 PM
I saved the macro into the VBA editor as Franks, pasted the macro text
between:

sub Franks ()

end sub

and deleting the comments the VBA created when I saved the macro. I
made it into its own module.

I see that it will run on document open, but nothing happens, the link
is not updated. When I try to run it manually, it errors with:

Compile error: Invalid inside procedure highlighting the <Option
Explicit> line of code in the macro (the first line after <sub Franks
()> ) I removed the two lines of code <sub Franks ()> and <end sub>
but then Word can't find the macro.

When the Word file opens, Word automatically wants to update the link
because that was the option selected when I first installed the link.
I tried selecting both "No" and "Yes" from the dialogue box that asks
whether to update the links - to no avail.

I've read up on macros but am not experienced with them - I tried
various things but can't get the macro to work. Any ideas?

Thanks.

Re: Word 03 link field relative file path or custom document property?
Frank <fkern12[ at ]gmail.com> 5/14/2007 11:07:22 PM
Update...

I got it to work by using the existing macro and deleting the lines
<sub Franks ()>, <end sub> and saving it in the normal.dot macros. I'm
not sure that is the most elegant way to implement the solution, is it
better to create a template file with the macro in it and the link to
the Excel file (plus a header, footer, and some standard disclaimer
language) and apply it to each of the documents?

Also is there a way to get rid of the Windows nag that asks whether
the user wants to update the link? Can it just run in the background
and update silently when the file is opened?

Thanks

Re: Word 03 link field relative file path or custom document property?
"macropod" <invalid[ at ]invalid.invalid> 5/15/2007 1:12:41 AM
Hi Frank,

The macro was ready to run as posted - no need to add anything.

If your documents (and their associated files) could end up being copied to somewhere that doesn't have a path back to your
template, it's definitely better to include the macro in them rather than just in the template - otherwise the links won't get
updated and they'll fail.

As for the update links nag, I think you can suppress that Via Tools|Options - somewhere in there I believe you'll find an item to
'prompt to update links' or something such (I don't have Word 2003 and am not familiar with all its options).

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

"Frank" <fkern12[ at ]gmail.com> wrote in message news:1179184042.635620.31430[ at ]p77g2000hsh.googlegroups.com...
[Quoted Text]
> Update...
>
> I got it to work by using the existing macro and deleting the lines
> <sub Franks ()>, <end sub> and saving it in the normal.dot macros. I'm
> not sure that is the most elegant way to implement the solution, is it
> better to create a template file with the macro in it and the link to
> the Excel file (plus a header, footer, and some standard disclaimer
> language) and apply it to each of the documents?
>
> Also is there a way to get rid of the Windows nag that asks whether
> the user wants to update the link? Can it just run in the background
> and update silently when the file is opened?
>
> Thanks
>

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net