PDA

View Full Version : Photo Macro



JMP
07-04-2004, 10:40 AM
I have a add and resize photo macro which should work but doesnt!!

Please can you advise me where it should live and what I need to do in order that other machines will be able to use it from within the document!?

The idea is that by pressing Alt + P the user will be sent to a folder and then may select a photo which will be resized to 1.4 by 1.8 inches.

Thanks in anticipation.

John

Sub AddPicturefromdirectory()
If Selection.Information(wdWithInTable) Then
Dialogs(wdDialogInsertPicture).Show
With Selection.Cells(1).Range.InlineShapes(1)
.LockAspectRatio = msoTrue
.Width = InchesToPoints(1.4)
.Height = InchesToPoints(1.8)
.Range.Copy linktofile:=False, savewithdocument:=True
End With
Else
' Error Message and Quit
End If
End Sub

Anne Troy
07-04-2004, 01:49 PM
I edited your post to use the VBA tags, John. You can see how to use them at the link in my signature. Just makes it easier to read your code. :)

Anyway, why doesn't it work?

If you want anyone to be able to use this in any file, they must copy the code into their normal.dot file. To do that:

1. Open Word.
2. Hit Alt+F11 to open the Visual Basic Editor (VBE).
3. At left, you should see "Normal" (or maybe it will say Normal.dot); select it.
4. From the menu, hit Insert-Module.
5. Paste or type the code into the window that shows up at the right of the VBE screen.
6. Hit the diskette to save the code into the normal.dot file.
7. Close the VBE with the X (like you would any other program).
8. Assign the code to a shortcut key or toolbar button, following the instructions in Word Help:



Assign shortcut keys to a command or other item

You can assign a shortcut key to a command, macro, font, AutoText entry, style, or a commonly used symbol.

On the Tools menu, click Customize.
Click Keyboard.
In the Save changes in box, click the current document name or template in which you want to save the shortcut key changes.
In the Categories box, click the category that contains the command or other item.
In the box to the right, click the name of the command or other item. Any shortcut keys that are currently assigned appear in the Current keys box.
In the Press new shortcut key box, type the shortcut key combination you want to assign.
Click Assign.

JMP
07-04-2004, 02:41 PM
The macro fails.

All I want to do is to have a macro that on pressing Alt+P allows the user to go to a folder - choose a photo and for the macro to then place the photo after it has been resized automatically.

Thanks

John

JMP
07-04-2004, 02:46 PM
I should have said, "I want it only to appear in the *.doc which it refers to!" but the code still doesnt do what it says on the tin!!.

John

TonyJollans
07-04-2004, 04:13 PM
Hi John,

What are you trying to do with the line ..

.Range.Copy linktofile:=False, savewithdocument:=True ??

I think you should remove it; the macro seems to do what you want without it.

JMP
07-04-2004, 11:51 PM
Thanks Tony


Best wishes

John

JMP
07-04-2004, 11:57 PM
Tony

It does work UNLESS you decline the offer to add photo then it goes into debug!

Can I cover that eventuality? Please!

John

jamescol
07-05-2004, 12:15 AM
John,
You just need to add error handling to your procedure. Something like this:


Sub AddPicturefromdirectory()

'Turn on error handling - there are multiple ways to handle errors.
'You can read about them in your VBA Help
On Error Goto ErrorHandler

If Selection.Information(wdWithInTable) Then
Dialogs(wdDialogInsertPicture).Show
With Selection.Cells(1).Range.InlineShapes(1)
.LockAspectRatio = msoTrue
.Width = InchesToPoints(1.4)
.Height = InchesToPoints(1.8)
End With
'You have to force an exit from here to prevent teh ErrorHandler from running
'when there is no error
Exit Sub
End If

'Notice above you don't need your Else statement. The error handler
'replaces it.


'You must insert the error handler code after all other code in your procedure
'in my example. There are ways to handle errors in-line during code
'execution. Just examine the VBA Help documentation.

ErrorHandler:
'You can do several things when encountering the error. My example simply
'displays a message box with the error's description

msgbox err.description

End Sub


Cheers,
James

TonyJollans
07-05-2004, 12:59 AM
You don't really need error handling to trap this - it isn't an error, it's just a user choice.

The Show Method gives a return code to indicate which button has been pressed. The Insert button returns -1, so all you need to do is ..

Sub AddPicturefromdirectory()
If Selection.Information(wdWithInTable) Then
If Dialogs(wdDialogInsertPicture).Show = -1 Then
With Selection.Cells(1).Range.InlineShapes(1)
.LockAspectRatio = msoTrue
.Width = InchesToPoints(1.4)
.Height = InchesToPoints(1.8)
End With
End If
Else
' Error Message and Quit
End If
End Sub

JMP
07-05-2004, 01:38 AM
That did it !

Thanks very very much.

John