Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Macro to my code

Status
Not open for further replies.

Turtleman10

Technical User
Sep 13, 2012
40
US
How come when I add the code in this macro to my application I get errors?

Code:
Sub CopyAttachments()
    
   Set objApp = CreateObject("Outlook.Application")
   Set objEXP = objApp.ActiveExplorer
   Set objSel = objEXP.Selection
   Set SRC1 = objSel.Item(1)
   Set fso = CreateObject("Scripting.FileSystemObject")
   Dim PATH As String
   PATH = "C:\test\"
       
   For Each ITEM2 In objSel
    
     Set myAttachments = ITEM2.Attachments
         
      If myAttachments.Count > 0 Then
 
          For i = 1 To myAttachments.Count
              myAttachments(i).SaveAsFile PATH & _
              myAttachments(i).DisplayName
          Next
    End If
    Next
    
End Sub
 
hi,

Errors? What errors on what statements?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I get Compile error: Variable not found on Set objApp = CreateObject("Outlook.Application")
it runds fine as a macro but when I attempt to put it in my application it has an issue. I'm sure its something simple that I am unaware of. Any help would be apreeciated.



Code:
Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Declaration
Dim strSubject As String
Dim strSubjectA As String
Dim strPartN As String
Dim strShipDT As String
Dim strPO As String
Dim strDesc As String
Dim strPiec As String
Dim myOrtK As String
Dim myOrtG As String
Dim strA As String
Dim strRev As String
Dim Filename As String
Dim Prompt$
Dim myOlApp As New Outlook.Application

'Destination folder
    myOrtK = "R:\CERTS\KOHLER"
    myOrtG = "R:\CERTS\GENERAC"
    
    On Error Resume Next
    
strSubject = Item.Subject
strA = Item.Subject

    If strSubject = "KECCERT" Then
    
        Prompt$ = "Do you want to send this Cert to Kohler?"
        If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Attachment") = vbNo Then
        Cancel = True
        Exit Sub
        End If
     
        strPartN = InputBox("Part Number", "Please insert Part number")
        
        strShipDT = InputBox("Ship Date", "Please insert Ship date")
    
        strPO = InputBox("P.O", "Please insert P.O number")
        
        strRev = InputBox("Revision", "Please insert Revision")
            
        Set Item = Outlook.Application.ActiveInspector.currentItem
            
        Item.Subject = "PN_" + strPartN + "_SD_" + strShipDT + "_PO_" + strPO
        
        strSubjectA = strA + strShipDT + strPartN + strRev
    
    ElseIf strSubject = "GNCCERT" Then
    
    Prompt$ = "Do you want to send this Cert to Generac?"
        If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Attachment") = vbNo Then Cancel = True
             
        strPartN = InputBox("Part Number", "Please insert Part number")
    
        strDesc = InputBox("Part Description", "Please insert Part Description")
        
        strPO = InputBox("P.O", "Please insert P.O number")
    
        strPiec = InputBox("Number of Pieces", "Please insert number of Pieces")
        
        strShipDT = InputBox("Ship Date", "Please insert Ship date")
        
        strRev = InputBox("Revision", "Please insert Revision")
        
        Set Item = Outlook.Application.ActiveInspector.currentItem
        
        Item.Subject = strPartN + "," + strDesc + "," + strPO + "," + strPiec + "," + strShipDT
        
        strSubjectA = strA + strShipDT + strPartN + strRev
        
End If

Prompt$ = "Do you want to send this Cert With this subject line?" + Item.Subject
    If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Attachment") = vbNo Then
    Cancel = True
    Exit Sub
    End If
        
   Set objApp = CreateObject("Outlook.Application")
   Set objEXP = objApp.ActiveExplorer
   Set objSel = objEXP.Selection
   Set SRC1 = objSel.Item(1)
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
   Dim PATH As String
   PATH = "C:\test\"
       
   For Each ITEM2 In objSel
    
     Set myAttachments = ITEM2.Attachments
         
      If myAttachments.Count > 0 Then
 
          For i = 1 To myAttachments.Count
              myAttachments(i).SaveAsFile PATH & _
              myAttachments(i).DisplayName
          Next
    End If
    Next
      
    'free variables

     
End Sub
 

You must declare ALL variables!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
The key is here:
Option Explicit

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
In Tools > Options -- Editor TAB, check Require variable declaration.

This option places Option Explicit[/]b in EVERY code window.

That way you will be required, at all times, to declare all variables before you can compile or execute your code, which is a VERY GOOD, Best and Accepted Practice for any programming effort.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
You have dimmed...
Code:
Dim [b]myOlApp[/b] As New Outlook.Application

...but are setting:
Code:
Set [b]objApp[/b] = CreateObject("Outlook.Application")

That is what Option Explicit is for: it checks for undeclared variables. NONE of the variables in the code you copied over is DIMmed; hence you'll get errors for each of these (objEXP, objSEL, etc.) unless you include them in your variable declaration.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top