Set Object Variable inside an If/ElseIf Statement (Run-time error 5)

xenedra13

Board Regular
Joined
Aug 15, 2014
Messages
58
This macro keeps giving me a run-time error '5'.
Code:
Dim myOlApp As Object
    Dim MyItem As Object
    Set myOlApp = CreateObject("Outlook.Application")
    
    If Range("C6").Value = "June" Then
        MyItem = myOlApp.CreateItemFromTemplate(JuneTemplate)                 'THE ERROR HIGHLIGHTS THIS LINE
    ElseIf Range("C6").Value = "July" Then
        MyItem = myOlApp.CreateItemFromTemplate(JulyTemplate)
    End If
I'm assuming that it isn't possible to set an object equal to something inside an if statement. Is this true? And is there any way around this problem?
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,497
Office Version
2010
Platform
Windows
This macro keeps giving me a run-time error '5'.
Code:
Dim myOlApp As Object
    Dim MyItem As Object
    Set myOlApp = CreateObject("Outlook.Application")
    
    If Range("C6").Value = "June" Then
        [COLOR=#FF0000][B]Set [/B][/COLOR]MyItem = myOlApp.CreateItemFromTemplate(JuneTemplate)                 'THE ERROR HIGHLIGHTS THIS LINE
    ElseIf Range("C6").Value = "July" Then
        [COLOR=#FF0000][B]Set [/B][/COLOR][COLOR=#FF0000][/COLOR]MyItem = myOlApp.CreateItemFromTemplate(JulyTemplate)
    End If
I'm assuming that it isn't possible to set an object equal to something inside an if statement. Is this true? And is there any way around this problem?
You need to use the Set keyword (as shown above in red) when assigning a reference to an object.
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,497
Office Version
2010
Platform
Windows
I included the set keyword and I still got the same error.
It would have helped if you mentioned which error you got. Anyway, I don't use Outlook, so I have not direct knowledge of what you are trying to do. Based on your declaring MyItem as an object, the Set keyword is required, so that was a problem. I have to assume you know what you are doing and that CreateItemFromTemplate is a method of myOlApp variable that does, in fact, return an object. If so, then without knowing the specific error message you got, the only thing I can think of is that either JuneTemplate (or JulyTemplate) is a variable that needs to be assigned a string value of some sort OR it is supposed to be a string argument and is missing the surrounding quote marks.
 

xenedra13

Board Regular
Joined
Aug 15, 2014
Messages
58
The error was listed in the first post I made but I will be more specific. I on the same line I got a "Run-Time Error '5'" "Invalid procedure call or argument". Both JuneTemplate and JulyTemplate are assigned to strings in a separate sub that calls the sub in which this segment is included. June and July Templates are assigned to "C:\Filepath\Filename.oft" and refer to a saved template. The CreateItemFromTemplate is indeed a method that returns an object and this procedure has worked in other situations. The only thing different that I can think of that I have done in this procedure as compared to the others that have all worked perfectly is that it is within an if statement. Thank you for your help
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,497
Office Version
2010
Platform
Windows
I on the same line I got a "Run-Time Error '5'" "Invalid procedure call or argument". Both JuneTemplate and JulyTemplate are assigned to strings in a separate sub that calls the sub in which this segment is included.
When the separate sub calls this one, how are the JuneTemplate and JulyTemplate passed... as direct arguments or globally declared variables?
 

xenedra13

Board Regular
Joined
Aug 15, 2014
Messages
58
They were direct arguments. I just moved them to the declarations section and it worked! Thank you! I hadn't thought about that.
 

Forum statistics

Threads
1,081,530
Messages
5,359,347
Members
400,524
Latest member
Excelbat

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top