How To Load Win32 dlls Dynamically In VBA

Well, by dynamically i just mean that i don’t know the path of the dll (or the path will be decided at runtime). now the problem is; you need to add a declares statement to use a function from VB but you don’t know the path so there is no way in which you can give this statement unless the dll is going to be in the default dll search path. 

Now when i faced this question i only had a few things in my mind (i haven’t tested (1) and (3) so please no rants if they don’t work or mess something up)

1) Use "LoadLibrary" API. But the problem over here is it gives you the handle of the DLL then you can use "GetProcAddress " to get the address of the function you are interested in. I know ..i know its not that simple; but i am just over simplifying it because anyways you don’t have a function pointer in VBA so you can’t use the function even if you have the address. If you want to do it in VC have a look at Using classes exported from a DLL using LoadLibrary. Yes! i know there is a way to use it even from VB, but its looks so scary that i will not use it any application other then the once that only i am going to use, moreover using these sort of hacks workarounds means that you don’t get any support when you are stuck!. If you are determined enough to use LoadLibrary from VB Here is the scary code.

2) Insert a module at the run time, this is something that i preferred when i faced this issue. The simple concept is to add a module at runtime containing the declares statement when you have the path information. Remember you will also need to add a dummy function dynamically which will call the real function that you want to call. Why ? because you can not include the real function in the code as it will generate a compile error. To call the dummy function you will need to use "Application.Run("DummyFunction") as you can not call it directly (Please .. don’t ask me why !! because it will give a compile error as its not defined you will add it dynamically). below is the quick and dirty code that i used.

Public Sub AddingFunctionDynamically()
    Dim pth As String, ss As String
    pth = InputBox("Where is it ?")
    ss = "Public Declare Function Beep Lib """ & pth & """ (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long" & vbCrLf & _
    "Sub DummyFunction()" & vbCrLf & _
        "Beep 1000, 1000" & vbCrLf & _
    "End Sub"
    Application.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule).CodeModule.AddFromString (ss)
    Application.Run "DummyFunction"
End Sub

.csharpcode, .csharpcode pre
{font-size:small;color:black;font-family:consolas, "Courier New", courier, monospace;background-color:#ffffff;}
.csharpcode pre
{margin:0em;}
.csharpcode .rem
{color:#008000;}
.csharpcode .kwrd
{color:#0000ff;}
.csharpcode .str
{color:#006080;}
.csharpcode .op
{color:#0000c0;}
.csharpcode .preproc
{color:#cc6633;}
.csharpcode .asp
{background-color:#ffff00;}
.csharpcode .html
{color:#800000;}
.csharpcode .attr
{color:#ff0000;}
.csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.csharpcode .lnum
{color:#606060;}

 

2) Modify the dll search path  but again you will need to use a dummy function and you will need to add it dynamically ..so why not go the easier way (read (2))

Welcome Windows Live Writer !!

Sorry guys i had to keep my mouth shut for many days…!  actually since a month (i guess ) i was blogging using dogfood builds of the cool new product from Microsoft..Windows..Live ..Writer ..!  no ..don’t get confussed its not only for using with windows live, it supports most of the comman blogging formats like atom api, metaweblog api etc. and most important thing is .. its very cost effective..yeah free..!!! you heard it right !!!! (and we are not bundling it with our OS so ..;) don’t cry )

I know your next question is why ? when Word 2007 already has that capibility .. why are we giving out two products with the same intension ..no we are not doing it .. both are for different people.. if you use Word 2007 very often ..then you can use word for blogging but if you don’t use it please don’t buy word just for blogging ..(unless you are habitual of buying a AK – 47 for killing birds 🙂 )

so the download link for live writer is http://download.microsoft.com/download/f/9/a/f9a19f2d-cec4-4a25-9b0b-eb9655ea7561/Writer.msi  if you want to know more about it you can check the page of windows live writer team blog http://windowslivewriter.spaces.live.com  below is a pic..and if you are thinking about the right hand insert pane..yes you are right ! windows live writer comes with an SDK so that you can extend it to do whatever (you know i don’t actually mean whatever ) you want !

Tagging Information With Windows Desktop Search API

Found something interesting while wandering on the web . The desktop search API, for the records I regularly use Desktop Search, and I never understood how did I worked before it..!

One more think that I like is the concept of tagging information, yes I know information can be found by searching but how the information relates with you ? this is a question that in my opinion no software can ans, because for answering this you will need to tell *what* is the relevance of *which* information to you. *How* do you relate to that information. So in *my* opinion tagging is the key. Have a look..

   
 

Windows Desktop Search, which is installed with MSN Search Toolbar, will find anything on your computer — e-mail messages, calendar appointments, photos, documents, and more. Searching your computer is as fast and easy as searching the Web, and the Desktop Search results window includes rich Windows Explorer features, such as multi-select, right-click, and drag-and-drop functionality.

Windows Desktop Search can be extended to index the contents of new file types and data sources. The user interface can be extended by associating icons and context-menu handlers with those items.

Also, queries can be passed into the Windows Desktop Search application and returned so that other applications can display the result sets from within the application.

This document explains these beta extensibility options and their implementation.

     
 

Now as the API is already provided I will try to build a tool for my use, which will allow users to tag the information. Unfortunately I have to earn my daily bread so I will not be able to give much time to this project but I will give as much as possible. Sure you can extend and use it as I will also upload the source code here (or maybe not exactly here I may also choose to keep it on a GotDotNet workspace).

I invite your suggestions for building this tool in a way that’s most useful. Please help me (and yourself ) by commenting on this entry as you get any idea. Don’t even try to judge your idea or how it can be implemented, let us get some ideas then we will think how to implement it

   
 

Inserted from <http://addins.msn.com/devguide.aspx>

Add Your Own Maps !!!

Believe it or not now you can add your own maps to Virtual Earth, at least I always wanted to do this, whenever I was limited views of my native place I always thought , why don’t they ask real people for the detailed maps ? and finally they did it ..!! Well I know ..innovation is nothing new for Microsoft Research .. but wow guys thanks ..I like it !!

Have you ever looked at satellite photos of a building in Virtual Earth — and wished you could zoom right in and see its floorplan? Have you ever used VE to plan a trip across town — and wanted to seamlessly switch from its road maps to maps of bicycle trails, bus routes, or carpool lanes? Have you ever wanted to create and publish your own map mashups — and wished you had a tool to make it easy to integrate a map you care about into Virtual Earth? With MapCruncher, you can!  

The Virtual Earth API allows web developers to supplement Virtual Earth’s maps with pushpins and lines. MapCruncher brings mashups to a whole new level by allowing developers to import entire maps to supplement the existing road and aerial imagery with detailed, application-specific information. The possibilities are endless: bicycle maps, transit maps, national park maps, university maps, antique city maps, or whatever drawn-to-scale maps you personally find interesting. You can even augment Virtual Earth with Do-It-Yourself Aerial Photography. See our Gallery for more examples.

 
 

Inserted from <http://research.microsoft.com/mapcruncher/>

Microsoft Office Beta – 2 Is Available !!

Great News !!! Microsoft Office Beta 2 is available for public review, wow I am so happy ..let me try to count the endless list of things that you can do now ..!!!

  • You can try the new ribbon UI, really it makes live easier ..
  • You can blog using the new blog feature of Word.
  • You can try new XML based extensibility modal, most add-in related plumbing is gone
  • Use the new OneNote note sharing feature.
  • Use OneNote BlogsThis feature.
  • Try SmartArt.
  • …. And so on ….

So how do you download it ..? good question to download it go to http://www.microsoft.com/office/preview/default.mspx

Blogging With Word 2007

One of the greatest advantages of working with Microsoft is Dog-Fooding, getting the hands on the technology before it actually comes out in the market, I really enjoy that and now as I already told you Beta 2 Word 2007 now has blogging that as a new feature.

Yes, I agree it’s a beta, it still has areas of improvement, but the important point is, product team, is working interactively with the dogfooders , in fact Joe, Krista jumps almost in every mail that you send to word blogging DL (it’s a special DL in which all the MS bloggers who are blogging using word are members ) and also he keeps on monitoring his previous posts on word blogging for issues and questions and gets back when / as he has a good number of questions and ofcourse answers to them. Have a look at http://blogs.msdn.com/joe_friend/archive/2006/05/18/600710.aspx

In general, I prefer to sit back and watch the comments come in and respond to them in future blog posts. This means that some folks have to learn a little about delayed gratification. However, no one needs to worry about missing important information that only gets mentioned in the comment stream. So, here are a few answers to issues raised by recent comments.

Once we raise an issue , we will see mails bombing from the entire team, Joe, Krista, Natalie, David, and many more in the team, if required they will actually have live meeting with you, will remote debug the issue and give you the results, its wonderfull working with people with such great commitments towards what they do.

 

 

Being In Microsoft – 2

This is the continuation of my post "Being In Microsoft – 1" actually when I started writing my previous post I was going to tell you things in Microsoft that make me proud that I am the part of such a company , but somewhere along the way, I got distracted from the main topic. In this post I will try my best not do get distracted, but before that let me tell you one more interesting thing that reinforced my thought of being in Microsoft.

Reinforcement Of Thought

When I was learning implementing COM using VB, I came across a book by Dan Appleman. If you have ever read his book the only thing you can say is he can never improve, because its hard to improve perfection (by the way if you were his blog reader a good news for you is he restarted blogging, thank Jeff Atwood for that ). Apart from COM I read a few more interesting incidents in his book. Here I am presenting one specific incident from his book that reinforced my thought of joining Microsoft, In his own words.

When people ask me how do you feel working with the biggest fish in the pond (i.e. Microsoft) , I tell them Microsoft is not the biggest fish in the pond; it’s the pond.

So yes, it’s the pond..!

Although I have many experiences in Microsoft in these 2 years of job, that I can dedicate an entire website with those, but to keep it simple, I will tell you the latest one.

Experience Of Support

Generally when I talk of support I talk of support that I we (our team or other teams in developer support) give to our customers, but this time the story was different I was asking for support.

Yesterday I checked that RSS feeds on my MSDN blog were not working, so I promptly wrote a mail to msdn blogs support and I went to sleep, I was expecting a reply in a couple of days as it was a weekend, but after a few hrs maybe three when I checked my mailbox , there was a mail from Jana, who is working in Microsoft communities(she is in Microsoft even before I started dreaming of Microsoft , yes more then 7 years) and the issue was resolved !! on a weekend ! in a few hrs.

When she read the mail , she immediately escalated the issue to telligent, the community server people (in case you don’t know MSDN Blogs is hosted on community server) and even the support guy also started working on it even though it was a weekend, and issue was resolved..!!

Thanks Jana .. Thanks Microsoft ..Thanks Dan ..Thanks Jeff ..Thanks everybody who is associated with Microsoft in any capacity.

Being In Microsoft – 1

People keep on asking me how it feels being in Microsoft . Actually it’s hard to explain because my limited vocabulary doesn’t have so many good words tom match my feelings ! but the good thing is I always have a pack of incidents which explain why I am proud being a part of Microsoft.

Let wander through history, don’t let me scare you I am not talking about history of mankind or history of universe, I am talking about history of a small micron in the universe and that’s me..

My First Love With Microsoft

This begun back in ’99 exactly around the time when Jana Carter joined Microsoft (I will tell how Jana came in my mind latter in this article in the next article ). I must admit that it had nothing to do with actually the company, but it had to do with idea.. it was the time when people started to talk about .NET and comparing .NET to Java, and ofcourse everybody on earth likes to predict things based on their opinion, I found out whenever anybody said anything bad about .NET I was not able to stand ..reason unknown, I didn’t even know exactly what’s .NET ! strange ..isn’t it ? It was at least for me. Then I started examining my thoughts like psychoanalyst would do and when I found the root cause it was actually the love for a specific language "Visual Basic 6.0".

Why I liked the language .. ? Yes , you are right.. it’s simplicity. But when you carefully analyze the language you will see that almost every redundant thing was made easy and code-free, so the developer did not have to waste his / her precious time, thinking about window handles, positioning, message loop etc. and if you needed more control API was always there to your rescue.

Dream Of Being A Part Of Microsoft

When anti-piracy thing was going on in our city I happened to meet with one of the people, whose name I don’t remember as per now, but he was an MS employee and somehow associated with anti-piracy. I bump into him in a coffee shop. After a round of coffee, he gave me his card and after taking that card in my hand I started imagining my name printed on that card and finally it did happen, although I did take around 6 yrs of efforts but it’s worth it. I wanted to say a few things more but I personally do not like too read too big articles so I avoid posting it as well, I will be posting rest of it in an hr with the title "Being In Microsoft – 2 "

Want me to blog on your tune..??

Just a new experimental idea .. I am starting to blog on your issues !! ofcourse they should be related to Office Automation or InfoPath.. so how to you tell me what to blog..good question.. I just created a channel9 wiki that will help you do it ..let’s hope it doesn’t get removed from there !

The URL of the entry is http://channel9.msdn.com/wiki/wikiedit.aspx?topic=Channel9.BlogRequest&return=Channel9.OfficeDevelopmentBlogs just login and let me know what you want and in most cases you will get it done..!! and I will ofcourse I will blog it on This Blog not on Blogspot or Spaces Blog