CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Posts
    2

    VBA Related > Removing Macro

    Hello,

    I had a quick question, more related to VBA, as opposed to VB.

    Right now in my document I have a command button that saves the document when you click the command button. What I want to do, is I have a script that removes VBA from the Word document after the command button is clicked:

    The process would be > User clicks command button to save document > after document is saved > another script executes right away and delete's all VBA in the VBE.. Right now I have a script that saves the document, and another one that will remove the vba - but Im just a little hazy on how I would combine the two scripts - any help would be appreciated, if possible.


    Code for Command button:

    Code:
    Private Sub CommandButton1_Click()
    
    Dim i As Long, l As Long
    Dim Today, FolderName$, DateValue$, NameofFile$, FullFileName$
    FolderName$ = "X:\Weekly Meetings\"
    DateValue$ = Format(Now, "yyyy-mm-dd")
    NameofFile$ = "- Core Agenda"
    FullFileName$ = FolderName$ + DateValue$ + " " + NameofFile$
    ActiveDocument.SaveAs FileName:=FullFileName$, FileFormat:=wdFormatDocument
    End Sub


    Code for Removal of VBA
    Code:
    Sub
    If objDocument Is Nothing Then Exit Sub
        i = 0
        On Error Resume Next
        i = objDocument.VBProject.VBComponents.Count
        On Error GoTo 0
        If i < 1 Then ' no VBComponents or protected VBProject
            MsgBox "The VBProject in " & objDocument.Name & _
                " is protected or has no components!", _
                vbInformation, "Remove All Macros"
            Exit Sub
        End If
        With objDocument.VBProject
            For i = .VBComponents.Count To 1 Step -1
                On Error Resume Next
                .VBComponents.Remove .VBComponents(i)
                ' delete the component
                On Error GoTo 0
            Next i
        End With
        With objDocument.VBProject
            For i = .VBComponents.Count To 1 Step -1
                l = 1
                On Error Resume Next
                l = .VBComponents(i).CodeModule.CountOfLines
                .VBComponents(i).CodeModule.DeleteLines 1, l
                ' clear lines
                On Error GoTo 0
            Next i
        End With
    
    End Sub

  2. #2
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,054

    Re: VBA Related > Removing Macro

    From what I understand you cannot delete the codes while it is still running unless you are referring to deleting codes in another file.

    It's like any application that cannot erase itself -- nor erase other application that is currently running.

    I would be surprised if I am wrong.
    Marketing our skills - please participate in the survey and share your insights
    -

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: VBA Related > Removing Macro

    Need to show more of your code. Unless it opens docs to scan for macros, then, the above is correct
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: VBA Related > Removing Macro

    You could perhaps add a Call to the RemoveMacros sub inside your Commandbutton1_click sub, like :
    Code:
    Private Sub CommandButton1_Click()
    
    Dim i As Long, l As Long
    Dim Today, FolderName$, DateValue$, NameofFile$, FullFileName$
    FolderName$ = "C:\"
    DateValue$ = Format(Now, "yyyy-mm-dd")
    NameofFile$ = "- Core Agenda"
    FullFileName$ = FolderName$ + DateValue$ + " " + NameofFile$
    ActiveDocument.SaveAs FileName:=FullFileName$, FileFormat:=wdFormatDocument
    Call RemoveMacros
    End Sub
    I doubt that it will delete the current running macro though...

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

Featured