473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Late Bind MSO

zmbd
5,501 Expert Mod 4TB
Ok,
I’ve an old version of a nice little file dialog that works well; however, it was developed using the “Microsoft Office 10 Objects Library” reference using early binding…
Now each time we’ve upgraded, for some reason I’ve had to go in to the database and make sure that the reference is set… (last time I used Microsoft Office 11 Objects Library)
The real pain is that with every new database I create and import this code into, I have to go back and make sure that there is a reference to the MSO…

Then a 3W Bulb went off, I thought… If I can late bind Excel, Word, VBScript, etc… and the code already works, then let’s late bind this monster too!!!

Simple, or so I thought… I have tried:
Expand|Select|Wrap|Line Numbers
  1. <doesn’t work>
  2. Dim objFD as Object
  3. Set objFD = CreateObject(“Office.Application”)
  4.  
  5. <doesn’t work>
  6. Dim objFD as Object
  7. Set objFD = CreateObject(“Office.FileDialog”)
  8.  
  9. <doesn’t work>
  10. Dim objFD as Object
  11. Set objFD = CreateObject(“MSO.Application”)
Puzzled, I turned to the books… no luck there…. So I’ve tried the normal Google and Bing and Yahoo and you name it searches… tried the three dozen forums I’ve read for years… seen a lot of the same question… and yet, no answer
How do we late bind the "Microsoft Office ## Object Library" so that vba module uses the latest version as we can simularly do with Excel/Word/Outlook/VBscript?

-z
Aug 16 '12 #1

✓ answered by Stewart Ross

You don't need to bind an instance of an object to MSO to use its services at all, Z. Some of MSO's methods are common across the Office applications, and where applicable have already been instantiated by the application itself. They are available to you explicitly through the Application object, as you have found yourself in post #5, so this is not about early or late binding of the MSO library as such.

What setting an explicit reference to the MSO library does is to allow you to use intellisense whilst developing, and it provides access to the values of the mso constants such as msoFileDialogViewDetails defined by the DLL. It makes no difference to your ability to use the methods concerned at all.

As you found for yourself in post #5, you can use the FileDialog method in your code without setting a reference to the MSO object, as long as you substitute or define actual values for the mso constants.

For example, the following function returns the paths of files selected by the user with no reference set to MSO in the calling application (which I've tested in both Excel and Access):

Expand|Select|Wrap|Line Numbers
  1. Public Function fFileDialog(Optional Filename = "", Optional FolderOnly As Boolean = False)
  2.  
  3. 'Does not require reference to Microsoft Office Object Library MSO.DLL
  4.  
  5. Const msoFileDialogFolderPicker = 4
  6. Const msoFileDialogFilePicker = 3
  7. Const msoFileDialogViewDetails = 2
  8.  
  9. Dim fd As Object
  10.  
  11. Dim varItems As Variant
  12. Dim flPath As Variant
  13. If FolderOnly Then
  14.     Set fd = Application.FileDialog(msoFileDialogFolderPicker)
  15. Else
  16.     Set fd = Application.FileDialog(msoFileDialogFilePicker)
  17. End If
  18.  
  19. With fd
  20.     .AllowMultiSelect = True
  21.     .InitialView = msoFileDialogViewDetails
  22.     .InitialFileName = Filename
  23.     If .Show = True Then
  24.         For Each varItems In .SelectedItems
  25.             flPath = varItems
  26.         Next
  27.     End If
  28. End With
  29.  
To sum up, the file dialog method provided by the MSO DLL is already instantiated and available through the Application object. It is not necessary to bind the MSO library itself to your code in order to use it (regardless of early or late binding) - but adding a reference for early binding allows the VBA editor to assist you with intellisense and gives you access to all the pre-defined constants.

The advantage of NOT specifying a reference to the MSO object is that your file dialog code will work across multiple Office versions with no reference changes required, as long as you define the constants you use.

-Stewart

6 12516
ariful alam
185 100+
please, have a look @ this -> http://www.tech-archive.net/Archive/...4-07/0359.html

hope works for you. :)
Aug 16 '12 #2
zmbd
5,501 Expert Mod 4TB
Ariful Alam, unfortunately this is not an issue with “Word Object Library” late binding. There is no issue with being able to late bind to the other office applications such as Word, Excel, Outlook, etc… nor is there an issue with VBScript.

What I cannot do is late bind the Microsoft Office Object ## Library that would find located at:
C:\Program Files\Common Files\Microsoft Shared\OFFICE14\MSO.DLL
In a typical MSOffice2010 local PC installation.
(in 2003 I believe the subfolder was OFFICE11)

What I need is the late bind to the MSO.DLL
-z
Aug 16 '12 #3
Rabbit
12,516 Expert Mod 8TB
Each one of these can potentially be used to create a file dialog.
Expand|Select|Wrap|Line Numbers
  1. UserAccounts.CommonDialog
  2. MSComDlg.CommonDialog
  3. MSComDlg.CommonDialog.1
  4. Word.Application
  5. SAFRCFileDlg.FileOpen
  6. InternetExplorer.Application
Oddly enough, I find that the most reliable one amongst the different versions of windows is the internet explorer object.
Aug 16 '12 #4
zmbd
5,501 Expert Mod 4TB
So this is what I've been able to do as P.O.C.
Expand|Select|Wrap|Line Numbers
  1. Function Z_File_Dialog()
  2. '
  3. 'Pulled these here for latter use to compare against the 
  4. 'code I am currently useing.
  5. '
  6. Const msoFileDialogFilePicker As Integer = 3
  7. Const msoFileDialogFolderPicker As Integer = 4
  8. Const msoFileDialogOpen As Integer = 1
  9. Const msoFileDialogSaveAs As Integer = 2
  10. '
  11. '
  12. Dim objFileOpen As Object
  13. Dim varSelectedItem As Variant
  14. '
  15. 'assign the object
  16. Set objFileOpen = Application.FileDialog(msoFileDialogFilePicker)
  17. '
  18. 'set the options
  19. objFileOpen.AllowMultiSelect = False
  20. '
  21. 'show the form
  22. objFileOpen.Show
  23. '
  24. 'now in this case I'm using the file picker so let's see if
  25. 'a file was returned and return a false if not
  26. If objFileOpen.SelectedItems.Count > 0 Then
  27.     For Each varSelectedItem In objFileOpen.SelectedItems
  28.         Z_File_Dialog = varSelectedItem
  29.     Next varSelectedItem
  30. Else
  31.     findthefile = False
  32. End If
  33. '
  34. 'cleanup
  35. Set objFileOpen = Nothing
  36. End Function
Works without reference to the object libraries as is required in my current code. Still an aggravation that using the "Office" name doesn't work as expected (that is the name that VBA returns from the reference collection when the MSO is selected) so the codeing has to change a tad from early binding (which makes the intelisense available while in development) as the are some differences in what is available for properties.

Still looking for a way to late bind the MSO

-z


-z
Aug 17 '12 #5
Stewart Ross
2,545 Expert Mod 2GB
You don't need to bind an instance of an object to MSO to use its services at all, Z. Some of MSO's methods are common across the Office applications, and where applicable have already been instantiated by the application itself. They are available to you explicitly through the Application object, as you have found yourself in post #5, so this is not about early or late binding of the MSO library as such.

What setting an explicit reference to the MSO library does is to allow you to use intellisense whilst developing, and it provides access to the values of the mso constants such as msoFileDialogViewDetails defined by the DLL. It makes no difference to your ability to use the methods concerned at all.

As you found for yourself in post #5, you can use the FileDialog method in your code without setting a reference to the MSO object, as long as you substitute or define actual values for the mso constants.

For example, the following function returns the paths of files selected by the user with no reference set to MSO in the calling application (which I've tested in both Excel and Access):

Expand|Select|Wrap|Line Numbers
  1. Public Function fFileDialog(Optional Filename = "", Optional FolderOnly As Boolean = False)
  2.  
  3. 'Does not require reference to Microsoft Office Object Library MSO.DLL
  4.  
  5. Const msoFileDialogFolderPicker = 4
  6. Const msoFileDialogFilePicker = 3
  7. Const msoFileDialogViewDetails = 2
  8.  
  9. Dim fd As Object
  10.  
  11. Dim varItems As Variant
  12. Dim flPath As Variant
  13. If FolderOnly Then
  14.     Set fd = Application.FileDialog(msoFileDialogFolderPicker)
  15. Else
  16.     Set fd = Application.FileDialog(msoFileDialogFilePicker)
  17. End If
  18.  
  19. With fd
  20.     .AllowMultiSelect = True
  21.     .InitialView = msoFileDialogViewDetails
  22.     .InitialFileName = Filename
  23.     If .Show = True Then
  24.         For Each varItems In .SelectedItems
  25.             flPath = varItems
  26.         Next
  27.     End If
  28. End With
  29.  
To sum up, the file dialog method provided by the MSO DLL is already instantiated and available through the Application object. It is not necessary to bind the MSO library itself to your code in order to use it (regardless of early or late binding) - but adding a reference for early binding allows the VBA editor to assist you with intellisense and gives you access to all the pre-defined constants.

The advantage of NOT specifying a reference to the MSO object is that your file dialog code will work across multiple Office versions with no reference changes required, as long as you define the constants you use.

-Stewart
Aug 18 '12 #6
zmbd
5,501 Expert Mod 4TB
Stewart,
This is as I was begining to suspect.
It is really very nice to confirm this information. I really did read over 100 different threads thruout the web without a single person, even the MVPs and other MS people, ever making this one statement very clear!
It would be nice if the MSO would follow the same conventions as the other references!

I may clean my code up a tad and make a nice generic now that I have this information!!

Thnx
-z
Aug 18 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Zlatko Matić | last post by:
I was reading about late binding, but I'm not completely sure what is to be done in order to adjust code to late binding... For example, I'm not sure if this is correct: early binding: Dim ws...
5
by: Chris | last post by:
Greetings, I'm trying to late bind a ActiveX DLL that was made with VB6 in C#. Can you give me an example or URL of this? I've been trying to google it, just cant find a straight answer. Thanks!
1
by: Jim | last post by:
Hey all I'm trying to late bind a VB6 object in Component Services using c#. I've been able to do tons of late binding, but now that I have a com+ object that doesn't have a method exposed...
0
by: Xavier MT | last post by:
Hi, I have a small problem I solve easily in VB.NET but now I need to implement it in C#. The problem is: I have a COM component written in C++ and the methods are like this: ...
2
by: C# Newbie | last post by:
Hi, I have a Class1.cs and a Main executable file. The main executable codes look like this: Assembly asm = Assembly.LoadFile(@"C:\ClassLibrary1.dll"); Type typ =...
2
by: Chris | last post by:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim AESobj As Object Dim boolResult As Boolean Const HKEY_CURRENT_USER As Integer =...
3
by: Dick Sutton | last post by:
Hi all, I just made the mistake of posting this question in the Microsoft.Public.VB.General.Discussion group. I was told that VB 2005 Express is NOT VB. Oh, well, must have touched a nerve. ...
0
by: Elan | last post by:
I Created Object A in the Object B. then I called a object A function from object B... Like.. Public Class A Public Event beforeSample() Public Sub function_Sample()
1
by: Elankathir S.N. | last post by:
I Created Object A in the Object B. then I called a function of object A from object B… Like.. Public Class A Public Event beforeSample() Public Sub function_Sample()
14
by: Siv | last post by:
hi, I am converting an application that writes to an Excel spreadsheet and the code trips the "option Strict" that I would like on because the parser says "option Strict On disallows late...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.