CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2015

    I need some help with this code for Microsoft Excel

    Some one made this code for me. Its for work to add pictures to an excel document and have them re-size. If I email it to someone the links are broken and the pictures don't show up. Any help would be greatly appreciated. TIA

    Sub ExampleUsage()
    Dim myPicture As String, myRange As Range
    myPicture = Application.GetOpenFilename _
    ("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", _
    , "Select Picture to Import")

    Set myRange = Selection
    InsertAndSizePic myRange, myPicture
    End Sub

    Sub InsertAndSizePic(Target As Range, PicPath As String)
    Dim p As Object
    Application.ScreenUpdating = False
    Dim sh As Worksheet: Set sh = ActiveSheet
    Set p = sh.Pictures.Insert(PicPath)
    sh.Shapes(p.Name).LockAspectRatio = False

    If Target.Cells.Count = 1 Then Set Target = Target.MergeArea
    With Target
    p.Top = .Top
    p.Left = .Left
    p.Width = .Width
    p.Height = .Height
    End With
    Application.ScreenUpdating = True

  2. #2
    Join Date
    Apr 2014

    Re: I need some help with this code for Microsoft Excel


    I am not quite clear about the reasons why your issue appears when emailing the excel file. but you could try some other solutions to re-size and insert images to Excel sheet. Here is an sample for your reference:

    Imports Spire.Xls
    Imports System.Drawing
    Imports System.IO
    Namespace InsertImage2Excel
    	Class Program
    		Private Shared Sub Main(args As String())
    			Dim workbook As New Workbook()
    			Dim sheet As Worksheet = workbook.Worksheets(0)
    			Dim image__1 As Image = Image.FromFile("logo.jpg")
    			'scale image in persentage and insert it to excel
    			Dim scaleWidth As Integer = 50
    			Dim scaleHeight As Integer = 50
    			sheet.Pictures.Add(1, 1, image__1, scaleWidth, scaleHeight, ImageFormatType.Jpeg)
    			workbook.SaveToFile("ExcelImage.xlsx", ExcelVersion.Version2010)
    		End Sub
    	End Class
    End Namespace

Tags for this Thread

Posting Permissions

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

Click Here to Expand Forum to Full Width