+ Reply to Thread
Results 1 to 3 of 3

copy module and past module in new workbook

Hybrid View

flunzy copy module and past module... 12-13-2013, 09:44 AM
xladept Re: copy module and past... 12-14-2013, 07:18 PM
flunzy Re: copy module and past... 12-17-2013, 04:33 AM
  1. #1
    Registered User
    Join Date
    04-10-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    76

    copy module and past module in new workbook

    Hi guys,

    I've got a beautifull code that works perfect for sending a workbook. Now I want to copy a module from my source book to the destination workbook but it doesn't succeed : ( the module that I want to copy is module 10 (sub check_files). Please help. Below the code:

    Sub Mail_Range()
        Dim Source As Range
        Dim Dest As Workbook
        Dim wb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set Source = Nothing
        On Error Resume Next
        Set Source = Range("A:O").SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    
        If Source Is Nothing Then
            MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
            Exit Sub
        End If
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        Set wb = ActiveWorkbook
        Set Dest = Workbooks.Add(xlWBATWorksheet)
    
        Source.Copy
        With Dest.Sheets(1)
            .Cells(1).PasteSpecial Paste:=8
            .Cells(1).PasteSpecial Paste:=xlPasteValues
            .Cells(1).PasteSpecial Paste:=xlPasteFormats
            .Cells(1).PasteSpecial Paste:=xlPasteValidation
            .Cells(1).Select
            Application.CutCopyMode = False
            
        Rows("1:1").Select
        Selection.RowHeight = 15
        Columns("M:M").Select
        Selection.ColumnWidth = 27
        
        Range("M3").Select
        
        With ActiveSheet.PageSetup
         .Orientation = xlLandscape
         .Zoom = False
         .FitToPagesWide = 1
         .FitToPagesTall = False
             
        End With
    End With
        
    
        
    
        TempFilePath = Environ$("temp") & "\"
        TempFileName = "file" & " " & Format(Now, "dd-mmm-yy")
    
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2013
            FileExtStr = ".xlsx": FileFormatNum = 51
        End If
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        With Dest
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
                .To = ThisWorkbook.Sheets("amount").Range("P6").Value
                .SentOnBehalfOfName = """
                .CC = ""
                .BCC = ""
                .Subject = ""
                .Body = ""
                .Attachments.Add Dest.FullName
                'You can add other files also like this
                '.Attachments.Add ("")
                .Display   'or use .Send
            End With
            On Error GoTo 0
            .Close savechanges:=False
        End With
    
        Kill TempFilePath & TempFileName & FileExtStr
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: copy module and past module in new workbook

    Hi flunzy,

    Is the destination workbook macro-enabled??
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    04-10-2013
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    76

    Re: copy module and past module in new workbook

    Quote Originally Posted by xladept View Post
    Hi flunzy,

    Is the destination workbook macro-enabled??
    Hi XL adept,

    The destination workbook is macro-enabled. I've made a workaround that partly does the trick. I've saved the module on a fixed location and added a macro that imports the module. This succeeds but when the module is imported and linked to the button, it states that the macro is only available in original sheet : ( Below you find the codes

    The send option:

    Sub Mail_Range()
        Dim Source As Range
        Dim Dest As Workbook
        Dim wb As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set Source = Nothing
        On Error Resume Next
        Set Source = Range("A:O").SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    
        If Source Is Nothing Then
            MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
            Exit Sub
        End If
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        
        Set Dest = Workbooks.Add(xlWBATWorksheet)
        Set wb = ActiveWorkbook
    
        Source.Copy
    
       
        Range("Q8").Select
        With Dest.Sheets(1)
            .Cells(1).PasteSpecial Paste:=8
            .Cells(1).PasteSpecial Paste:=xlPasteValues
            .Cells(1).PasteSpecial Paste:=xlPasteFormats
            .Cells(1).PasteSpecial Paste:=xlPasteValidation
            .Cells(1).Select
            Application.CutCopyMode = False
            
        Rows("1:1").Select
        Selection.RowHeight = 15
        Columns("M:M").Select
        Selection.ColumnWidth = 27
        
        Range("M3").Select
        
        With ActiveSheet.PageSetup
         .Orientation = xlLandscape
         .Zoom = False
         .FitToPagesWide = 1
         .FitToPagesTall = False
             
        End With
    End With
        
            ActiveSheet.Buttons.Add(1371, 29.25, 191.25, 45.75).Select
        Selection.Characters.Text = "Knop 4"
        With Selection.Characters(Start:=1, Length:=6).Font
            .Name = "Calibri"
            .FontStyle = "Standaard"
            .Size = 11
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = 1
        End With
        ActiveSheet.Shapes.SelectAll
        Selection.Characters.Text = "test"
        With Selection.Characters(Start:=1, Length:=4).Font
            .Name = "Calibri"
            .FontStyle = "Standaard"
            .Size = 11
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ColorIndex = 1
        End With
        Range("R9").Select
        
        With Dest.Sheets(1)
        ActiveWorkbook.VBProject.VBComponents.Import ("H:\My documents\Send_whole_workbook.bas")
        
            ActiveWorkbook.ActiveSheet.Buttons.Add(1371, 30, 192.75, 45).Select
        End With
        
            ActiveSheet.Shapes.Range(Array("Button 3")).Select
        Selection.OnAction = "Mail_workbook_Outlook_3"
        
        TempFilePath = Environ$("temp") & "\"
        TempFileName = "File" & " "
    
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2013
            FileExtStr = ".xlsm": FileFormatNum = 52
        End If
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        With Dest
            .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
            On Error Resume Next
            With OutMail
                .To = ThisWorkbook.Sheets("Main").Range("P8").Value
                .SentOnBehalfOfName = ""
                .CC = ""
                .BCC = ""
                .Subject = "File"
                .HTMLBody = "<H3><B>File</B></H3>" & 
                .Attachments.Add Dest.FullName
    
                
                .Display   
            End With
            On Error GoTo 0
            .Close savechanges:=False
        End With
    
        Kill TempFilePath & TempFileName & FileExtStr
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    and this is the module that needs to be imported:

    Sub Mail_workbook_Outlook_1()
    'Working in Excel 2000-2013
    'This example send the last saved version of the Activeworkbook
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .to = "info@me.nl"
            .CC = ""
            .BCC = ""
            .Subject = "This is the Subject line"
            .Body = "Hi there"
            .Attachments.Add ActiveWorkbook.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Code to Copy a Module from on Workbook to another not working?
    By JungleJme in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-20-2013, 10:48 AM
  2. How do I copy a macro module to another workbook?
    By Joe Miller in forum Excel General
    Replies: 10
    Last Post: 06-16-2010, 01:19 PM
  3. how to access Sheet module, normal module, Worbook module to type code
    By alibaba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2009, 07:51 AM
  4. How to copy module form one workbook to another
    By deepakmehta in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-08-2006, 05:10 PM
  5. [SOLVED] Run worksheet module code from workbook module?
    By keithb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2005, 12:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1