|
Microsoft Office Application Development
|
|
Creating an addin from an Excel macro. Introduction 1 2 3 4 5 6 7 8 9 10 Conclusion Use Different User Interface LanguagesFor many utilities an ability to change the user interface language can be very convenient for the user. The consequence is that all texts shown to the user (including captions and tooltips on userform controls) have to be removed from the code and placed elsewhere. There are different ways the translations can be stored: Inside A TextfileAdvantage: if the syntax and ordering of the texts is chosen properly it is relatively easy for a user to add his own language. Disadvantage: If the user makes a mistake, the application may read the texts in an unexpected order and the application's messages may become nonsensical. On A Worksheet In The Add-inAdvantages: Editing is easy because all of Excel's editing facilities are available. The languages can be placed next to each other for easy reference and for quick checking. Disadvantage: New languages cannot be added by the user, unless special measures are included, like a language import macro. When using a worksheet, this could be a useful layout:
Column A contains a description of the location for which the translations are used. Column B denotes the object, column C the property and finally from column D and on, the translations themselves. The VBA Code that is used to read the translations uses a set of defined
names, which each point at the upper left-hand corner of the translations
belonging to a location. The name “Userform1” (cell D2) points at the start of the translations needed for the userform controls of Userform1. First the code offsets to the column belonging to the currently selected language. Then all cells are read downwards, untill an empty cell is found (denoting the end of the translations belonging to this object). Similarly the name “MsgBoxes” is used to read the translations for all message boxes. The Translations reading code is shown in lising 7: Listing 7 Option Explicit Every time the user selects a new language, the sub ReadMessages has to be run again to ensure the proper language is used in the code. Of course the userform also needs code to update its controls (sub placed in the code module behind the form): Sub SetTexts()
When the text needs to be interspersed with variable information a possible problem arises. The position of the variable text may not be the same in different languages. The solution chosen here is to use keywords which are going to be replaced with the relevant variable contents in a special function in the code: "You have chosen _ARG1_ for your userinterface language." Unfortunately this function will not work in Excel 97, due to the use of the "Replace" function. Listing 8 Function ReworkMsg(ByVal sMsg As String, _ This function has 5 optional arguments. So in order to retrieve the sentence shown earlier, the function can be called as follows: Sub Example() The result of running this example is shown below:
So the example above replaces “_ARG1_” with "English". By inserting the codeword "_NEWLINE_", a carriage return can be inserted in the text, e.g.: Sub Example() The result of running this example is shown here:
| ||||||||||||||
Use the contact page to issue
questions or comments about this website. |