bilfusiontech
asked on
How to get Word VBA macro to compile a VBA project?
I'm using the attached procedure below to delete all the lines of code from a VBA, to ensure the saved document will not run code again. On opening the saved document getting "System Error &H8000040005(-214746259).
Once the saved document is manually opened and the VB editor recompiled(even though no code exists), this error will no longer affect the document.
Once the saved document is manually opened and the VB editor recompiled(even though no code exists), this error will no longer affect the document.
Sub DeleteAllVBACode_WhenSaving()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
On Error GoTo error_trap
Set VBProj = ActiveDocument.VBProject
Application.ScreenUpdating = False
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
Exit Sub
error_trap:
MsgBox Err.Number & vbCrLf & Err.Description, vbOKOnly + vbCritical, "Error in CleanUp"
End Sub
ASKER
Thanks for that, but I won't be able to use it as a solution.
These are existing documents with VBA that run autoclose, which is stored in a referenced template.
The DeleteAllVBAcode is done in a module in referenced template and removes the VBA code from the original doc calling it, not pretty I know. But I'm dealing with a volume of letters using this, that I only want to modify the global referenced template if possible.
Ideally, if there was a way to save a document using SaveAs in the code, that would not save the VBA code with it, then this issue with DeleteAllVBAcode and recompile would not exist. If there is a way to do this, it would be sufficient for what I'm looking for?
Otherwise, I'll still need a way to tell the document to compile or flag it as compiled so that the deleted code is no longer remembered when saved?
These are existing documents with VBA that run autoclose, which is stored in a referenced template.
The DeleteAllVBAcode is done in a module in referenced template and removes the VBA code from the original doc calling it, not pretty I know. But I'm dealing with a volume of letters using this, that I only want to modify the global referenced template if possible.
Ideally, if there was a way to save a document using SaveAs in the code, that would not save the VBA code with it, then this issue with DeleteAllVBAcode and recompile would not exist. If there is a way to do this, it would be sufficient for what I'm looking for?
Otherwise, I'll still need a way to tell the document to compile or flag it as compiled so that the deleted code is no longer remembered when saved?
Yes there is. Save as an RTF formatted document as is maintains your formatting but it does not support Macros.
ActiveDocument.SaveAs FileName:="filename.rtf", FileFormat:=wdFormatRTF, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False , EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=F alse, SaveFormsData:=False, SaveAsAOCELetter:= _
False
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False
SaveNativePictureFormat:=F
False
ASKER
This looks good. The only problem with this is the file size grows by 600%? Is there any way to avoid this or is this standard with rtf files? Space would be a big issue with this.
ASKER
Found a solution to this.
Opening a saved document that has self deleted all VBA code, gives a System Error &H8000040005(-214746259) when the macro is enabled on the saved document.
This can be avoided using the code below.
Once you've deleted the VBA procedure and all it's commands - add the procedure back in
Eg:
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Sub AutoOpen()"
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
This seems to avoid the error listed above.
Opening a saved document that has self deleted all VBA code, gives a System Error &H8000040005(-214746259) when the macro is enabled on the saved document.
This can be avoided using the code below.
Once you've deleted the VBA procedure and all it's commands - add the procedure back in
Eg:
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Sub AutoOpen()"
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
This seems to avoid the error listed above.
Sub DeleteAllVBACode_WhenSaving()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Const DQUOTE = """" ' one " character
On Error GoTo error_trap
Set VBProj = ActiveDocument.VBProject
Application.ScreenUpdating = False
For Each VBComp In VBProj.VBComponents
If VBComp.Type = vbext_ct_Document Then
Set CodeMod = VBComp.CodeModule
With CodeMod
.DeleteLines 1, .CountOfLines
End With
'Add in AutoOpen module to prevent compilation errors when opening archive
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Sub AutoOpen()"
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
Else
VBProj.VBComponents.Remove VBComp
End If
Next VBComp
Set VBProj = Nothing
Exit Sub
error_trap:
MsgBox Err.Number & vbCrLf & Err.Description, vbOKOnly + vbCritical, "Error in CleanUp"
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open a new document
Select Tools . . . Macro . . . Security
Select the Trusted Publishers Tab
Select Truct access to Visual Basic Project
Click Ok
Select Tools . . . Macro . . . Macros
Type AutoClose for the macro Name
Select Normal.dot in the Macros in Dropdown
Click the Create button
Type:
DeleteAllVBACode_WhenSavin
so your macro with look like:
Sub AutoClose()
DeleteAllVBACode_WhenSavin
End Sub
Press CTRL+END
Paste your macro (DeleteAllVBACode_WhenSavi
Click the File . . . Save Normal
This should run on all documents when closed. You can modify your AutoClose macro to prompt for this action or run on specific documents. But based on your question ths will run on all documents that are Closed (except the Normal.dot of course).