Posts filed under '.Net'
(Updated Wednesday 30JAN2009.) Developing applications that manipulate OpenOffice documents has always been rather tricky; not very difficult, but just tricky to get right.
With OpenOffice 3, things got trickier and applications that used to work will now fail.
I’ve just spend nearly a day trying to get a mail-merge application I built to work again with the new version of OO.
Changes and limitations
Developing .Net applications with OpenOffice 3 now requires that the .Net framework 3.5 be installed.
Only one version of OpenOffice/StarOffice must be installed.
Getting the CLI assemblies in your project
OpenOffice used to ship with a bunch of CLI DLLs for manipulating Open Documents from .Net.
With OpenOffice version 3, these DLLs are directly installed in the GAC and not available in the Program installation folder any longer, making them unavailable when you need to add them as references to your Visual Studio project.
The trick is to extract the DLLs from the installation CAB, then reference those and make sure you exclude them in your setup file so only the latest version installed on the user’s computer’s GAC will get used.
- Open the installation folder where the installation files are extracted (usually left on your desktop under a folder like
OpenOffice.org 3.0 (en-US) Installation Files during the OO installation process.
- Open the
openofficeorg1.cab file (using a utility like 7zip if necessary) and extract the files matching
- Add the ones you need to your VS Project’s references and make sure the properties of these references have their Copy Local and Specific Version properties set to false.
- If you have a setup project that added these references to the list of files, right-click each of them and select Exclude to make sure they won’t be packaged.
The reason for not deploying the DLLs is that they are very specific to a given OpenOffice version and the latest assemblies relevant to the user’s OpenOffice installation will already be deployed to the target machine’s GAC.
When .Net loads assemblies, if they are not included in the path of the application being launched, they will be loaded from the GAC if they can be found there.
Path and environment setup
Before being able to call OpenOffice from your application you now must set your application’s environment variables to the correct paths so the assemblies can find the actual OpenOffice library and program files.
Basically, you need to add to your
PATH the path to the folder where the UNO java libraries reside.
You also need to add a
UNO_PATH environment variable that points to the program folder of OpenOffice.
Basically, before any call to OpenOffice functions you must:
- Append to
PATH something like
C:\Program Files\OpenOffice.org 3\URE\bin.
- Create a
UNO_PATH variable set to
C:\Program Files\OpenOffice.org 3\program.
Because there is no guarantee that these paths will not change or are valid for all systems you must get them from specific keys located in the Registry:
PATH is appended with the vaue of
HKLM\SOFTWARE\OpenOffice.org\Layers\URE\1\UREINSTALLLOCATION to which you must append the
UNO_PATH is set to the content of the
See the C# and VB.Net code below for working examples.
Special Considerations for x64 systems
My development machine runs Windows Server 2008 x64 and I’ve ran into some specific issues that you’re likely to encounter when deploying to a 64 bits OS.
OpenOffice is 32 bits only
That means that your .Net project must be set to target x86 systems only:
Open your Solution’s Configuration Manager and under Active solution platform click New… then:
Make sure you repeat this for both the Debug and Release configurations.
Registry keys are elsewhere
32 bit applications see their registry keys normally expected under:
HKEY_LOCAL_MACHINE\Software moved to:
This of course creates issues when you’re trying to read a registry key that’s not where it should be…
The code below will allow you to correctly connect to OpenOffice 3 under 32 or 64 bit systems.
It reads the registry to find the proper paths and appends the
PATH and creates the
UNO_PATH environment variables expected by the the bootstrapper to find the OpenOffice program and libraries.
The code is built upon information and a test program made available by Marten Feldtmann on his blog (more information, in English, is available on OOoForum ).
Please let me know if this works for you or if you have any corrections.
07MAY2009 -- Added reference link to OOo documentation.
03DEC2008 -- Added VB.Net translation. Thanks to Stefan for suggesting it.
30JAN2009 -- Added reference to Aleksandr Sazonov’s article on CodeProject (thanks for the the article).
November 6th, 2008
There are times when the coexistence of 64 and 32 bit code on the same machine can cause all sorts of seemingly strange issues.
One of them just occurred to me while trying to run the ASPx demos from Developer Express, my main provider of .Net components (the best supplier I’ve ever been able to find).
I was getting the following error:
The ‘Microsoft.Jet.OLEDB.4.0′ provider is not registered on the local machine:
It may look otherwise, but this error is generally due to either of two thing:
- you don’t have Office 2007/2010 Jet drivers installed
- or you are running a 32 bit application in a default x64 environment.
The first issue is easy to solve, just download the Access 2010 Database Engine from Microsoft (works with Access 2007 databases as well).
For the second one, the fix is also easy enough:
- For Windows 2008: Navigate to Server Manager > Roles > Web Server (IIS) > Internet Information Services (IIS) Manager, then look under your machine name > Application Pool.
- For Windows 7: Navigate to Programs > Administrative Tools > Internet Information Services (IIS) Manager, then look under your machine name > Application Pool.
Under there you can call the DefaultAppPool’s advanced settings to change
Enable 32-Bits Applications to
You may have to restart the service for it to take effect but it should work.
- 10DEC2011: Updated driver link to use the Access 2010 engine.
- 03APR2010: Added instructions for Windows 7
- 12FEB2009: Added reference to Scott’s article.
- 28OCT2008: Original version
October 28th, 2008
This project provides a custom and enhanced message box replacement for the default
MsgBoxfound in Access. A Test database for Access 2007 is available at the bottom of this post. (Updated Monday 01APR2013.)
What’s wrong with the default MsgBox
The default message box in Access is sometimes useful to warn, inform or ask confirmation from the user.
It has, however, a few drawbacks:
- It is bland: the standard message box does not even follow the currently selected Office 2007 scheme.
- The amount of text it can display is limited: if you try to display too much text it will be truncated.
- You can’t copy or save the content of the message.
- Because popup boxes are viewed as intrusive, people tend not to read them and end-up closing message boxes before they realize they may have contained useful information.
- They only displays plain text: you cannot format the message to draw attention to the key points.
- They are blocking, meaning that nothing can happen in the main application while the box is displayed (it can’t even shut down).
- It will only appear on the monitor that has the main Access application window, even though the message box may have been opened from a form on another monitor.
Sometimes you need to display an important message or require users to make take a decision.
Message boxes are not to be abused but they serve a useful purpose.
An enhanced message box
Rather than using the bland standard message box you can now have something a bit more customized.
Plain Text version of the enhanced custom message box under the Office Blue Colour Scheme:
RichText version of the enhanced custom message box under the Office Black Colour Scheme:
Here are the features of the enhanced message box:
- It is entirely compatible with the standard one: just change
Box using find and replace should be enough (see tip below to avoid getting strange errors).
- It allows the user to simply click on a button to copy the content of the message to
the clipboard or save it to a text file to a configurable default location.
- It looks and feels like it belongs to the main application, following its colour scheme.
- It attempts to prevent users from blindly closing the modal box reading the message: buttons will first be inactive for a configurable amount of time. It’s not a perfect solution, but it is quite effective.
- There is a
RichBox version that can display rich HTML content, not just plain text, so important parts of the message can be formatted in a useful way.
- It is able to display large amount of data. While it’s not something you usually want, it may be useful for the message box to display more text in some situations (log or tracing information, legal documentation, etc).
- Rather than sprinkling your code with “
& vbCrLf & _” uglies, you can embed newlines in the text itself by using C-style “
\n” escape sequences that will automatically be transformed into the appropriate newlines. Makes for clearer code and less typing.
- Because you get the source, you can easily customise the message box with new icons and colours to better match your overall application’s personality.
- It is non-blocking: if your application forces users to log-off after a certain amount of inactivity, the enhanced message box will just close rather than prevent Access from shutting down like the standard
MsgBox does. Of course, it’s up to you to decide how to handle that gracefully, if at all.
- It properly displays the expected button captions based on the language of the operating system, so it behaves very much like the default
MsgBox (for instance, it will properly display “Cancel” on English systems and “Annuler” on French ones).
- It also properly plays the system sounds associated with the type of message. You can also enable or disable the sound effect as needed.
- As of version 1.4, it will properly display on the correct monitor in a multi-monitor setup.
How to use it
Download the demo below and copy (drag & drop) the following into your application:
If you rename the
FormDialog, make sure you replace any occurrence to it in the code, in particular in the
Since the enhanced message box is just a replacement for the standard one, you just use it like you would use the
As of version 1.4, you can also customise the button labels instead of using the default ones (thanks to Kristjan for the suggestion):
There are a few additional settings that can be used to change the behaviour of the enhanced message boxes.
One is that you can adjust the delay before the buttons become activated.
Another one is that you can enable or disable whether beeps should be played or not.
The last settings is the folder where we should save the content of the message when the user clicks the Save button on the message box.
These few settings make the enhanced message box more customizable.
MsgBox cannot display much text. On the other hand, there is no real limitation to the amount of text the
RichBox can display.
When the amount of information is too much to fit the maximum allowed size for the message box the text will overflow and can be scrolled up/down as necessary.
Limitations of the RichBox
RichBox version relies on the normal TextBox control’s ability under Access 2007 to display RichText wich is nothing more than lightweight HTML.
Because font size may be varying a lot in the message, it becomes very difficult to accurately predict the size of the box needed to display the whole message.
Short of implementing a complete HTML engine, we have to rely on some assumptions to display HTML.
The risk is that sometimes the content may not properly fit the TextBox control in some circumstances.
If you use the
RichBox, thoroughly try displaying your messages and tweak the HTML as necessary to include additional lines or non-breaking spaces to ensure that the result looks good.
If you don’t overuse font size and don’t display in multiple fonts the
RichBox should do the right thing most of the time.
Don’t overuse the
RichBox to display colourful messages. There is a fine line between being informative and tasteless. Keep colours and formatting where it is useful.
I think that in most cases, the plain text version
Box is more than enough.
Replacing MsgBox in existing code
As I said above, replacing the standard
MsgBox is easy but you need to make sure your search and replace parameters are configured correctly:
If you’re getting strange compile errors, it may be because you forgot to tick the Find Whole Word Only and some of the strings containing the letter sequence “msgbox” were replaced in the process.
If that’s the case, you can revert the damage by simply doing a search and replace across the whole project on:
VbDialog.BoxStyle to be replaced with
VbDialog.BoxResultto be replaced with
How it works
The code makes extensive use of Win32 API calls.
Most of the hard work is done in the
FomDialog class form. There is too much there to really go into the details but you are welcome to have a look at the commented code.
The code relies also on a utility function from Stephen Lebans used to calculate the size of of text. I have made some minor modification to that code so I would refer you to his original implementation if you are interested in calculating TextBox sizes for forms or reports.
In the code for the
FormDialog, I re-implement some of the expected functionalities of the
MsgBox: proper arrangement of the buttons, displaying of the appropriate icon, etc.
Once this is done, we calculate the size of the textbox needed to display the whole of the message.
In the case of RichText, we first use
Application.PlainText() to convert the HTML into properly formatted plain text. We then calculate the Textbox size using a slightly larger font than needed as a way to ensure that the content of the RichText message will fit the box in most cases.
Once we know the size of the TextBox, we can easily resize the form to properly display the TextBox.
If there is too much text, we resize the form to its maximum permissible (70% or screen width and 90% of screen height) and change some of the visual cues to let the user know the text is overflowing.
One thing of note is the way the form is kept modal.
Rather than using
DoCmd.Close I use the form as a class and create an instance manually (see the code in
Dialog.Richbox). I keep this instance alive until I got the form’s result back.
If you are interested in knowing how the form is made modal, this is the code in
FormDialog.ShowModal() what keeps the form open until the user clicks a button:
Sleep() function is a Win32 API that stops the current process for the given number of milliseconds. This in effects hands back the control to the Operating System for a short time. That way the system is still responsive and does not consume resources when it’s just waiting for user input.
You can download a sample database containing all the necessary code as well as a number of tests.
This version only contains the database in the new Microsoft Access accdb format.
Download the EnhancedMsgBox1.4.zip (142KB), v1.4 containing the ACCDB database.
It’s been a while, but at last some improvements and bug fixes!
- As per Julie B’s comment, updated code to properly display the dialog on the proper monitor in multi-monitor environments.
The dialog box will open in front of the Access window that currently has focus (assumed to be the one that opened the dialog), so if your application has forms on different monitors, the dialog should open on the right one. If we can’t determine the active window, the dialog box will open in the middle of the monitor containing the main Access application window.
- Implemented Kristjan’s suggestion regarding the use of custom button labels. See updated description above.
- Corrected background colours for the dialog box so they correctly match the MS Office theme.
- Corrected a bug in the code that decided of the correct sound to play.
Thanks to Henry of Access-Pro.de for proposing a correction to the default buttons behaviour.
- Updated behaviour for the default buttons. They are now focused in a way that matches that of the standard msgbox.
- Reversed the naming of the buttons on the form to make it a bit more consistent with the standard box.
Thanks to Andy Colonna (http://www.pcesoft.com) for uncovering the following bugs (check out his free Spell Checker with source code!):
- Corrected bug in
Form_FormDialog.FilenameSanitize() function that would
fail to remove all invalid characters for a file name.
- File name for the saved text message will be truncated to first 32 characters
of message box title in
- Changed the use of
ShellExecute to avoid security warning
in some instances in
- Corrected twips to pixel conversion bug in
would result in an improperly sized dialog box when the text message was too
- Corrected code for
DefaultButtonDelay (thanks to Geoffrey) (was referencing
wrong variable, causing self-referencing code).
- Corrected code for
RichBox to take the
DefaultSavedTextFileFolder into account (the path was previously not passed onto the dialog boxes and
the text file would always be created in the application folder instead of
the one specified by
- Added license notice at top of source code.
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
Free for re-use in any application or tutorial providing clear credit is made about the origin of the code and a link to this site is prominently displayed where end-users can easily access it.
May 20th, 2008
Microsoft SQL Server comes in many editions, ranging from completely free to use and distribute to versions costing tens of thousands of dollars.
For small businesses, or when you can live with the limits imposed, the Express edition is one option to consider.
Here are some reasons why SQL Server Express may be a good choice:
You’re upsizing an Access database
SQL Server is the natural extension of upsizing an existing Access database. It work automagically with minimum effort providing that you followed some simple good-design rules from the start.
You’re future-proofing your needs
Because SQL Server comes in many flavours, you know you -or your customers- can upgrade to a more capable (albeit more expensive) version in the future if needed.
As usual with a lot of Microsoft development tools, SQL Server will happily let you shoot yourself in the foot by providing you with a fairly easy way to treat your database as a complete development platform.
It’s good in the sense that you have interesting tools and capabilities included in the server, and it’s bad for the exact same reasons.
I tend to prefer database servers to be just that: data repositories, and I’m not too fond of relying on specific, non-standard features of a particular database system, but what do I know.
Excellent out-of-the-box development support
Deep integration with .Net and Visual Studio, without any effort, Microsoft saw to that of course.
In some cases, such as LINQ to SQL, it’s almost the only real choice, although the other database vendors are working hard at the necessary providers, so that lead should be short-lived.
There is something to be said about developer productivity: you have to give credit to Microsoft for making their tools well integrated and usable from each-other. What it means is that for small developer shops there is much to gain in surrendering to this “ease of the default”.
Of course, it’s a double-edged sword, but having a complete development infrastructure work out of the box is certainly a big help, and if you don’t like it, you’re still free to chose something else.
Lots of tools
With SQL Server Advanced Services, you also get Server Management and Reporting Services. These are great tools made available for free.
The only missing one for SQL Server is the Reporting designer. While the reporting service means that you can use existing reports, only SQL Server Standard and Enterprise have it.
There is an option for developers though: the (nearly free) SQL Server Developer edition is in fact the same as SQL Server Enterprise, without the license to use in non-developer or tester environment. This means that as a developer, you can create and distribute your reports to be used by your customers who will be using SQL Server Express.
Did I mention it’s free?
All this is free, as in beer, not as in liberty though.
For commercial applications targeted at small businesses, SQL Server Express is a really good choice: you can distribute it without problem, the customer gets all the tools, can easily find outside support, and they can always migrate to a more beefy version if their needs grow, all that without having to depend on you.
So it sort of offers customers a kind of freedom that they wouldn’t have with other choices.
Of course you can get that with other database systems, although you have to be careful which Open Source one you choose: I recently decided not to use MySQL any longer for the simple reason that it’s too expensive and restrictive in a business environment, at least for the kind of work I do.
Why would you not want to use SQL Server Express?
You don’t want to depend on Microsoft
That can be a good reason enough sometimes. There is nothing preventing Microsoft from crippling SQL Server Express in the future to force users to move to a paying version early.
I suppose that whatever database system you use, even Open Source ones, there is always the possibility that the company supporting its development goes bankrupt, the Open Source projects goes dead or decides to go in a direction that doesn’t suit you..
It’s only supported on Microsoft OS
True, and that’s a good reason to chose something else.
There is a hidden cost in SQL Server Express: it needs to run on a Windows machine, and that’s not free, although SQL Server will work on older Windows 2000 machines and Windows XP which are arguably not expensive.
Your database needs will exceed SQL Server Express specifications
If you think any of your databases will grow beyond 4GB or that it will get busy and you need all the RAM and CPU you can get, then SQL Server Express is probably not for you as it will only use 1 CPU and 1GB of RAM at most.
If your needs go beyond that, then you’ll have to move to a paying version.
Upgrading can be expensive
It’s true that moving to the next cheapest upgrade of SQL Server Workgroup will cost you about US$700 for a 5 user license. The limits imposed on the database are much higher (2 processors, 3GB RAM and no size limit) but if you need more clients / or higher limits, then the expense will grow quite fast, and you’ll have to manage those hateful client access licenses.
Your needs are more modest
We haven’t talked here about single-file/single-user database systems.
These databases don’t user resident services and are usually meant for more limited needs, sometimes allowing only a single user to be connected.
The footprint of these non-server databases is a lot smaller, typically only requiring a single dll or a handful of files to be installed.
They are extremely useful for desktop application that do not really require multi-user support or advanced security features.
Here again, Microsoft offers SQL Server Compact, which, despite the name, doesn’t have much to do with the other SQL Server editions. This one is also free, but has a limited feature set and only allow single-user access as it is meant to be a lightweight database and works well in limited memory environments such as those found on mobile devices.
Of course, here again there is a lot of competition: Thunderbird, SQLite, MS Access and VistaDB (for embedding into .Net applications, not free) to name a few.
These are pretty good times when it comes to databases: we get more choices now than we ever had.
As usual, choosing a database as a back-end for your products isn’t easy: you need to consider cost, licensing, support and the future.
There isn’t a single database system that will meet everyone’s needs for all types of use, so choose carefully.
SQL Server Express is a very good contender in that market. It should not be dismissed out of hand because it’s from Microsoft, in the same way that PostgreSQL shouldn’t be dismissed because it’s Open Source.
Just use the tool that best answers your needs for your particular circumstances.
January 30th, 2008
Reflection is a hugely useful technology: it allows you to get inside objects and get their intimate details, modify their values and even rewrite part of their code.
Today I wanted to build a simple treeview control that would display the hierarchical structure of an arbitrary object by going through its properties and building a tree-like structure. Useful to visualise the internals of an object and see changes at runtime.
Using PropertyInfo you get enough information about a particular property to decide if you want to display it or recurse into it if it’s a complex type that contain other properties.
The only big issue I faced was when dealing with indexers.
The issue with Indexers
Indexers are defined in code like that:
We’ve defined an overloaded indexer: one takes a
DateTime, the other just a string representation of a date.
We could use them like that for instance:
Now, let’s say we want to print all property names and their values for an arbitrary object.
Let’s try on our
So, that part is easy: we iterate through all properties of
myDay and print their
Name and they value.
Problem is, when we reach the indexer property, we get a
TargetParameterCountException on line 8.
We need to detect the indexer before attempting to get its value.
To do that, we need to modify our
foreach loop to:
Here we attempt to get the indexer parameters for every property. If we’re dealing with an indexer, the
parcoll array will contain a list of
ParameterInfo related to that particular indexer.
So what really is this
parcoll and its
ParameterInfo contain information about the parameter of an indexer property.
parcoll array will contain one entry for each overloaded indexer.
In our concrete example this is what we get:
pi.Name will be Items, this is standard for all indexer properties.
pi.PropertyType is the type of the returned class of our indexer, in our case, a System.String.
parcoll.Length will be equal to 2 because we have 2 overloaded indexers.
parcoll.Name will be date.
parcoll.Name will be datestr.
parcoll.ParameterType will be System.DateTime.
parcoll.Member are in fact references to
That’s a lot of nice information about our indexer, but what if we want to get a value from it?
Consider the definition for
That second parameter is used to get a value from the indexer.
So, how do we use it?
If you know valid keys that you can feed to the indexer, then you just do that (assuming that
pi is the
PropertyInfo for the indexer).
Or, if you prefer to use a
DateTime:But what if you don’t know anything about the values contained by the indexer?
Well, unless you use attributes to decorate the indexer or the underlying storage field with specific custom information, you’re basically screwed.
To understand why, just consider how the indexer is implemented:
When you pass it a
DateTime or a
string as a parameter, it just passes it on to the getter/setter as the
What is done with that is up to the implementer.
If you want to maintain a collection of some kind, then you need to use private fields to hold the keys and their values.
.Net doesn’t store the values passed to the indexer or the values returned by it anywhere.
Consider how .Net doesn’t give you any means of iterating through the keys and values of an indexer.
You can’t do a
for(;;) or a
foreach() loop, you can’t even know how many items are maintained through the indexer by directly questioning it.
So, it certainly is normal that you cannot get anything out of the indexer without knowing exactly how to access it.
The Observer Effect
In many fields of science, the act of measuring something, whether it’s an electronic circuit or a psychological experiment, can actually influence the observed phenomenon.
This is unsurprisingly called the Observer Effect.
In .Net, Reflecting on object can be very much the same.
Consider the use of Reflection when it comes to getting information about an object:
You use metadata describing the object to get more meta data information about its members
You can use that meta data to direcly query the state of an object.
If you limit yourself to getting metada, then no harm is done: you’re not querying the object instance itself but the metadata about its class.
If you start querying an actual object instance for its values then you are actively interacting with it.
In most cases, if you’re just getting the value of Fields or Properties defined as follow, then it’s fine:
Using reflection, we can get the values of both
Meeting without any side-effect to the object.
Now consider this:
Now what we’ve got here is a case of lazy loading or lazy initialization.
The consequence is that by querying the Collection property we’re actually creating it, with a potential for a lot of side-effects, like the change of other properties, here maybe related to opening the database connection for instance.
So, when getting values of the properties of an object instance, we can actually completely change its state.
Whether this is a concern or not really depends on what you’re trying to achieve: you may not be able to achieve being a passive observer using Reflection unless you know about the construction of the particular class you’re dealing with.
Using Attributes, you could give hints about which Property not to query and which Field to get instead. In our case, we could add a custom attribute like
[ImplementedBy("_collection")] to our
Collection property for instance.
If you want to do something generic, then you’re out of luck it and have to deal with that fact.
One possible way to minimize the effect would be to not query Properties that only have a getter and no setter, but that’s not a guarantee that all properties that implement a getter/setter are going to play nice.
Then there is also the -probably rarer- case of the Property that only has a setter.
Then there is also the case of the property getter that has some in-build security and will return different things depending on who’s calling…
All this seems pretty obvious once you understand the whole picture but it’s easy to get so impressed and dazzled by the power of Reflection that you don’t realise its natural limits.
June 13th, 2006