Convert VBA Code To Visual Studio VB 2008

I have created some VBA code that takes the subject of an email in outlook and sticks the date of the email in subject in front of it and saves it.

I cant seem to get the code to work in Visual Studio because I want to create an addin so that it creates a button for all of our users on the system to just push it and rename the subjects.

It only renames the subjects that are highlighted.

I think it has something to do with there not being a Set command in Visual Studio, because when I type that it just disappears.

Also I know some of the code doesnt need to be in there but I know it works in VBA before I go removing the code I want to get it working in VS.

If someone could help me with this it would be appreciated.
Sub FileManuscript()
    Dim objFolder As Outlook.MAPIFolder, _
        objMessage As Outlook.MailItem
    Dim answer, newDate, spot1, spot2, spot3, strNewSubject As String
        Dim outlookObj As Outlook.Application
    Dim selectionObj As Selection
    Dim itemCount As Integer
    Set outlookObj = CreateObject("Outlook.Application")
    Set selectionObj = outlookObj.ActiveExplorer.Selection
    
    itemCount = selectionObj.Count
    
    For i = itemCount To 1 Step -1
        Set mailObj = selectionObj.Item(i)
 
        newDate = Left(mailObj.SentOn, 11)
        spot1 = Mid(newDate, 9, 1)
        spot2 = Mid(newDate, 10, 1)
        spot3 = Mid(newDate, 11, 1)
        
        If spot1 = " " Then
            newDate = Left(mailObj.SentOn, 9)
        ElseIf spot2 = " " Then
            newDate = Left(mailObj.SentOn, 10)
        ElseIf spot3 = " " Then
            newDate = Left(mailObj.SentOn, 11)
        End If
        
        strNewSubject = newDate & " " & mailObj.Subject
        mailObj.Subject = Replace(strNewSubject, Chr(34), "")
        mailObj.Save
    Next
        Set objMessage = Nothing
        Set mailObj = Nothing
        Set outlookObj = Nothing
        Set selectionObj = Nothing
    
End Sub
 
 
Function OpenMAPIFolder(ByVal szPath As String)
    Dim app, ns, flr As MAPIFolder, szDir, i
    On Error GoTo errOMF
    Set flr = Nothing
    Set app = CreateObject("Outlook.Application")
    If Left(szPath, Len("\")) = "\" Then
        szPath = Mid(szPath, Len("\") + 1)
    Else
        Set flr = app.ActiveExplorer.CurrentFolder
    End If
    While szPath <> ""
        i = InStr(szPath, "\")
        If i Then
            szDir = Left(szPath, i - 1)
            szPath = Mid(szPath, i + Len("\"))
        Else
            szDir = szPath
            szPath = ""
        End If
        If IsNothing(flr) Then
            Set ns = app.GetNamespace("MAPI")
            Set flr = ns.Folders(szDir)
        Else
            Set flr = flr.Folders(szDir)
        End If
    Wend
    Set OpenMAPIFolder = flr
    On Error GoTo 0
    Exit Function
errOMF:
    Set OpenMAPIFolder = Nothing
    On Error GoTo 0
End Function
 
Function IsNothing(Obj)
  If TypeName(Obj) = "Nothing" Then
    IsNothing = True
  Else
    IsNothing = False
  End If
End Function
 
Function GetRootFolder()
    Dim olkRoot As Outlook.MAPIFolder
    Set olkRoot = Session.GetDefaultFolder(olFolderInbox).Parent
    GetRootFolder = "\" & olkRoot.Name
    Set olkRoot = Nothing
End Function

Open in new window

TectonicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arthur_WoodCommented:
In VB.NET all variables are derived from Object, hence there is no need to the SET keyword, which VB6 (and thus VBA) used to distinguish between variables that were Object types , and variables which were not.  
 
in VB.NET, this line:
Dim answer, newDate, spot1, spot2, spot3, strNewSubject As String
will result in ALL of the variables being declared as String type.  The VBA code declared the first 5 ( answer, newDate, spot1, spot2, spot3) as Variants ( a type that no longer exists in .NET)
likewise, this line
Dim app, ns, flr As MAPIFolder, szDir, i

will result in app, ns being declared as MAPIFolder types, which is clearly wrong.
I suggest this as the change:
 
Dim app as Outlook.Application
Dim ns as NameSpace
Dim szDir as String
Dim i as Integer
Dim flr as MAPIFolder
 
AW
 
0
TectonicAuthor Commented:
It declared those first five as variants? Is that a sub class to a string? I never heard that term before.

I think the part of the code with the (Dim app, ns, flr As MAPIFolder, szDir, i), i believe that is left over from the old way I was trying to get this code to work.

That way was putting mail items we wanted renamed in a folder push the button and it will rename only the files in the folder, after speaking to my boss he made the suggestion that it would be better to get it to work by selecting which mail items you wanted renamed.

Do you think that variant thing is the problem that the vba code is not working in vs?
0
Arthur_WoodCommented:
a variant type is the most general type available in VBA - it is equivalent to Object the .NET.
 
" VBA supports the Variant data type that can hold any type of data. If you omit the As Type clause in a variable declaration, Variant is the default type. While this may seem useful, it increases processing time when encountered in code because behind the scenes, the compiler has added no small amount of code to test what type of data is actually stored in the variable. Moreover, using a Variant can mask possible Type Mismatch errors that should be caught during testing. Instead of using a Variant type, declare the variable with a specific data type. "
 
Sorry to ask, but how have you been able to avoid ever hearing about the VARIANT data type?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

TectonicAuthor Commented:
i dont know how i have been able to avoid hearing about that type. i am not new to programming but i am fairly new to vba and vs. i thought when i was declaring those six variables i was decaring them all as strings because they was all the same line. so if i type dim x, y as integer only y will be declared an integer and x is unkown in vs and a variant in vba?
0
Arthur_WoodCommented:
>>"i thought when i was declaring those six variables i was decaring them all as strings because they was all the same line" >>That only works in Visual Studio.  In VBA, each variable MUST be declared separately, and it is best if they are each declared on their own line, like this:


Dim app as Outlook.Application
Dim ns as NameSpace
Dim szDir as String
Dim i as Integer
Dim flr as MAPIFolder

other than taht, I don't have much to offer as to why your code isn't working.  Can you set breakpoints, and then single step your code (using the F8 key)?
0
TectonicAuthor Commented:
Yeah I will try stepping through it to see if I can find out something on why its not working. I did step through it before and I noticed that once it got to one line it was like ending the code before it reached the end sub. I think the command it was ending on was mailObj = selectionObj.Item(i), I will take a further look at it when I get back. Thanks for your help.
0
TectonicAuthor Commented:
I seem to have it working, there was nothing really wrong with the code, i changed the lines

Set mailObj = selectionObj.Item(i)
newDate = Left(mailObj.SentOn, 11)

TO

newDate = Left(selectionObj.Item(i).sentOn, 11)

and it started working like normal, I am just going to work on creating an install for this and get it deployed

Thanks for your help Arthur.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.