Consulting

Results 1 to 10 of 10

Thread: Photo Macro

  1. #1
    VBAX Regular
    Joined
    Jun 2004
    Posts
    8
    Location

    Photo Macro

    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

    [vba]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[/vba]

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    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.
    1. On the Tools menu, click Customize.
    2. Click Keyboard.
    3. In the Save changes in box, click the current document name or template in which you want to save the shortcut key changes.
    4. In the Categories box, click the category that contains the command or other item.
    5. 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.
    6. In the Press new shortcut key box, type the shortcut key combination you want to assign.
    7. Click Assign.
    ~Anne Troy

  3. #3
    VBAX Regular
    Joined
    Jun 2004
    Posts
    8
    Location

    Angry Photo Macro

    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

  4. #4
    VBAX Regular
    Joined
    Jun 2004
    Posts
    8
    Location
    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

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    VBAX Regular
    Joined
    Jun 2004
    Posts
    8
    Location
    Thanks Tony


    Best wishes

    John

  7. #7
    VBAX Regular
    Joined
    Jun 2004
    Posts
    8
    Location
    Tony

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

    Can I cover that eventuality? Please!

    John

  8. #8
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    John,
    You just need to add error handling to your procedure. Something like this:

    [vba]
    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
    [/vba]

    Cheers,
    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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 ..

    [VBA]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
    [/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    VBAX Regular
    Joined
    Jun 2004
    Posts
    8
    Location
    That did it !

    Thanks very very much.

    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •