PDA

View Full Version : Finding the next image and copying it



Boggie1982
02-08-2016, 05:34 PM
Hi,
Ok I'm completely new at this, but I hope someone can help. I need a shortcut that will find the next image (or first image in current page) in a word document, and copy it... That's it.
It's part of a migration project into a different program. I'm automating parts of it using macros and autohotkey.
This is the 2nd macro ever that I'm trying to make. The first was to find text and copy it based on style in the word document (which would be migrated using AHK). The code for that used selection.find.style, then selection.find.execute and selection.copy ... It doesn't seem to be very transferable to an image.
I tried learning about objects and shapes and inline shapes, but I couldn't seem to find many good examples for what I'm trying to do.
Any help would be appreciated - even just getting me on the right track... Thank you so much.

gmayor
02-08-2016, 11:26 PM
Maybe something like the following, which first searches for 'floating' shapes on the page, and selects the first one it finds. If there are no floating shapes, it looks for the first inline shape on the page. If there are none of those either, it displays a message box. You can swap the searches aroound if you wish, or remove the search that doesn't apply if, say, you are only looking for inline shapes.
Option Explicit
Sub CopyFirstShape()
Dim oPage As Range
Set oPage = ActiveDocument.Bookmarks("\page").Range
If oPage.ShapeRange.Count > 0 Then
oPage.ShapeRange(1).Select
Selection.Copy
ElseIf oPage.InlineShapes.Count > 0 Then
oPage.InlineShapes(1).Select
Selection.Copy
Else
MsgBox "There are no images on this page"
End If
lbl_Exit:
Set oPage = Nothing
Exit Sub
End Sub

Boggie1982
02-09-2016, 05:38 PM
Oh this worked perfectly, thank you so much. It also helped me realize where my other pieces of code were lacking. I owe you a beer, or a six pack.

gmayor
02-09-2016, 10:15 PM
Beers and six packs are always welcome at my web site :content:

Boggie1982
02-10-2016, 01:42 PM
Beers and six packs are always welcome at my web site :content:

Enjoy!