+ Reply to Thread
Results 1 to 7 of 7

Delete code when saving the file

Hybrid View

  1. #1
    Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2007
    Posts
    438

    Delete code when saving the file

    Hello again,

    I have posted already the problem to delete the code when sending a sheet via email and I have found the solution to the problem.

    I tried to do the same when saving the file to specific folder, but i am facing problem.
    The code that I am using now is:

    Sub SaveFile()
    
    Dim CurrentPath As String
    Const PathToSaveTo As String = "C:\Documents and Settings\All Users\Documents\"
        'Store the current path
        CurrentPath = CurDir
        ''Change the path to the one we want
        'ChDrive "C"
        'ChDir PathToSaveTo
        ''or perhaps...
        SetCurrentDirectory PathToSaveTo    '"\\SomeServer\Some Path"
        'test to see if the folder is already saved in the correct folder
        
                 With ThisWorkbook
                If .Path <> PathToSaveTo Then
                .SaveAs FileName:=PathToSaveTo & .Name
                         Else
                'file is already saved in folder therefore just save it
                .Save
                
            End If
        End With
        ''Change the path back
        'ChDrive CurrentPath
        SetCurrentDirectory CurrentPath
        
    End Sub
    Can I add somewhere in this code the lines that will delete the code form the file?

      Destwb.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule.DeleteLines 1, _
      Destwb.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule.CountOfLines
    Last edited by Bob@Sun; 01-26-2010 at 04:43 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Delete code when saving the file

    Can't you call the macro that deletes the code that you have in the other reply?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2007
    Posts
    438

    Re: Delete code when saving the file

    Do you mean this macro?

    Sub DeleteAllVBACode()
    
            Dim VBProj As VBIDE.VBProject
            Dim VBComp As VBIDE.VBComponent
            Dim CodeMod As VBIDE.CodeModule
            
            Set VBProj = ActiveWorkbook.VBProject
            
            For Each VBComp In VBProj.VBComponents
                    VBProj.VBComponents.Remove VBComp
            Next VBComp
        End Sub
    I tried but probably I am not calling the macro in the right place because it is not working.

    Where should I add the Call DeleteAllVBACode function in my code?

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Delete code when saving the file

    I would recommend that you test on a backup copy. Try
    Option Explicit
    
    Sub SaveFile()
    
        Dim CurrentPath As String
        Const PathToSaveTo As String = "C:\Documents and Settings\All Users\Documents\"
        'Store the current path
        CurrentPath = CurDir
        ''Change the path to the one we want
        'ChDrive "C"
        'ChDir PathToSaveTo
        ''or perhaps...
        SetCurrentDirectory PathToSaveTo    '"\\SomeServer\Some Path"
        'test to see if the folder is already saved in the correct folder
    
        With ThisWorkbook
            If .Path <> PathToSaveTo Then
                .SaveAs Filename:=PathToSaveTo & .Name
            Else
                'file is already saved in folder therefore just save it
                .Save
            End If
        End With
        DeleteAllVBACode
        ''Change the path back
        'ChDrive CurrentPath
        SetCurrentDirectory CurrentPath
    
    End Sub

  5. #5
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Delete code when saving the file

    Hi,

    As a quick reminder, do not forget in VBE
    Tools>References
    Microsoft Visual Basic for Applications Extensibility ...

    HTH

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Delete code when saving the file

    For others' reference,this is a continuation of an earlier post

  7. #7
    Forum Contributor Bob@Sun's Avatar
    Join Date
    09-03-2009
    Location
    Montuak, Usa
    MS-Off Ver
    Excel 2007
    Posts
    438

    Re: Delete code when saving the file

    Thanks to all! I got it to work

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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