PDA

View Full Version : Word 2010 Macro to insert Images



legg1979
04-06-2012, 02:43 PM
Hi 1st post so hi eveyone & thanks in advance.

Im looking for a macro or way to insert certain images into a 2010 word doc.

Example: Word doc is in c:\work\job name
images will be in c:\work\job name\images The images will always be named the same ie image1

I want a macro to insert image1 into my the word doc which will be in c:\work\job name

Basicly the folder structure never changes. The word doc is unique to the current folder & the images are also unique to the folder.

I hope this makes sense & hope you can help.

Thanks guys & girls look forward to hearing back

fumei
04-06-2012, 03:31 PM
Have you tried doing a search on this? It is a very common request.

Basically you need to decide if this is a straight-forward insert - an InlineShape, or do you want it to float - a Shape.

If you are just doing an insert, this uses AddPicture. Look this up in VBA Help.

legg1979
04-07-2012, 01:38 AM
I have searched and can't seem to find what I'm looking for. There are plenty of inserting macros for inserting the same image from the same folder but I need it to insert from the same place as the document. The root folder name will change every time but it will always have an images folder within it.
It will be similar to HTML style ie ../images/file.jpg

I don't know anything about VBA but this must be able to do it surely.

macropod
04-07-2012, 04:21 AM
If the image name is always the same, you don't even need a macro. All you need is an INCLUDEPICTURE field configured for relative path referencing. To see how to do that, check out the solution I've posted at: http://windowssecrets.com/forums/showthread.php/102080-Relative-Paths-in-Word-Fields-(All)?p=584769&viewfull=1#post584769

fumei
04-07-2012, 07:38 AM
If I understand correctly...

The image path location will always be the same as the document path location, but one folder down, i.e.

Document: C:\Yadda\Whatever\document.doc
Image: C:\Yadda\Whatever\Images\someimage.jpg

Yes?

So, look at the code that you have found and see where the locations(pahs) are coded, and change it so it works. When you have ONE example working, ask how you can get it to work for ANY document's path location.

Hint: any given document path is...ActiveDocument.Path

macropod
04-07-2012, 05:28 PM
If I understand correctly...

The image path location will always be the same as the document path location, but one folder down
Even if that is so, you don't need a macro - it can all be done with field coding.

fumei
04-07-2012, 08:44 PM
True. The OP seems to want to VBA solution.

macropod
04-08-2012, 02:36 AM
True. The OP seems to want to VBA solution.
Or maybe just assumed vba is required.

fumei
04-08-2012, 06:00 PM
Or...they want a solution where they never have to put in a FILENAME field and then put that in an INCLUDEPICTURE field to make it work. A field solution would have to be done for each file. Maybe they want a simple button push (or shortcut key) that they can use that will work on any file, any time, regardless of location - with no editing or typing required.

Which of course VBA could do.

macropod
04-08-2012, 06:28 PM
If you're using Word 2007 or later, you could use a custom Quick Part to insert the field. But since you don't, you don't have access to these new features ...

PS: When did the OP become plural (they)?

fumei
04-08-2012, 08:24 PM
LOL. When gender is unknown WE make them plural.

Even with Quick Parts would you not still have to do it individually for each file? With a VBA procedure it could be applicable to any file from that one procedure.

macropod
04-08-2012, 08:34 PM
Even with Quick Parts would you not still have to do it individually for each file?
No more so than having to run the macro for each file. It's a simple Insert|Quick Part|PicLink, where 'PicLink' is the name given to the custom Quick Part - or you could assign a shortcut key combo to it. Of course, if you were to use a template with the field already there, it'd point to the right picture as soon as you save the document in the applicable folder.

Talis
04-08-2012, 09:10 PM
Place the cursor where you want the image then run this subroutine:
Sub InsertImage()
ActiveDocument.InlineShapes.AddPicture _
FileName:=ActiveDocument.Path & "\images\image1.jpg", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
End Sub

This is as simple as it gets and in particular has no error trapping.
It assumes that the document has been pre-saved along with a folder named 'images' in which there is an image file named 'image1.jpg'.
If an error message appears click 'End' and check the above.

You may have other requirements such as adding carriage returns before and after.

fumei
04-08-2012, 11:46 PM
Thanks for posting the actual code. I deliberately did not, as I was hoping the OP would actually do some of the research I suggested. You are kinder than I am.

Talis
04-09-2012, 10:46 AM
I understand where you're coming from fumei and your response was predictable - and I mean that in a friendly way. Almost all my smattering of knowledge of Word VBA has come from going through the archives of this forum and reading your's and Macropods' posts - a great option for anyone who wants to learn. At the same time I would like think that this forum is there to help people who have not used macros previously and demonstrate to them how they can be used to improve efficiency.
Later they can start writing their own code, probably as simple modifications - as you suggested in this case; but it is important to register that when someone says: "I don't know anything about VBA ..." they are going to have great difficulty understanding code posted in previous posts.
My opinion is that beginners will be encouraged to explore VBA if they are given something that works for their immediate situation which they can then try to tweak as their confidence in VBA's usefulness grows. I am fully aware that you consider the main focus of this forum should be to get people to learn correct VBA and I apologize if I have upset you by providing a basic piece of code.

fumei
04-09-2012, 11:39 AM
Upset? Heck no. That is why I thanked you. I was not being sarcastic or anything else.

I am happy to post code for people, but I do want people to indicate that they are at least willing to TRY, to at least do SOMETHING...anything. If they are not willing to try at least once..then..shrug...what can I say? I definitely do not believe in hand holding, although I most certainly DO believe in giving people a hand up.

The original question: "I want a macro to insert image1 into my the word doc which will be in c:\work\job name"

That is a simple straightforward request, and there are many examples of how to do that, including a recent post. I was hoping to OP would post something that indicated they did, in fact, get an image inserted via code.

The next step was to get an image inserted (via code) that matched the request/requirement - i.e. a folder down from the folder containing the document. If the OP had shown that they at least tried SOMETHING, I would have been happy to expand their code example to show the way.

I did in fact give a hint, ActiveDocument.Path (which of course you used in your post). However the OP did not even bother to ask anything at all. Nothing. Again, I do not believe in hand holding, but I do believe in helping.

We learn a lot by making mistakes, and trying to fix things. The key though is in making SOME effort. And especially in trying to respond to hints and/or questions. If you can not be bothered to proceed, I won't be bothered to drag you there.

Shrug. Like I said, you are kinder than I am. And no, I am not upset or bothered whatsoever. Good on you.

Talis
04-09-2012, 05:44 PM
Point taken!

And it seems legg1979 'has left the building'.

Anyway here's one way to add CRs before and after an inserted image (even though there's a deathly hush from the op):
Sub InsertImage()
Selection.TypeParagraph
ActiveDocument.InlineShapes.AddPicture _
FileName:=ActiveDocument.Path & "\images\image1.jpg", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
With Selection
.MoveRight
.TypeParagraph
End With
End Sub