Q Our project directory structure includes folders for office documents, such as spreadsheets and text files, in addition to the usual MicroStation® files.
A It makes sense to keep related project files in a consistent directory structure. The problem of keeping office documents in a project folder is that the owning application doesn't recognise your folder structure.
Microsoft Office® is almost ubiquitous.
Unlike MicroStation's flexible configuration capabilities, Office applications recognise only one folder per Windows user account.
That is to say, you can't configure Microsoft Word™ to look automatically in your project-specific directories.
Each time a user wants to look at a spreadsheet, she has to navigate to your project folder and double-click the
or navigate using the spreadsheet's File Open dialog.
What if you could browse your office files while using MicroStation, and open a file without wasting time browsing? This article proposes a solution that does exactly that.
What if you could browse for any file using MicroStation, and open that file without wasting time browsing? This article proposes a solution that does exactly that.
The solution provided in this article relies on Windows File Associations. When you install an application in Windows, that application registers its interest in a particular file extension. You're probably familiar with this, but as a reminder, here are some common assocations …
|Bentley MicroStation|| |
|Microsoft Word|| |
|Microsoft Excel|| |
|Adobe Portable Document Format|
The consequence of those associations is simple and intuitive.
If, while navigating your Windows folders with Windows Explorer,
you double-click a
.doc file, Windows starts Word and Word opens the file you indicated.
Because Windows connects any file extension to its application,
the solution proposed here is not restricted to any particular file type.
It works with any registered file extension.
Suppose you have an AVI file, and that movie is associated with Windows Media Player.
When you choose a
.avi file, then we fire up the Media Player and tell it to play that movie.
As a programmer, you have access to Windows file associations through the
That API provides hundreds of functions related to file and folder management.
Many of those fall into the class of shell extensions.
There's a shell extension whose sole duty is to open a file with its associated application.
The link at the end of this article provides a freely downloadable VB
project that wraps the Win32
Here's its C language function prototype …
HINSTANCE ShellExecute (HWND hwnd, LPCTSTR lpOperation, LPCTSTR lpFile, LPCTSTR lpParameters, LPCTSTR lpDirectory, INT nShowCmd );
The number of arguments and their options provides a bewildering number of ways that you might invoke that function.
We've simplified things for you, and wrapped it inside VBA wrapper.
The VBA example code shows how to use
The example project
ShellExecute2 provides two ways to open a document …
vba run [FileViewer2]modMain.OpenDoc "C:\My Docs\Example.docx"
UserFormthat searches project folders
We assume that you have a standard directory structure for project files. You may have a specific folder per project where office documents are located.
It will be useful to define MicroStation configuration variables (CfgVars) that point to your project root and to specific folders in your project. For example …
PROJECT_ROOT = V:/Projects/FlexiTable/ OFFICE_FOLDER = $(PROJECT_ROOT)Office Files/
As you probably know, MicroStation VBA has methods to get the value of a CfgVar from the
We can find the office folder programmatically like this …
Dim path As String path = ActiveWorkspace.ConfigurationVariableValue("OFFICE_FOLDER")
Now you need to be able to open a file in that
Office Files folder.
MicroStation doesn't know how, but Windows does.
Windows knows that a given file extension is associated with an application.
For example, a
.dgn file is associated with MicroStation,
.doc file is associated with Microsoft Word,
.xls file is associated with Microsoft Excel.
The Windows API is a little tricky to use from VB/VBA, so we've wrapped the functionality in a VBA wrapper
The example VBA project lets you open any document using a keyin …
vba run [FileViewer2]modMain.OpenDoc C:\MyDocs\Example.docx
Beware spaces! If a document path contains one or more spaces then you must enclose the path in double-quotes …
vba run [FileViewer2]modMain.OpenDoc "C:\My Documents\Example.docx"
Here's the procedure that interprets your keyin and invokes
Public Sub OpenDoc() Dim docName As String ' Get the user keyin argument string and remove quotes if necessary docName = RemoveQuotes(KeyinArguments) If OpenDocWithApplication(docName) Then Debug.Print "Opened '" & docName & "' successfully" Else Debug.Print "Failed to open '" & docName & "'" End If End Sub
Here's the procedure that wraps
Public Function OpenDocWithApplication(ByVal docName As String) As Boolean OpenDocWithApplication = False Const VERB_OPEN As String = "open" Const SW_SHOWNORMAL As Long = 1 Dim r As Long r = ShellExecuteWin32(GetDesktopWindow(), VERB_OPEN, docName, 0, 0, SW_SHOWNORMAL) OpenDocWithApplication = ExplainShellExecuteReturnCode(r) End Function
Putting it all together, here's an example VBA application to show an office document.
The document is in a folder specified by the MicroStation CfgVar
The document name is
Excel Workbook.xls, and when your user presses the View button,
Excel starts and opens the file.
The Open Office Document Example is provided in a ZIP archive.
There's a VBA project, a VB project, and a DLL.
Copy the VBA project to a well-known folder (i.e. one specified in your
Once you've copied the files, you can open or run the example VBA project. Use this keyin …
vba run [FileViewer2]modMain.Main
You'll see the example dialog appear. Type a valid CfgVar into the upper text box, and a file name including its extension into the lower text box. Press the View button, and watch an office application magically start with your document.
Post questions about MicroStation programming to the MicroStation Programming Forum.