#1
|
|||
|
|||
Replacing all images in a document with numbered files
I am having an issue with the following:
I am importing Word documents into various other software tools. One of these tools has issues with several of the formats Words stores its figures, and will not import all of them properly (and Word import is the only import option). The workaround so far has been to use the Word Cleaner tool to extract all figures as .png files (they are in essence just screenshots of the figures), and then manually replace these. I am looking for a way to automate the process. I have started writing a script that goes through each image, but I am at a loss on how to actually replace the figure in question with a figure from an external file. Word Cleaner creates the .png files with filenames "word-filename.001.png", "word-filename.002.png" etc., so having a procedure for the actual replacing, doing it for the entire document shouldn't be a big problem. I'm just at a loss on how to get that (rather essential...) part of the script working. Any ideas on how to start would be most welcome! I'm using Word 2016 32 bits btw. |
#2
|
||||
|
||||
Please clarify what you mean here. Are you referring to Word's Figure captioning, which is basically just a paragraph in the Caption Style, or to something else?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
The figures/images/shapes, not the captions or any other text. Word can store figures in several ways (they can be created within Word, it can be a copy-paste from somewhere, it can be an OLE object), it seems like InlineShape object type picks out the ones I am interested in. And these will then have to be replaced with the contents of the .png files (given the title of the thread, and with .png being an image format, I thought it was pretty obvious that I wanted to replace the images themselves, not the captions or any other textual stuff... images/figures only) in a sequential order.
Like I stated in my original post, the tool I am working with does not import all Word figures, but if I replace all figures with the ones exported to .png, all get imported. However, manually replacing all figures in a few hundred documents is something I am hoping to avoid... |
#4
|
||||
|
||||
I doubt anyone reading your post would have understood 'figures' to mean shape/inlineshape objects or that you essentially want to replace one image with another. I, for example, got the impression you might have been trying to replace 'Figure' Captions with images of those captions. Neither would anyone who hasn't used what you're calling a 'Word Cleaner tool' have any idea what the issue with that program is.
As for what you're trying to do, there really isn't any way for a macro to tell how an image in a document relates to an image on disk unless that image has been inserted as a link - and even then, it could only tell how the image in the document relates to the image it's linked to, not to any other image (though one might make inferences if both had the same name).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Why on Earth would I want to replace a figure caption with an image of the same caption? That makes no sense... And why would I call a figure caption "figure"? Never mind, I am looking for a possible solution, not semantics.
The issue is the following: The tool I am importing the Word documents to can distinguish "flat" images (such as a jpeg or a png that is pasted or inserted in a document), but it won't import for example figures made within Word itself (like text boxes, lines etc..), but by replacing these with .png or .jpg representations of the same images, the import works just fine. The manner in which I create the .png/.jpg files doesn't really matter, I just mentioned Word Cleaner for sake of clarity, as this is the tool that does this part of the job. The bottom line is that I have created .png files of the figures, and I want to create a script that removes the original figure, and inserts (pastes, whatever, it "just" needs to be in the correct place) the .png file where the original figure was. I don't need a link, like I already said I have a script that just runs through all figures in the document. That script has a counter, and what I want is when it has found the first figure (counter=1), then it removes the figure, and inserts filename_001.png. When it gets to the second figure (counter=2), it removes the figure, and inserts filename_002.png. I have the initial script, but I am missing the part where I insert the file in the specific position where the image was. So to sum up, what I really need, is a way to insert the contents of a picture file (in .png format, but that shouldn't matter) in a Word document via VBA. |
#6
|
||||
|
||||
And, as I have said, there is no way for VBA to relate any of your png files to an existing image in the document. You may as well insert the lot in one go via Insert|Picture, then drag them to wherever you want them and delete the original content.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
||||
|
||||
If your document is saved in docx or docm format you can change its filename to end with .zip and then open the document with a zip editor. The images will all be stored in the word\media folder and these can be replaced/updated in the zip.
However, if you are changing image formats then perhaps the filenames there will need to be changed to reflect those changes and so perhaps then other xml files in that zip will also need to be modified to reflect the new filename and/or file format.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
Thanks for the suggestion Guessed! The issue is however not to get the image files out, but to get them back in at the correct place. And since the problem originally is the various image formats used within Word, simply using the ones in the zip file won't work.
Getting images into a Word doc is not a problem, this small code inserts the three test images I made in a sequential order. Code:
Sub InsertImage() Dim i As Integer For i = 1 To 3 Selection.InlineShapes.AddPicture FileName:= _ "C:\path\test" & i & ".png", LinkToFile:=False, _ SaveWithDocument:=True Next i End Sub Macropod, the png images I want to include in the document are also sequentially numbered, and I know there is a way to walk through all InlineShapes in the document (i.e. sequential). And since it is also not a problem inserting an image at a selection, why would this be impossible? I am a VBA newb, so I'm just trying to figure stuff out... but getting a "that's not possible" answer when I see the outlines of a solution only wants me to dig deeper. I am thinking of something like this (pseudocode): Code:
For i = 1 to numberOfInlineShapesInDoc Select InlineShape Delete InlineShape Insert image_i.png 'one of the sequentially numbered images Next i |
#9
|
||||
|
||||
Quote:
Code:
Sub ReplaceInlineImages() Application.ScreenUpdating = False Dim strFolder As String, i As Long, Rng As Range Dim sngWdth As Single, sngHght As Single, iShp As InlineShape strFolder = GetFolder If strFolder = "" Then Exit Sub With ActiveDocument For i = .InlineShapes.Count To 1 Step -1 Set Rng = .InlineShapes(i).Range With Rng With .InlineShapes(1) sngWdth = .Width sngHght = .Height .Delete End With Set iShp = .InlineShapes.AddPicture(FileName:=strFolder & _ "\image" & i & ".png", LinkToFile:=False, SaveWithDocument:=True) With iShp .Width = sngWdth .Height = sngHght End With End With Next End With Set Rng = Nothing: Set iShp = Nothing Application.ScreenUpdating = True End Sub Function GetFolder() As String Dim oFolder As Object GetFolder = "" Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0) If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path Set oFolder = Nothing End Function
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Thanks a lot! That does exactly what I want!
But it doesn't find all figures... I thought it was because they perhaps were Shapes instead of InlineShapes (as these are figures created within Word, via Insert Shapes), but I ran a script to change all Shapes into InlineShapes, without effect, so it shouldn't be that. Should it?? I am not sure how Word categorizes all figures/images in a document, is there a way to look "behind the scenes" to see how a figure is categorized? Or simply force Word to make all figures (of any type) into an InlineShape object? Here's the wee script I used btw: Code:
For Each shFigure In ActiveDocument.Shapes shFigure.ConvertToInlineShape Next shFigure |
#11
|
||||
|
||||
Well, you did previously say you were working with InlineShapes, and that's all the code looks at. Once you start working with Shape objects, things become far more complicated, as their visual position on the page may not be related to where they are anchored in the document and it the anchor position that determines the order. There is simply no way for a macro to make sense of that. I'd suggest converting all your Shapes to InlineShapes before doing your png conversion.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
I know, and I thought I was (because I had run the script, and therefore thought all shapes should have been converted into inline shapes...), but your script doesn't pick up all figures in my document. Any idea why? The ones it doesn't pick up are shapes created in Word, and then converted via the script above. Is there a way I can see (in Word) if these have actually been converted?
|
#13
|
||||
|
||||
As I've already indicated, the code I provided works with inlineshapes only, not with floating shapes. The code snippet you posted may not convert all shapes to inlineshapes. Try:
Code:
With ActiveDocument While .Shapes.Count > 0 .Shapes(1).ConvertToInlineShape Wend End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
change, figures |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Replacing multiple words at once in excel document | Legaly | Excel | 2 | 05-23-2014 08:30 AM |
Insert images with incrementallly-numbered filenames | mqx | Word VBA | 4 | 11-13-2013 11:22 AM |
Number position for numbered list of images | ofey | Word | 3 | 09-29-2013 02:51 AM |
Replacing text string within document, it's not retaining formatting | livemusic | Word | 4 | 02-25-2013 12:33 AM |
Flowing Numbered Lists Around Images | Paul_A_Parks | Drawing and Graphics | 5 | 03-09-2011 10:22 PM |