Welcome toWord.Tips.Net
Ask a Word Question
Make a Comment
Learn Access Now
Free Printable Forms
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips
Setting Fraction Bar Overhang Spacing in the Equation Editor
Printing On Both Sides of the Paper
Turning Off AutoComplete for Dates
Understanding Auto Line Spacing
Adding Comments to Your Document
Conditional Calculations in Word
When working with long documents that contain graphics, it is not unusual to use the INCLUDEPICTURE field to actually insert the graphics into the document. If you have a large number of these fields in your document, you may want a way to create an "art list" that includes the names of the graphic files used in the document.
By using a relatively simple macro, you can step through each instance of the INCLUDEPICTURE field in your document and extract just the names of the files referenced. These can then be added to a new document, so that when the macro is done, the new document contains just a list of the files referenced in the INCLUDEPICTURE fields. Here is a macro that will do just this:
Sub GetIncludePictures()
Dim oField As Field
Dim oCurrentDoc As Document
Dim oNewDoc As Document
Dim sFileName As String
Set oCurrentDoc = ActiveDocument
Set oNewDoc = Application.Documents.Add
For Each oField In oCurrentDoc.Fields
If oField.Type = wdFieldIncludePicture Then
sFileName = Replace(oField.Code, "INCLUDEPICTURE", "")
sFileName = Replace(sFileName, "MERGEFORMAT", "")
sFileName = Replace(sFileName, "\*", "")
sFileName = Replace(sFileName, "\d", "")
sFileName = Replace(sFileName, Chr(34), "")
sFileName = Replace(sFileName, "\\", "\")
sFileName = Trim(sFileName)
oNewDoc.Range.InsertAfter sFileName & vbCrLf
End If
Next oField
oNewDoc.Activate
Set oField = Nothing
Set oCurrentDoc = Nothing
Set oNewDoc = Nothing
End Sub
Notice the use of the Replace function several times in the macro. This function replaces occurrences of one string within another string with other text. That may sound confusing, but it is very handy. As an example, imagine that you have a string (sMyString) that contains the characters "This is my string", and that you use the following:
SMyString = Replace(sMyString, "s", "X")
This results in every lowercase "s" in sMyString being replaced with an uppercase "X". The result is that sMyString will now contain "ThiX iX my Xtring".
In the case of the GetIncludePictures macro, the several lines that contain Replace functions work to remove all the extraneous characters from the field code, except for the actual file name of the picture. If you want something else removed, as well, you can add more lines to remove those elements. (For instance, if you wanted to remove a standard path name to where your pictures are stored.)
When the macro is done running, the only thing in the new document should be the name of pictures, as in the following examples:
C:\mypics\picture1.tif ..\graphics\chap01\fig03.gif
If you wanted just the file names, and not the full path names, you could modify the GetIncludePictures macro a bit to include code that parsed out the path. You could do this by adding the following to the macro, just after the line sFileName = Trim(sFileName):
While Instr(sFileName, "\") > 0
sFileName = Mid(sFileName, Instr(sFileName, "\") + 1)
Wend
Tip #1296 applies to Microsoft Word versions: 97 2000 2002 2003 2007
Take Control! Master the real power behind Word! Successfully master the secrets of powerful formatting and create documents that stand out from the rest. Best of all, you can create documents that are easy to maintain and quick to change.