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
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.
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
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...