Forum Discussion

office970549's avatar
office970549
Copper Contributor
Feb 24, 2024

InsertPictureInCell in VBA gets run-time error occasionally

Hi, I'm working on a VBA macro that Insert Picture based on cell values, the code goes like this:

Range("K" & i).Select

Selection.InsertPictureInCell ("D:\Folder A\" & Cells(i, 1).Value & "\" & Cells(i, 2).Value & ".jpg")

This code works at first, however, the next day when I want to continue my work, the macro show error at this line.

Later I found out that, insert a picture somewhere in the workbook manually before running the macro, and then the code will works fine.

Is it that I'm missing something in the code like get access to D drive??

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    office970549 

    The error you're encountering may be due to various reasons, but it's unlikely related to accessing the D drive. Here are a few potential reasons and solutions you can try:

    1. File Path: Ensure that the file path you're using is correct and accessible. Check if the file exists in the specified location and that the file extension (.jpg) is correct. Also, ensure that there are no extra spaces or special characters in the file path.
    2. Active Sheet: Ensure that the sheet containing the cell you're trying to insert the picture into is active when running the macro. You can explicitly specify the worksheet to avoid any confusion: Sheets("Sheet1").Range("K" & i).Select
    3. Picture Size: The InsertPictureInCell method may fail if the picture size is too large or if the cell size is too small to accommodate the picture. Try resizing the cell or the picture to see if it resolves the issue.
    4. Corrupted Workbook: Sometimes, workbooks can become corrupted, leading to unexpected errors. Try saving the workbook under a new name and see if the issue persists.
    5. Memory Issues: If you're working with a large number of pictures or complex Excel files, it's possible that there may be memory issues causing the error. Try closing any unnecessary applications or reducing the number of open Excel files.
    6. Excel Version Compatibility: Ensure that the InsertPictureInCell method is compatible with the version of Excel you're using. This method is available in newer versions of Excel (e.g., Excel 2010 and later) and may not work in older versions.
    7. Office Repair: If the issue persists, try repairing your Office installation. Sometimes, errors can occur due to corrupted installation files.
    8. Debugging: Use breakpoints and debugging tools in VBA to pinpoint the exact line of code causing the error and analyze the variables and values at that point.

    If none of the above solutions resolve the issue, it may be helpful to provide more information about your Excel environment (e.g., excel version, operating system, storage system, etc.) and the specific circumstances under which the error occurs. The text was created with the help of AI.

     

    If your "D drive" is a network drive mapped to a network location or if it's a folder synced with OneDrive, you can still access files from it using VBA in Excel. Just ensure that the file path you're using in your VBA code points to the correct location where the image files are stored.

    For example, if your OneDrive folder is synced to your "D drive" and your images are located in a folder named "Folder A" within OneDrive, the file path in your VBA code would look something like this:

    "D:\Folder A\" & Cells(i, 1).Value & "\" & Cells(i, 2).Value & ".jpg"

    However, if your "D drive" is a mapped network drive or has a different setup, you'll need to adjust the file path accordingly in your VBA code to point to the correct location where your image files are stored.

     

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

     

    Was the answer useful? Mark as best response and like it!

    This will help all forum participants.

  • MrBraun's avatar
    MrBraun
    Copper Contributor
    I have the exact same issue. If i freshly open the workbook it doesn't work regardless where the file is located. After inserting a picture manually it works.

Resources