+ Reply to Thread
Results 1 to 4 of 4

Thread: VSTO : First forays into Excel Automation!

  1. #1
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,006

    VSTO : First forays into Excel Automation!

    Note I was using Visual studio 2010 and Excel 2010 for this!

    But I decided to try and colour a sheet from a JPG dont ask me why, but it seemed like a fun application, the first thing I found is that there is a large lack of information but here is the code incase you wish to try!

    Imports System.Drawing
    Imports Microsoft.Office.Interop.Excel
    
    Public Class ThisWorkbook
    
        Private Sub ThisWorkbook_Startup() Handles Me.Startup
    
            Dim StepSize As Integer = 4 'speed up by only processing every nth pixel
    
            Dim MyImage As New Bitmap("C:\Users\Daz\Pictures\Elephants\DSCF0789.jpg")
    
            Dim PixelColor As Color  ' Contains the pixel from the image
            Dim GreyScale As Integer  ' used in converting to greyscale, since it overloads the maximum formatting otherwise
    
    
            Me.Application.ScreenUpdating = False
            Me.Application.EnableEvents = False
    
            For x = 0 To MyImage.Width - 1 Step StepSize
                Me.Sheets(1).Range("A1").offset(1, x / StepSize).columnwidth = 0.2 ' seemed to be 2 pixel wide excel cells
            Next
    
            For y = 0 To MyImage.Height - 1 Step StepSize
                Me.Sheets(1).Range("A1").offset(y / StepSize, 1).rowheight = 1.8 ' seemed to be 2 pixel wide excel cells
    
            Next
    
            For x = 0 To MyImage.Width - 1 Step StepSize
                For y = 0 To MyImage.Height - 1 Step StepSize
    
                    'read pixel from bitmap
                    PixelColor = MyImage.GetPixel(x, y)
    
                    ' convert pixel into greyscale
                    GreyScale = PixelColor.R * 0.3 + PixelColor.G * 0.59 + PixelColor.B * 0.11
                    ' set cell on excel sheet to pixel color
                    Me.Sheets(1).Range("A1").offset(y / StepSize, x / StepSize).interior.color = RGB(GreyScale, GreyScale, GreyScale)
                Next
            Next
    
            ' select image range
            Me.Sheets(1).range("A1:" & Me.Sheets(1).Range("A1").offset(MyImage.Height / StepSize - 1, MyImage.Width / StepSize - 1).address()).select()
            'zoom to full image
            Me.Windows(1).Zoom = True
    
            Me.Application.EnableEvents = True
            Me.Application.ScreenUpdating = True
    
            MyImage = Nothing
    
    
        End Sub
    
        Private Sub ThisWorkbook_Shutdown() Handles Me.Shutdown
    
        End Sub
    
    End Class

    The resulting file is here (its too big to post here) :-

    http://cid-7c0eef3f7aa6f25b.skydrive...0Pricture.xlsx

    ignore the error, as I havent included the compiled code!
    Last edited by squiggler47; 05-13-2010 at 02:00 PM. Reason: Added Link
    Regards
    Darren

    Update 12-Nov-2010 Still job hunting!

    If you are happy with the results, please add to our reputation by clicking the blue scales icon in the blue bar of the post.

    Learn something new each day, Embrace change do not fear it, evolve and do not become extinct!


  2. #2
    Forum Contributor daksh1981's Avatar
    Join Date
    04-15-2009
    Location
    India
    MS-Off Ver
    2003 & 2007
    Posts
    110

    Re: VSTO : First forays into Excel Automation!

    Hi squiggler47,

    I am unable to try this. I would like to try this code. I am using Excel 2007. Do I need to reference something for this ? Please guide me.

    Regards,
    Daksh

  3. #3
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,006

    Re: VSTO : First forays into Excel Automation!

    you need Visual Studio 2005 - 2010 with VSTO installed, (express editions wont work) and you select an excel 2007/2010 automation project!
    Regards
    Darren

    Update 12-Nov-2010 Still job hunting!

    If you are happy with the results, please add to our reputation by clicking the blue scales icon in the blue bar of the post.

    Learn something new each day, Embrace change do not fear it, evolve and do not become extinct!


  4. #4
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,778

    Re: VSTO : First forays into Excel Automation!

    Daksh, please ask your question in one of the question forums. This isn't one.

    Thanks.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ 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.2.0