How do i fix Outlook VBA Compile Error Type Mismatch

rob l

I'm getting a Compile Error: Type Mismatch when i execute macro to open an non-default mail folder. It occurs at Set oMailBox

Sub openSxxInbox()

Dim oOutlook As Outlook.Application
Dim oFolder As Outlook.Folder
Dim oMailBox As Outlook.Folder
Dim oFldr As Outlook.Folder

Set oOutlook = CreateObject("Outlook.Application")
Set oNS = oOutlook.GetNamespace("MAPI")
Set oMailBox = "Sxx"
Set oFldr = "Inbox"

oNS.Logon 'does not do anything if Outlook is already running

Set oFolder = oNS.Folders(oMailBox).Folders(oFldr)

    If (oOutlook.ActiveExplorer Is Nothing) Then
        oFolder.Display
        Else
        Set oOutlook.ActiveExplorer = oFolder
    End If

End Sub
Eugene Astafiev

In the code you may find the following line:

Dim oMailBox As Outlook.Folder

and later you are trying assign a string value which is not correct:

Set oMailBox = "Sxx"
Set oFldr = "Inbox"

There is no direct cast between folder objects and strings. You can use the NameSpace.GetDefaultFolder method which returns a Folder object that represents the default folder of the requested type for the current profile; for example, obtains the default Inbox folder for the user who is currently logged on.

Note, you may find the Store.GetDefaultFolder method helpful which returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.

Also you can define the object as string in the code:

Sub openSxxInbox()

Dim oOutlook As Outlook.Application
Dim oFolder As Outlook.Folder
Dim oMailBox As string
Dim oFldr As string

Set oOutlook = CreateObject("Outlook.Application")
Set oNS = oOutlook.GetNamespace("MAPI")
oMailBox = "Sxx"
oFldr = "Inbox"

oNS.Logon 'does not do anything if Outlook is already running

Set oFolder = oNS.Folders(oMailBox).Folders(oFldr)

    If (oOutlook.ActiveExplorer Is Nothing) Then
        oFolder.Display
        Else
        Set oOutlook.ActiveExplorer = oFolder
    End If

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how do I fix the type mismatch issue?

What is "Type mismatch" and how do I fix it?

How to fix Compile Error: User-defined type not defined when using Excel VBA from Outlook?

How do I fix the Visual Studio compile error, "mismatch between processor architecture"?

How to fix vba type mismatch?

How do I resolve this type mismatch error?

How do I fix this compile error - Expected end of statement? Inputting a formula into a cell with VBA that passes a VBA variable into that formula

How do I fix this data type error?

How to fix this type mismatch error in Scala?

How to fix this type mismatch error? (TypeScript)

How do I fix the Typescript error Unable to compile TypeScript:

VBA compile error due to "ByRef argument type mismatch"

VBA Excel Fails to run because of Compile Error: Type Mismatch

How to fix a VBA "type mismatch" error after switching to 64-bit Excel

type mismatch vba error

VBA 'User Defined Type Not Defined' Compile Error with Outlook

How do I fix a "Vue packages version mismatch" error on Laravel Spark v4.0.9?

How do I fix a "Vue packages version mismatch" error on Vue js 3

Compile error: ByRef arugment type mismatch: How to call a value in an array

How do I fix TypeError: unhashable type: 'list' Error

How do I fix this type of error System.Diagnostics.DebuggerStepThrough()

How do I fix JMeterPluginsCMD "Missing plugin type specification" error

How do I fix Error: adder_out is an unknown type

how do i fix error type object() takes no parameters

How do I fix Parser Error Message: Could not load type?

Error: "Type mismatch. Required: Observer<PagedList<Message!>!>". How to fix?

Error: "Type mismatch. Required: Observer<PagedList<MyItem!>!>". How to fix?

How to fix run time error 13 type mismatch

VBA script , how can i write this code better? Run time error 13 , Type mismatch