Get name of library for specific object vba - vba

I have looked everywhere and coulndt find a way to do this:
I have created an object with late binding as such:
set objNtWork = CreateObject("WScript.Network")
Now I want to add a reference in my project to the library.
How do I find the library name for that object?

Sub Alluding()
Dim o As Object
Set o = CreateObject("wscript.network")
Debug.Print TypeName(o)
End Sub
Returns
IWshNetwork2
So use as follows, Add the reference Windows Script Host Object Model:
Dim x As New WshNetwork
x.AddPrinterConnection "printername", "remotename"
So the following will show :
Sub Alluding()
Dim o As Object
Set o = CreateObject("wscript.network")
Debug.Print TypeName(o)
Dim w As New WshNetwork
Debug.Print TypeName(w)
End Sub
IWshNetwork2
IWshNetwork2
The same :)

Related

While accessing Outlook SubFolder in VBA Run time error -2147221233

I am trying to access the Folder from Outlook in VBA,some folder I am able to access but few folder are not. Although I am able to read 1 subfolder from the below code but when i am trying to change folder it is giving error.
This the code with which I am trying to access :
Public Sub ReadOutlookEmails()
Dim out_app As Outlook.Application
Dim get_name As Outlook.Namespace
Dim get_folder As Outlook.MAPIFolder
Dim oAccount As Object
Dim store_add As Object
Dim monthKeyValuePair As New Scripting.Dictionary
Dim email_list As New mscorlib.ArrayList
Dim date_List As New mscorlib.ArrayList
For Each c In Worksheets(ActiveSheet.Name).Range("D8:AH8")
date_List.Add c
'MsgBox c
Next c
Set out_app = New Outlook.Application
Set get_name = out_app.GetNamespace("MAPI")
For Each oAccount In out_app.Session.Accounts
If oAccount.SmtpAddress = "chhabranaveen#gmail.com" Then
Set store_add = oAccount.DeliveryStore
'MsgBox store_add.GetDefaultFolder(olFolderInbox).Folders("New Joinees")
'Set get_folder = store_add.GetDefaultFolder(olFolderInbox).Folders("On Bench Training")
Set get_folder = store_add.GetDefaultFolder(olFolderInbox)
Set get_folder = get_folder.Folders("On Bench Training")
Please help me here what I am doing wrong.
The error is MAPI_E_NOT_FOUND, which means the folder with the given name does not exist.
Make sure the folder named "On Bench Training" is really a subfolder of the Inbox.
Instead of getting the folder by its name you may try iterating over all subfolders and checking their name. So, basically instead of the following line:
Set get_folder = get_folder.Folders("On Bench Training")
You may iterate over all subfolder:
For Each uFolder In get_folder.Folders
If uFolder.Name = "On Bench Training" Then
MsgBox "Found!"
End If
Next uFolder

MS Access VBA Run-time Error '13' Type Mismatch when looking up folder/subfolder names

Here's the VBA code I'm using in MS Access (from Microsoft's support site - no edits):
**
Private Sub Command3_Click()
Dim fso As New FileSystemObject
Dim f As Folder, sf As Folder, path As String
'Initialize path.
path = Environ("windir")
'Get a reference to the Folder object.
Set f = fso.GetFolder(path)
'Iterate through subfolders.
For Each sf In f.SubFolders
Debug.Print sf.Name
Next
End Sub
**
I have also created the directory "C:\Test" and tried path = "C:\Test\" and path = "C:\Test"
I am aware that the reference "Microsoft Scripting Runtime" needs to be enabled - and it is.
The code compiles without error.
Despite the above, I'm still getting Run-time Error '13' Type Mismatch when I try to run it.
Is there anything else I'm doing wrong?
Try late binding
Private Sub Command3_Click()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim f As Object, sf As Object, path As String
'Initialize path.
path = Environ("windir")
'Get a reference to the Folder object.
Set f = fso.GetFolder(path)
'Iterate through subfolders.
For Each sf In f.SubFolders
Debug.Print sf.Name
Next
End Sub

Create Object for handling word on the fly (VBA)

I'm using VBA for creating an instance of word currently by adding a reference to the library, but this cause an issue, because there existing some machines without word.
This cause instantly a runtime error at startup on these machines. It's not possible to catch this error.
However, I try to create an object in VBA on the fly by something like this
Dim oWshShell As WshShell
Set oWshShell = New WshShell
' *** TEST REGESTRY
Dim tmp As String
tmp = oWshShell.RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\15.0\Word\Options\PROGRAMDIR")
Debug.Print tmp
tmp = tmp + "winword.exe"
Dim oWord As Object
Set oWord = Shell(tmp)
But my problem is that oWord is not an Object of Word.Application. So how to handle this?
Would be nice to get available all functionalities like with Word.Application.
You don't have to use a shell, use COM factory directly:
Function openWordApp() As Object
On Error Resume Next
Set openWordApp = CreateObject("Word.Application")
If openWordApp Is Nothing Then
msgBox "Word not installed on this machine"
Else
openWordApp.Visible = True
End If
End Function
In the caller, check if the returned value Is Nothing.

Global variable not seen by user form sub

I have set a global dictionary object to keep track of file paths (keys being the "project name" which is the first word of the filename). The way this program works is a form opens in which the user can then open a file dialog to add files to a listbox. When this is done it should add the name and path to the dictionary.
Primary code:
Public project_dict As Scripting.Dictionary
Public Sub excelToPPT()
Set project_dict = New Scripting.Dictionary
...
Dim u As UserForm1
Set u = New UserForm1
u.Show
...
End Sub
UserForm code:
Private Sub cmdAddProject_Click()
Dim project_fd As FileDialog
Set project_fd = Application.FileDialog(msoFileDialogFilePicker)
Dim it As Variant
Dim path_split() As String
Dim f_split() As String
Dim f As String
With project_fd
.AllowMultiSelect = True
.Title = "Select project excel file(s)"
.Filters.Clear
If .Show = -1 Then
For Each it In .SelectedItems
path_split = Split(it, "\")
f_split = Split(path_split(UBound(path_split)))
f = f_split(0)
' WHERE ERROR OCCURS
project_dict.Add f, it
Me.ListBox1.AddItem f
Next it
End If
End With
End Sub
When this is run the error is saying
project_dict.Add f, it
is no good because project_dict is not an object which I assume in this case is a scoping issue. Any ideas?
The issue was solved by placing the below properties in the UserForm code and passing the dictionary in to the form instead of using a global variable.
Dim project_dict As Scripting.Dictionary
Public Property Get ProjectDictionary() As Scripting.Dictionary
Set ProjectDictionary = project_dict
End Property
Public Property Let ProjectDictionary(ByVal pd As Scripting.Dictionary)
Set project_dict = pd
End Property

Can't get value from DatePicker

I have a DatePicker control on a worksheet. When I'm in the embedded code for the worksheet, I can access the control's value as follows:
Public Sub showValue()
Debug.Print Me.DTPicker21.value
End Sub
I would like to get the value from a module. Code here:
Sub getDate()
Dim obj As Object
Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets("Tool interface")
For Each obj In sht.OLEObjects
If obj.Name = "DTPicker21" Then
Debug.Print obj.value
End If
Next
End Sub
When I run this, the obj.value triggers this error:
Object doesn't support this property or method
I checked the list of properties for obj using this procedure, and there is no value property. How can I get the date value that's been set in the DatePicker?
I don't know all of the details, but some of the OLEObjects require that you first access their Object property, then you can access other properties. I think the OLEObject serves as a container, then the "sub-object" is the actual object with which you want to interact. For example, if you run the following two lines of code, you will see that the first returns OleObject and the second returns DTPicker:
Debug.Print "Obj: " & TypeName(obj)
Debug.Print "Obj.Object: " & TypeName(obj.Object)
In your case, try the following code change to remove the error(note the Debug line):
Sub getDate()
Dim obj As Object
Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets("Tool interface")
For Each obj In sht.OLEObjects
If obj.Name = "DTPicker21" Then
Debug.Print obj.Object.Value
End If
Next
End Sub