Switch from picture in a cell to picture over cell

Anonymous
2024-06-14T08:02:23.46+00:00

Hi All, I want a code in VB.net to switch from picture in a cell to picture over a cell. The below is the code I have so far. Next step will be to select the cell B1 and make the picture over cell and save it in the desktop that picture. Thanks for your help

xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = True  
xlWorkbook = xlApp.Workbooks.Add()
xlWorksheet = xlWorkbook.Worksheets(1)
xlWorksheet.Activate
xlWorksheet.Range("A1").Value = "https://a360.co/3UKfwfrwkZj"
xlWorksheet.Range("B1").Value = "=IMAGE(""https://api.qrserver.com/v1/create-qr-code/?size=80x80&data=""&A1)"

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,642 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 28,001 Reputation points Microsoft Vendor
    2024-06-17T06:28:51.5466667+00:00

    Hi,

    Please try the following code to see if it meets your need.

    Using the CopyPicture method to copy the picture.

    Using the Paste method of xlWorksheet to paste the picture on the cell.

    After pasting, using xlWorksheet.Shapes(xlWorksheet.Shapes.Count) to access the last shape in the Shapes collection.

            xlWorksheet.Range("B1").Select()
    
            Dim left As Double = xlWorksheet.Range("B1").Left
            Dim top As Double = xlWorksheet.Range("B1").Top
            Dim width As Double = xlWorksheet.Range("B1").Width
            Dim height As Double = xlWorksheet.Range("B1").Height
    
            xlWorksheet.Range("B1").CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlBitmap)
    
            xlWorksheet.Paste(xlWorksheet.Range("B1"))
    
            Dim xlShape As Excel.Shape = xlWorksheet.Shapes(xlWorksheet.Shapes.Count)
            xlShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoFalse
            xlShape.Left = left
            xlShape.Top = top
            xlShape.Width = width
            xlShape.Height = height
    
            Dim desktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            Dim savePath As String = IO.Path.Combine(desktopPath, "ImageOverCell.xlsx")
            xlWorkbook.SaveAs(savePath)
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments