Relative pathname of an image

I

il barbi

I've got a file .doc where I want to insert some linked jpeg images that
reside in the same directory or in a subdirectory, but I'd like possibly to
move my file carrying with it all images and I don't want to be obliged to
change the pathname of the linked jpegs in the .doc. To do so it should be
possible to express the pathname of the linked images in a relative way,
while it seems to me it is only permitted in an absolute way.
In some way this the same thing as for a htlm page that is made of a .htm
file and a directory - but there you can move both freely
il barbi
 
M

macropod

Hi il barbi,

Add the following vba code to your document:

Option Explicit
Public SFileName As String, FieldType As String, OldPath As String

Sub AutoOpen()
' This routine runs whenever the document is opened. It mainly performs a set of housekeeping functions.
' Most of the work is done by the UpdateFields and GetSourceFileName routines.
Dim sBar As Boolean, oSection As Section, shp As Shape, oHeadFoot As HeaderFooter
sBar = Application.DisplayStatusBar ' Store StatusBar visibility condition
Application.DisplayStatusBar = True ' Make StatusBar visible
Application.ScreenUpdating = False ' Minimise screen flicker
Selection.EndKey Unit:=wdStory
ActiveWindow.View.ShowFieldCodes = True
Call UpdateFields
' Set the saved status of the document to true, so that path update changes via this macro are ignored.
' Since they'll be recreated the next time the document is opened, saving such changes doesn't really matter.
' Then clean up and exit
ActiveWindow.View.ShowFieldCodes = False
On Error Resume Next ' In case there's only one active pane
ActiveWindow.ActivePane.Close
If ActiveWindow.View.SplitSpecial = wdPaneNone Then
ActiveWindow.ActivePane.View.Type = wdPrintView
Else
ActiveWindow.View.Type = wdPrintView
End If
ActiveDocument.Saved = True
Application.DisplayStatusBar = sBar ' Restore StatusBar to original visibility condition
Selection.HomeKey Unit:=wdStory
Application.ScreenUpdating = True
End Sub

Private Sub UpdateFields()
' This routine sets the new path for external field references, calls the GetSourceFileName routine to get the
' link's filename, plus any bookmarks and switches from the original field then merges these into a new field.
Dim wdRange As Range, FieldCount As Integer, NewPath As String, NewField As String
' Get the new path
NewPath = Replace$(ActiveDocument.Path, "\", "\\") & "\\"
' Go through the document, updating all external field links with the new path.
For Each wdRange In ActiveDocument.StoryRanges
If wdRange.Fields.Count > 0 Then
For FieldCount = wdRange.Fields.Count To 1 Step -1
wdRange.Fields(FieldCount).Select
With wdRange.Fields(FieldCount)
Select Case True
Case .Type = wdFieldHyperlink
FieldType = "HYPERLINK"
Case .Type = wdFieldIncludeText
FieldType = "INCLUDETEXT"
Case .Type = wdFieldIncludePicture
FieldType = "INCLUDEPICTURE"
Case .Type = wdFieldLink
FieldType = "LINK"
Case .Type = wdFieldRefDoc
FieldType = "RD"
Case Else
FieldType = ""
End Select
End With
If FieldType <> "" Then
Call GetSourceFileName
' Don't bother doing anything if the paths are the same
If OldPath <> NewPath Then
' Compile the new field's code
NewField = FieldType & " " & """" & NewPath & SFileName
Application.StatusBar = "Updating " & SFileName ' Show progress on status bar
' Replace the old field with the new one
With Selection
.Delete
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:=NewField, PreserveFormatting:=False
End With
End If
End If
Next FieldCount
End If
Next wdRange
Application.StatusBar = "Finished!"
End Sub

Private Sub GetSourceFileName()
' This routine gets the source file's name, plus any bookmarks and switches from the original field.
Dim CharPos As Integer
SFileName = Selection
For CharPos = Len(SFileName) To 0 Step -1
On Error Resume Next 'In case there's no path
If Mid(SFileName, CharPos, 2) = "\\" Then
SFileName = Mid(SFileName, CharPos + 2)
Exit For
End If
Next CharPos
' Delete any extra spaces on the right, but preserve leading & internal spacing.
SFileName = RTrim(Replace$(SFileName, Chr(21), ""))
' Extract the old path for testing
OldPath = Trim(Replace(Replace(Mid(Selection, 2, CharPos), FieldType, ""), """", ""))
End Sub

To add the code to your document:
.. press Alt-F11 to open the vba editor
.. click Insert|Modue
.. copy & paste the code
.. exit the vba editor
.. save the document

Provided you allow Word to run macros when you open this document, the code runs automatically.

Cheers

--
macropod
[MVP - Microsoft Word]


| I've got a file .doc where I want to insert some linked jpeg images that
| reside in the same directory or in a subdirectory, but I'd like possibly to
| move my file carrying with it all images and I don't want to be obliged to
| change the pathname of the linked jpegs in the .doc. To do so it should be
| possible to express the pathname of the linked images in a relative way,
| while it seems to me it is only permitted in an absolute way.
| In some way this the same thing as for a htlm page that is made of a .htm
| file and a directory - but there you can move both freely
| il barbi
|
|
|
 
M

macropod

Oops - wrong set of code. Related but different (incomplete) project I started working on (to allow the user to choose which types
of fields to update). The attached simpler version does the job just as well for your purposes.

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 NewPath As String
' Set the new path
NewPath = Replace$(ActiveDocument.Path, "\", "\\")
' 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


--
macropod
[MVP - Microsoft Word]


| I've got a file .doc where I want to insert some linked jpeg images that
| reside in the same directory or in a subdirectory, but I'd like possibly to
| move my file carrying with it all images and I don't want to be obliged to
| change the pathname of the linked jpegs in the .doc. To do so it should be
| possible to express the pathname of the linked images in a relative way,
| while it seems to me it is only permitted in an absolute way.
| In some way this the same thing as for a htlm page that is made of a .htm
| file and a directory - but there you can move both freely
| il barbi
|
|
|
 
I

il barbi

macropod said:
Hi il barbi,

Add the following vba code to your document:
thank you but I was looking for some simple solution, this one forces to
execute vba code, this seems too sophisticated to me
il barbi
 
M

macropod

Hi il barbi,

There is no simpler solution - there is no field or document setting in Word to do what you want.

Cheers

--
macropod
[MVP - Microsoft Word]


| "macropod" <[email protected]> ha scritto nel messaggio
| | > Hi il barbi,
| >
| > Add the following vba code to your document:
| >
| thank you but I was looking for some simple solution, this one forces to
| execute vba code, this seems too sophisticated to me
| il barbi
|
|
 
M

macropod

Hi il barbi

Here's a non-macro solution based on work done by fellow MVP, Peter Jamieson. Code each INCLUDEPICTURE field like:
{ INCLUDEPICTURE "{ filename \p }\\..\\Filename.jpg}
where 'Filename.jpg' is the name of the picture file.

Note that the field braces around 'filename \p' are created via Ctrl-F9 - you can't just type them.

Since it seems you might have a lot of these to do, I'd suggest doing the first one, copying that field to all the remaining picture
positions, then pressing Alt-F9 to expose all the field codes and changing the filenames to suit. Delete the old fields as you go.
When you're done, press Alt-F9 again to hide the field codes, then Alt-A to select the whole document and F9 to update all the
fields.

Cheers

--
macropod
[MVP - Microsoft Word]


| I've got a file .doc where I want to insert some linked jpeg images that
| reside in the same directory or in a subdirectory, but I'd like possibly to
| move my file carrying with it all images and I don't want to be obliged to
| change the pathname of the linked jpegs in the .doc. To do so it should be
| possible to express the pathname of the linked images in a relative way,
| while it seems to me it is only permitted in an absolute way.
| In some way this the same thing as for a htlm page that is made of a .htm
| file and a directory - but there you can move both freely
| il barbi
|
|
|
 
C

Chris

Hi,
So this code will let us use a relative path to an image, without needing to
use a macro?

I've tried it but couldnt' get it to work.

But just want to make sure I read this right, and that's what that code does.
 
C

Chris

Any tried this code? does it work?

Chris said:
Hi,
So this code will let us use a relative path to an image, without needing to
use a macro?

I've tried it but couldnt' get it to work.

But just want to make sure I read this right, and that's what that code does.

macropod said:
Hi il barbi

Here's a non-macro solution based on work done by fellow MVP, Peter Jamieson. Code each INCLUDEPICTURE field like:
{ INCLUDEPICTURE "{ filename \p }\\..\\Filename.jpg}
where 'Filename.jpg' is the name of the picture file.

Note that the field braces around 'filename \p' are created via Ctrl-F9 - you can't just type them.

Since it seems you might have a lot of these to do, I'd suggest doing the first one, copying that field to all the remaining picture
positions, then pressing Alt-F9 to expose all the field codes and changing the filenames to suit. Delete the old fields as you go.
When you're done, press Alt-F9 again to hide the field codes, then Alt-A to select the whole document and F9 to update all the
fields.

Cheers

--
macropod
[MVP - Microsoft Word]


| I've got a file .doc where I want to insert some linked jpeg images that
| reside in the same directory or in a subdirectory, but I'd like possibly to
| move my file carrying with it all images and I don't want to be obliged to
| change the pathname of the linked jpegs in the .doc. To do so it should be
| possible to express the pathname of the linked images in a relative way,
| while it seems to me it is only permitted in an absolute way.
| In some way this the same thing as for a htlm page that is made of a .htm
| file and a directory - but there you can move both freely
| il barbi
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top