Showing posts with label Windows Development. Show all posts
Showing posts with label Windows Development. Show all posts

Wednesday, January 9, 2008

Why my VSTO Office 2003 addin doesn't work?

First things you need to know

  1. You can see the list of detected and enabled addins in Outlook in:
    Tools->Options->Other->Advanced Options->COM Add-Ins...
    If there were some error and addin has not been loaded it will be there with unchecked box. This is where you enable disabled addins. When you check addin and click OK - addin will be enabled right away, you don't have to restart Outlook

  2. If addin prevented Outlook from functioning correctly it will be "hard disabled" and put there:
    Help->About Microsoft Office Outlook->Disabled Items...

  3. When addin fails to load, by default, we don't see any info about it or the error details. But we can change it!
  • Open command prompt:
    cmd
  • Type:
    set VSTO_SUPPRESSDISPLAYALERTS=0
  • Run Outlook from that command line, should be like this:
    "c:program filesMicrosoft OfficeOffice11Outlook.exe"

Now if something fails with addin - you will see it.

This will give you good basic to debug why your addin doesn't work.
I recently had a lot of problems with it. I thought about sharing it with you so that you don't have to go through it again :-)
BTW: you won't find those information on any forums or other pages ;-)

Things to verify

  1. Install all required assemblies to GAC. This gives them full permissions so if it's something with permissions now it should work. If it does work - check your CAS settings.

  2. Check manifest file. If you played with solution reorganization, changed names of projects, classes - there will be a problem.
    Take a look at the underlined fragments:

    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" manifestVersion="1.0">
      <assemblyIdentity name="Sample.Addin.dll" version="1.0.0.0" />
      <asmv2:entryPoint name="Startup" dependencyName="dependency0">
        <asmv2:clrClassInvocation class="Sample.Addin.OutlookAddin" />
      </asmv2:entryPoint>
      <asmv2:dependency asmv2:name="dependency0">
        <asmv2:dependentAssembly>
          <assemblyIdentity name="Sample.Addin" version="3.2.0.0" publicKeyToken="c45b3b012bedcf43" />
        </asmv2:dependentAssembly>
        <asmv2:installFrom codebase="Sample.Addin.dll" />
      </asmv2:dependency>
    </assembly>

  3. Then take a look at the registry setting in the setup project. Verify all paths.
    Make sure that XXX in keys below are equal to ProgID:
    HKCU/Software/Classes/XXX/CLSID
    and
    HKCU/Software/Microsoft/Office/Outlook/Addins/XXX

  4. Got this:
    "Could not load file or assembly 'Microsoft.Office.Interop.Outlook, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies.
    System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Office.Interop.Outlook, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies...."


    Yeah, had this too :) Make sure that you don't have any Office 2007 applications installed on the development machine. I had SharePoint Designer. Uninstall them and reopen solution. Dependency to office.dll 12.0.0.0 should be gone (verify it in the dependency list in setup project).
    Rebuild setup project.


I think that's it :)
Good luck!

Friday, December 21, 2007

Can TextBox.Text ever be null?

Recently I have been asked this question. Answer is that it cannot be null and will not be null, so you don't have to:
.Text != null

Why? Because .Text property in base class does:
    set 
{
if (value == null)
{
value = "";
}
.....
}

Above is for TextBox from Windows.Forms, as for TextBox from WebControls it's the same. The getter looks like this:
    get 
{
string str = (string) this.ViewState["Text"];
if (str != null)
{
return str;
}
return string.Empty;
}

If you don't believe me just try to assign a null value :-)

Wednesday, November 21, 2007

Closing Form with WebBrowser control after window.close with C#

I recently had this problem. I have a Windows Form that hosts a WebBrowser control. The page in the WebBrowser control closes itself using window.close and when it does that the Windows Form freezes up.

Found a solution here posted by Jeff Sanders on his blog (thanks man!)

The sample code is provided in VB.NET Below is the same code rewritten in C#

public class ExtendedWebBrowser : WebBrowser
{
public const int WM_PARENTNOTIFY = 0x0210;
public const int WM_DESTROY = 2;

public delegate void ClosingEventHandler();
public event ClosingEventHandler Closing;

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_PARENTNOTIFY:
if (!this.DesignMode)
{
if (m.WParam.ToInt32() == WM_DESTROY)
{
if (this.Closing != null)
{
this.Closing();
}
}
}
base.DefWndProc(ref m);
break;

default:
base.WndProc(ref m);
break;
}
}
}