Reputation: 775
I need to run a macro in a *.docm
file but, once the routine it's over, the document has to be saved as a .docx
file. After, this new file has to be opened to avoid making the final user open the file generated by himself and so, make the file's usage comfortably.
I need the macro to do this because of a trusting issues.
I use the code below:
Dim source As Document, copy As Document
Set source = Documents(1)
Dim l1 As InlineShape, l3 As InlineShape
With source
.Bookmarks("Logo1").Select
'Selection.InlineShapes.AddPicture FileName:=UserForm1.Label2 & UserForm1.TextBox1.Text & UserForm1.Label3.caption & "/logo1.jpg", _
LinkToFile:=False, SaveWithDocument:=True
l1.LockAspectRatio = True
l1.Height = CentimetersToPoints(2)
.Bookmarks("Logo3").Select
Selection.InlineShapes.AddPicture FileName:=UserForm1.Label2 & UserForm1.TextBox1.Text & UserForm1.Label3.Caption & "/logo3.jpg", _
LinkToFile:=False, SaveWithDocument:=True
l3.LockAspectRatio = True
l3.Height = CentimetersToPoints(2)
ActiveWindow.View.Type = wdPrintView
GetFileName 'This is used to type the name of the file to be saved as docx
Dim fName As String
fName = UserForm2.TextBox1.Text & UserForm2.Label2.Caption
.SaveAs2 fileName:=fName, FileFormat:=wdFormatDocumentDefault, SaveFormsData:=False
Set copy = Documents.Open fName
End With
If I run this macro with the code pasted above, creates a .docx
file with the same code that the .docm
has and runs the macro again.
I have tried to remove the code using this function that I found in a website:
Sub DeleteAllVBACode()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Set VBProj = ActiveDocument.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
But I get an error, so I have tried to remove it using this:
copy.VBProject.VBComponents.Remove copy.VBProject.VBComponents("UserForm1")
copy.VBProject.VBComponents.Remove copy.VBProject.VBComponents("Module1")
After running the code with these two lines, the .docx
has code still.
How can I achieve it?
Upvotes: 1
Views: 4211
Reputation: 775
Solved!
Using this sub, works perfectly:
Sub RemoveAllMacros(objDocument As Object)
' deletes all VBProject components from objDocument
' removes the code from built-in components that can't be deleted
' use like this: RemoveAllMacros ActiveWorkbook ' in Excel
' or like this: RemoveAllMacros ActiveWorkbookDocument ' in Word
' requires a reference to the
' Microsoft Visual Basic for Applications Extensibility library
Dim i As Long, l As Long
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
Provided by Exceltip in http://www.exceltip.com/modules-class-modules-in-vba/delete-all-macros-in-a-workbookdocument-using-vba-in-microsoft-excel.html
Upvotes: 1