Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-07-2015, 08:01 AM
addey addey is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 64bit
Novice
Replacing all images in a document with numbered files
 
Join Date: Feb 2015
Posts: 10
addey is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 12-07-2015, 04:48 PM
macropod's Avatar
macropod macropod is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by addey View Post
several of the formats Words stores its figures
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]
Reply With Quote
  #3  
Old 12-08-2015, 12:43 AM
addey addey is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 64bit
Novice
Replacing all images in a document with numbered files
 
Join Date: Feb 2015
Posts: 10
addey is on a distinguished road
Default

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...
Reply With Quote
  #4  
Old 12-08-2015, 01:53 AM
macropod's Avatar
macropod macropod is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 12-08-2015, 08:43 AM
addey addey is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 64bit
Novice
Replacing all images in a document with numbered files
 
Join Date: Feb 2015
Posts: 10
addey is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 12-08-2015, 02:09 PM
macropod's Avatar
macropod macropod is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 12-08-2015, 07:55 PM
Guessed's Avatar
Guessed Guessed is online now Replacing all images in a document with numbered files Windows 10 Replacing all images in a document with numbered files Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #8  
Old 12-10-2015, 01:34 AM
addey addey is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 64bit
Novice
Replacing all images in a document with numbered files
 
Join Date: Feb 2015
Posts: 10
addey is on a distinguished road
Default

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
As there are also ways of going through a document, selecting each InlineShape on the way, I was hoping there was a way to link the Selection in the above code to a selection in a code that runs through a document finding InlineShapes.

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
Reply With Quote
  #9  
Old 12-10-2015, 02:57 AM
macropod's Avatar
macropod macropod is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by addey View Post
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?
Sure, inserting images into a document in a sequential order is quite straightforward. I've posted code here that can do that for hundreds of images. That's not the problem; the problem is in relating those images to the ones already in the document that you want to replace. The location sequence in the document may or may not be the same as your replacement image sequence. Unless you can be sure they are in the same order, you'd be better off doing the lot manually. Assuming the inlineshapes are in the same order as their replacements, you could use code like:
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
The above code lets you choose the source folder and, as well as replacing the original images, rezises the new ones to the original sizes.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 12-10-2015, 05:44 AM
addey addey is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 64bit
Novice
Replacing all images in a document with numbered files
 
Join Date: Feb 2015
Posts: 10
addey is on a distinguished road
Default

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
Reply With Quote
  #11  
Old 12-10-2015, 01:54 PM
macropod's Avatar
macropod macropod is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #12  
Old 12-11-2015, 03:23 AM
addey addey is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 64bit
Novice
Replacing all images in a document with numbered files
 
Join Date: Feb 2015
Posts: 10
addey is on a distinguished road
Default

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?
Reply With Quote
  #13  
Old 12-11-2015, 01:12 PM
macropod's Avatar
macropod macropod is offline Replacing all images in a document with numbered files Windows 7 64bit Replacing all images in a document with numbered files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Even this code won't convert shapes in the header/footer, for example, but neither does the macro I posted previously process anything there.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
change, figures



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing all images in a document with numbered files Replacing multiple words at once in excel document Legaly Excel 2 05-23-2014 08:30 AM
Replacing all images in a document with numbered files Insert images with incrementallly-numbered filenames mqx Word VBA 4 11-13-2013 11:22 AM
Replacing all images in a document with numbered files 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
Replacing all images in a document with numbered files Flowing Numbered Lists Around Images Paul_A_Parks Drawing and Graphics 5 03-09-2011 10:22 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:14 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft