Results 1 to 9 of 9

Thread: [RESOLVED] copy worksheet

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Resolved [RESOLVED] copy worksheet

    I have names of all worksheets from a single workbook in a listbox.

    User will select multiple sheets from the listbox.

    I want to copy each selected sheets to a temp workbook(for each sheet there will be a seperate temp workbook). These temp workbooks will be placed in a folder which is already created.



    I have path of original workbook.

    I have path where the tempworkbooks will be placed.



    Thanks...

  2. #2
    New Member
    Join Date
    Oct 2005
    Posts
    5

    Re: copy worksheet

    I had something similar to this to do just last week. I turned on Macro Record, performed the desired functions directly and was able to modify the code easily for programmable extension.

    Sheetx = sheet to be copied or moved
    BookY = workbook to be copied or moved to

    looping through your list selection...
    VB Code:
    1. Sheets("Sheetx").Copy After:=Workbooks("BookY").Sheets(1)
    'select the workbook that received the new sheet.
    VB Code:
    1. Windows("BookY").Activate
    'save workbook(Y) in file path
    VB Code:
    1. ActiveWorkbook.SaveAs Filename:="D:\My Docs\BookY.xls", FileFormat:= _
    2.         xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
    3.         , CreateBackup:=False
    next loop

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: copy worksheet

    I have done it in a bit different way...


    I am taking sheetnames from listbox..

    Code:
     Set wbSource = Workbooks.Open(mypath)
        Set wshtSource = wbSource.Sheets(xls) ' xls is anme of my sheet without .xls
        Set wbTarget = Workbooks.Add
        
        'do the copy
        CopySheet wbSource, wshtSource, wbTarget
    
    
       'rename the sheet
        wbTarget.SaveAs (SaveName)
        
        wbSource.Close False
        Set wbSource = Nothing
        
        Set wbactive = Workbooks.Open(SaveName)
       
        'delete default 3 sheets
        For Each wsht In ActiveWorkbook.Sheets
            If wsht.Name <> xls Then
                wsht.Delete
            End If
        Next
    
    
    Private Sub CopySheet(ByRef wbSource As Workbook, ByRef wshtSource As workSheet, _
                          ByRef wbTarget As Workbook)
        'copies the source sheet to the end of the workbook
        wshtSource.Copy After:=wbTarget.Sheets(wbTarget.Sheets.Count)
    End Sub
    Can anyone tell me how can i attach multiple attachments in a single mail?

    I am sending single mail with somthing like this...

    [code]
    With EmailItem
    .Subject = txtsubject.Text
    .Body = txtmessage.Text
    .To = Txtname.Text
    .Attachments.Add SaveName
    .send
    End With
    [\code]

    I have tried somthing like

    .Attachments.Add "c:\SaveName.xls"
    .Attachments.Add "c:\SaveName1.xls"

    and also

    .Attachments.Add "c:\SaveName.xls","c:\SaveName1.xls"

    but it didn't workd out....

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: copy worksheet

    the .Attachements.Add should of worked in outlook, what is the error messge you are getting.
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: copy worksheet

    can i add 2 attachment like i have shown in above post?

    Its not giving any error just coming out of procedure.
    Last edited by v_gyku; Oct 29th, 2005 at 03:48 AM.

  6. #6
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: copy worksheet

    Yes you can with your first method.. something like this

    VB Code:
    1. Dim obout As Outlook.Application
    2.   Dim obmail As Outlook.MailItem
    3.   Dim att As Outlook.Attachments
    4.   Set obout = New Outlook.Application
    5.   Set obmail = obout.CreateItem(olMailItem)
    6.   With obmail
    7.     .To = "someone@Somewhere"
    8.     .Subject = "test"
    9.     .Body = "testing attachment adding"
    10.     Set att = obmail.Attachments
    11.     att.Add "C:\File1.txt"
    12.     att.Add "C:\File2.rtf"
    13.     Set att = Nothing
    14.     .Display
    15.   End With
    16.   Set obmail = Nothing
    17.   Set obout = Nothing

    I have used a object declaration for the attachments in the email, and it worked fine this way, but taking this out and using just the Mailitem.Attachments.Add Works aswell, which is why I asked you what error message you were getting, as to me it seems as though it cannot find the file to be attached.

    If it is coming out of procedure the remove your error trapping.
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: copy worksheet

    i have removed all error trapping stil not giving any error just coming out of procedur.

    In the above example u have given, not saying .send anywhere?

  8. #8
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: copy worksheet

    That is true.. I'm displaying it instead, so you can see if the attachments are being added, when I tested it they were at the top of the email.
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    Re: copy worksheet

    hey i have added .send and it worked thanks....

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