Consulting

Results 1 to 7 of 7

Thread: Delete VBA Code of another Excel

  1. #1
    VBAX Newbie
    Joined
    May 2010
    Posts
    5
    Location

    Delete VBA Code of another Excel

    Hi,

    I am writing VBA to copy the current Excel to a different location.

    After copying I need to remove the VBA code of the excel in the target location.

    I use the code from cpearson.com/excel/vbe.aspx

    [VBA]Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule

    Set VBProj = ActiveWorkbook.VBProject

    For Each VBComp In VBProj.VBComponents
    If VBComp.Type = vbext_ct_Document Then
    Set CodeMod = VBComp.CodeModule
    With CodeMod
    .DeleteLines 1, .CountOfLines
    End With
    Else
    VBProj.VBComponents.Remove VBComp
    End If
    Next VBComp[/VBA]
    The problem is if I use ActiveWorkbook.VBProject it uses the current workbook and it deletes the VBS in current workbook.

    But I need to delete the VBA code from a different excel in a different place.

    Could someone help me??

    Regards

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by jeema
    I am writing VBA to copy the current Excel to a different location...
    Can you show us how you are doing this bit? An example workbook would be nicest, or at least the code.

    Thanks,

    Mark

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Set VBProj = Workbooks("MyOtherWorkbook.xls").VBProject
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Or, maybe we don't need to see the rest...

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by GTO
    Or, maybe we don't need to see the rest...
    ... is that 'shooting the bull'?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Newbie
    Joined
    May 2010
    Posts
    5
    Location

    Still Having Problem

    I use the below code to copy


    [VBA]Sub TheSub()
    ActiveWorkbook.Save

    On Error Resume Next
    Application.ScreenUpdating = False
    TargetFileName = "c:\test\copy_" & ThisWorkbook.Name
    Workbooks(ThisWorkbook.Name).SaveCopyAs TargetFileName

    DeleteAllVBACode (TargetFileName)

    End Sub
    [/VBA]

    I use the below code to delete the VBA


    [VBA]Sub DeleteAllVBACode(TargetFileName As String)
    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
    If VBComp.Type = vbext_ct_Document Then
    Set CodeMod = VBComp.CodeModule
    With CodeMod
    .DeleteLines 1, .CountOfLines
    End With
    Else
    VBProj.VBComponents.Remove VBComp
    End If
    Next VBComp
    End Sub
    [/VBA]
    It is not working even I use the below code


    [VBA]Set VBProj = Workbooks(TargetFileName).VBProject [/VBA]


    Please help.

    Regards

  7. #7
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by xld
    ... is that 'shooting the bull'?
    Well I don't suppose a Bar/Pub is a legal prerequisite , but it sounds nice


    Hi jeema,

    Check out the VBA Help topic for SaveCopyAs, but in short, it does as named. The copy is saved to disk, but the copy is not opened. You'll need to open the new copy and then work on it.

    Hope that helps,

    Mark

Posting Permissions

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