+ Reply to Thread
Results 1 to 2 of 2

Macro inc. on error go to start, import image, crop image

  1. #1
    Registered User
    Join Date
    01-16-2012
    Location
    Dunkeswell
    MS-Off Ver
    Excel 2007
    Posts
    5

    Macro inc. on error go to start, import image, crop image

    This is a Macro that has taken me weeks of research to write, mainly because I have had no experience with Macro before and finding the information on how to write the different codes took alot of research so I am sharing this Macro on here so that anyone struggling with any part of their macro that is similar to mine can at least have a reference to start from, especially seeing as most of the Macro's I found had no real explanation to them and the code often didn't make any sense to someone who didn't know a thing about them!

    1. The first part of the macro gets it to pause for 45 seconds before it is run (I have an outside program creating the files my macro needs and it triggers my macro so it has to wait before it starts for the files to be created.)

    2. It then looks for a file path in the cell that has been called "JPEG" again this path is written by my outside program. So if the "picpath" = 0 (0 meaning the files isn't there) then got to "Start:" which as you can see sends it back to the start so that it pauses again. This stops the macro from crashing because even if the file isn't there it has a command to follow.

    3. when the file has been created it then imports the file (an image) and puts it in the right place.

    4. I then crops the image down to the sizes that I have specified (this was a bit of trial and error really) I just changed the numbers after it had been run a few times to suit what I needed.

    5. It then looks at a different sheet to import a company logo. I done this last because the crop command crops all the pictures and it was cropping to logo down too which I didn't need it to do.



    Please Login or Register  to view this content.
    If this helps just one person it will be worth it! Good luck to any Newbies out there too... I feel for you!

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Macro inc. on error go to start, import image, crop image

    Please confirm whether this updated code works as intended. I cleaned up the spacing/indenting a bit, and Dim'd all of the variables first and as the correct types. (Don't Dim variables as Integer, as they are just converted to Long during run-time processing anyway.) I assumed anywhere that referenced "ActiveSheet" was a reference to the sheet named "PerryLogo". Also, whenever you're specifying ranges on worksheets, and working with multiple worksheets, you should qualify each range or object reference with the worksheet it is linked to.

    You should really build error handling into the code to handle if you encounter a Shape type other than 13. You even mentioned it in the comments that it will error. You can probably just add an Else clause (to do nothing, simply skip the object) to your If statement. Also, when the Picture is created, I set the name property to name it specifically "Picture 1". If you ran it twice, the second picture would otherwise be created as "Picture 2" (then 3, 4, etc.) and your code would bomb when it tries to Cut/Paste "Picture 1".

    I also added Option Explicit at the top, which forces you to Dim all variable names and helps prevent errors using them. There were a few variables that weren't declared, and some that were declared but not used (probably from previous versions of the code).
    Please Login or Register  to view this content.
    Last edited by Paul; 02-29-2012 at 02:46 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1