December 2006

Get Excel Instance without IDTextensibility2 in .Net

Here is a little .Net app that shows how you can get hold of an excel instance so that you can use that Excel object from code - but with out using IDT. This is useful when writing classes rather than managed addins. I will hopefully make a post about how we can use .net classes from VBA in a little bit, but this is worth a post in it's own right.

GetInstance.JPG

here is the example app

Know, the most interesting method here is the bind to moniker one as that lets us get the application that a particular file is active in, and so give use the most certainty that we are targeting the Excel instance we wish to. I have stuck this into a function:

Imports System.Runtime.InteropServices
Imports xl = excel.application

'''Class for Getting an Excel Intance

Public Class GetExcelInstance

Public Function GetTargetXLApplication(ByVal sPassedFilePath As String) As Excel.Application
Dim sFilePath As String = sPassedFilePath
Dim wkbBindToBook As Excel.Workbook
Dim blXLBelongsToUs As Boolean ''FLAG FOR KILLING XL APP LATER
Dim cFiles As System.IO.FileInfo
Dim sWkbName As String

''get filename from path
cFiles = My.Computer.FileSystem.GetFileInfo(sFilePath)
sWkbName = cFiles.Name

Try

''Set our target wkb from the file path to our
''.net wkb ojbect dimed here
wkbBindToBook = Marshal.BindToMoniker(sFilePath)
''set the appliction referance to the host excel sever this workbook is running in
GetTargetXLApplication = wkbBindToBook.Application

''the following code manipulates the excel clients window state.
If GetTargetXLApplication.Visible = False Then
''we opened excel
GetTargetXLApplication.Visible = True
GetTargetXLApplication.Windows(sWkbName).Visible = True
blXLBelongsToUs = False  ''FLAG FOR QUITING IF WE STARTED IT
'     MsgBox("Moniker from no intance") ''for debug
Else
''excel was opened but we loaded a hidden file into it, lets make it visable
If GetTargetXLApplication.Windows(sWkbName).Visible = False Then
GetTargetXLApplication.Windows(sWkbName).Visible = True
'         MsgBox("Moniker from existing intance")  ''for debug
End If
'     MsgBox("Moniker from existing intance")      ''for debug
End If

Catch ex As Exception
MsgBox(ex.Message)
GetTargetXLApplication = nothing 'sets function to null object if error happen
End Try
End Function

The VS project files are here

Any questions feel free to leave a comment, cheers Ross

Old dates, New VBA

Here's something i did not know about VBA!

http://www.puremis.net/excel/cgi-bin/yabb/YaBB.pl?num=1166424720/0#8

Cool, now i just have to get a job at the Natural History Museum.

File Level Protection demo

Last in the series of videos about workbook protection. My new wide screen seams to have effected the video, I could not get it into a editor, and it looks a bit strange in the media player too. Having said that if you need to know how to protect office or Excel files, this will show you a couple of ways.

Happy Birthday MIE Blog!

This blog is one year old!, I've made about 90 post in that time, not very impressive but hey you miss 100% of the shots you don't play!

I hope that in the next year this blog will contain more content about Excel and .Net, as well has lots more videos and VBA stuff!

hbone.jpg

Thanks for reading everyone!