Results 1 to 15 of 15

Thread: Logical problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Logical problem

    I am sending email using vb code. My code is working fine.
    I have one difficulty...

    Code:
    Set OL = CreateObject("Outlook.Application")
        Set EmailItem = OL.CreateItem(olMailItem)
        fileName = List1.Text  ' here i am setting the filename as selected worksheet from list.
        Debug.Print fileName
       
        SaveName = folderPath & "RightAnswersTempWorkbooks" & Num & "\" & StripChars(fileName, "[/,\\,*,?,"",<,>,|,:]", "")  'folderpath is the path where the original selected workbook is located.num is random number.
        ActiveSheet.Copy
        Set Wkb = ActiveWorkbook
        Wkb.SaveAs SaveName
    
     With EmailItem
            .Subject = txtsubject.Text
            .Body = txtmessage.Text
            .To = Txtname.Text
            .Attachments.Add Wkb.FullName ' wkb.fullname is complete path
            .send
        End With

    What i am doing is .. user can select a workbook.All the sheets from that workbook will be listed in a listview.Now user can select a single sheet from listview. i am copying the selected worksheet in a new tempworkbook, and placing this workbook in new folder (folderPath & "RightAnswersTempWorkbooks" & Num) while sending mail i am attaching that temp workbook to mail.After sending the mail i am destroying the folder (folderPath & "RightAnswersTempWorkbooks" & Num)

    I want my code to work if user selects more than one worksheet. i.e for each selected worksheet i will copy the sheet into new folder(folderPath & "RightAnswersTempWorkbooks" & Num). and while sending i will attached these files from folders as an attachment.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Logical problem

    if the user selects multiple sheets just loop through the listview...

    Activate the selected sheet one at a time.. run your code to copy save & move to the next selected sheet
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: Logical problem

    Sorry i didn't mark thread as solved.

    I got the solution. Actually i was confused in my code only. It's a really ....... one.

    I loop in list box and its working properly.

    Code:
    Wkb.SaveAs SaveName
       
        On Error GoTo finderror
        
        With EmailItem
            .Subject = txtsubject.Text
            .Body = txtmessage.Text
            .To = Txtname.Text
            .Attachments.Add Wkb.FullName
            .send
        End With
    Here i am attaching activeworkbook to the mail.
    But with my new code, i have selected two worksheets and stored in a folder(both the sheets). How will i attached both the sheets?
    Anyone knows how can i send multiple files as attachment?
    Last edited by v_gyku; Oct 28th, 2005 at 07:57 AM.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Logical problem

    Quote Originally Posted by v_gyku
    Sorry i didn't mark thread as solved.

    I got the solution. Actually i was confused in my code only. It's a really ....... one.

    I loop in list box and its working properly.
    For everyone else reading this, this is why it is kind of important that a thread get marked as resolved once an answer has been found.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: Logical problem

    Hey Hack my thread is not solve, i thought like it is solved but it is no solved...

    Code:
    Wkb.SaveAs SaveName
       
        On Error GoTo finderror
        
        With EmailItem
            .Subject = txtsubject.Text
            .Body = txtmessage.Text
            .To = Txtname.Text
            .Attachments.Add Wkb.FullName
            .send
        End With
    I am attaching the activeworkbook to the mail.

    But now i changed the code and i am selecting more than one sheet.
    So how can i attach two sheets.(these are located in a same folder)
    do i have to save it in array rather than storing in a string variable SaveName, if yes can u plz guide me on this....

    Thanks...

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Logical problem

    The Attachments is a collection. So if your adding one then your done, but if your adding several to the same email then its needing a loop.

    Ps, why not if they are selecting sheets to mail, add all the selected sheets to a single temp workbook. Then just attach the one workbook? Easier for the receipient.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: Logical problem

    Robdog thanks for posting...
    I am helpless its client requirement.
    Do u have any solution for 1st condition?

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Logical problem

    What is first condition? Just test if more then one item is selected in your listview then loop when adding each attachment.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: Logical problem

    thats not the problem i am copying each selected heet in a folder.
    Now while sending the mail i have to take these sheets from folder and attched them.

    When i was just sending a single sheet

    [code]
    Wkb.SaveAs SaveName

    On Error GoTo finderror

    With EmailItem
    .Subject = txtsubject.Text
    .Body = txtmessage.Text
    .To = Txtname.Text
    .Attachments.Add Wkb.FullName ' this is the path of temp workbook
    .send
    End With
    [\code]

    Since i was sending only one sheet
    [code]
    Attachments.Add Wkb.FullName
    [\code]
    it was working. But now it will take only last wkb...

    Thanks...

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Logical problem

    Not sure if I understand the problem here. Can you explain a little more.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: Logical problem

    user has selected a single sheet from list.
    Sheet will be copied to a temp workbook and will be placed in a folder.
    After mail is sent this folder will be deleted.

    I am doing same thing for more than one worksheet.
    Two temp workbooks will be copied to same folder.

    Now i want to attach these two tempworkbook in mail.

    This will give u idea

    Code:
    Set OL = CreateObject("Outlook.Application")
        Set EmailItem = OL.CreateItem(olMailItem)
        fileName = List1.Text  ' here i am setting the filename as selected worksheet from list.
        Debug.Print fileName
       
        SaveName = folderPath & "RightAnswersTempWorkbooks" & Num & "\" & StripChars(fileName, "[/,\\,*,?,"",<,>,|,:]", "")  'folderpath is the path where the original selected workbook is located.num is random number.
        ActiveSheet.Copy
        Set Wkb = ActiveWorkbook
        Wkb.SaveAs SaveName
    
     With EmailItem
            .Subject = txtsubject.Text
            .Body = txtmessage.Text
            .To = Txtname.Text
            .Attachments.Add Wkb.FullName ' wkb.fullname is complete path ' this is for active sheet.'i want to attch two tempworkbooks here.
            .send
        End With

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Logical problem

    Ok, so you check your List1 for multiple selections. Then save each out to your temp files. Create the email and do a loop around the .Attachments.Add adding each name as you loop. Then send.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: Logical problem

    thanks robdog

    i wll try that tommorrow..
    thanks for being with me.
    Now i am leaving office...

    Bye....

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: Logical problem

    hello

    I tried doing somthing like this...

    Code:
     With EmailItem
            .Subject = txtsubject.Text
            .Body = txtmessage.Text
            .To = Txtname.Text
            .Attachments.Add SaveName
            .Attachments.Add "c:\sample.txt"   ' I have also tried somthing like .attachments.add file1,file2
            .send
        End With
    It dosn't work.
    What i have to do to attch two files in a single mail?

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Logical problem

    You need to add them like so in a loop for ease of programming.
    VB Code:
    1. Dim arArray() As String
    2. Dim i As Integer
    3. 'Populate your array with the filename/paths of the attachment file.
    4.  
    5. With EmailItem
    6.     .Subject = txtsubject.Text
    7.     .Body = txtmessage.Text
    8.     .To = Txtname.Text
    9.     For i = 0 to UBound(arArray) - 1
    10.         .Attachments.Add arArray(i)
    11.     Next
    12.     .send
    13. End With
    14. Erase arArray
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width