Post #124,985
11/10/03 5:48:31 PM
|
Programmatically stripping VBA from a document?
I have a word document that has a solitary macro included. So that we don't offend the users on the other end, (by presenting them with the "This document has a macro" message) I would like the macro to politely self-destruct on execution, as it is a one-time macro.
Is there functionality such as this contained in VBA, or am I pipe dreaming?
The universe makes no sense until you accept that God uses base 13.
Even then, it's still pretty dodgy.
|
Post #125,003
11/10/03 8:56:03 PM
|
No clue, but...
You can define a macro in one document that works on another document. Then the document that you send users can have the macro run, but never had that macro installed so they won't trip the issue.
Ben
"good ideas and bad code build communities, the other three combinations do not" - [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
|
Post #125,013
11/10/03 9:20:35 PM
|
Re: No clue, but...
Are you sure of that? I would think that any document that tries to do any macro would raise the warning. If what you say is true, then that is the most balled up "feature" I can think of.
-drl
|
Post #125,112
11/11/03 10:42:27 AM
|
The goal is only to avoid annoying the users
Annoying yourself is acceptable.
Also I haven't used Windows enough recently to know where the warning is coming from. My guess is that Outlook is doing it as an anti-virus feature. So running the program wouldn't generate a warning. I could trivially be wrong on that. But I would be surprised if the warning was something that you couldn't choose to silence on your machine...
Ben
"good ideas and bad code build communities, the other three combinations do not" - [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
|
Post #125,051
11/11/03 3:48:37 AM
|
CodeModule
Look at the CodeModule Object. I then found the following on the net by searching for vba codemodule delete word. No idea if it works: \n\nSub Remove(ByVal MacroName As String)\n Dim vbc As VBComponent\n Dim MyMacro As String\n\n Set vbc = VBE.ActiveVBProject.VBComponents("ThisDocument")\n With vbc.CodeModule\n .DeleteLines .ProcStartLine(MacroName, vbext_pk_Proc), _\n .ProcCountLines(MacroName, vbext_pk_Proc)\n End With\n\nEnd Sub\n
-- Chris Altmann
|
Post #125,179
11/11/03 4:07:03 PM
11/11/03 4:09:36 PM
|
Some ways to do it
One way is to take the macro out of your Autoexec Startup for the document. Usually the error doesn't happen until a macro is run IIRC. Of course my experience is only with Office 2000 and previous versions, so I don't know how newer versions handle that.
One quick way is to save the document as RTF and then load it and save it as a Doc file. Just hope that you don't lose any important formatting. Save it as a different name and see if it strips out the macro, I am sure it will. I am not sure how it will handle tables, bookmarks, and other Word features. Another way may be to use OpenOffice.Org as I think it does strip out macros as it doesn't use them, but don't quote me on that.
Even if you have a self destructing macro, the user will still get the "This document has a macro" error. They will get it only once and after they run the macro it should clean itself off.
What you want is almost the reverse of a Macro Virus, you have a Macro that kills itself.
I found this self-modifying code: [link|http://chrisrae.com/vba/routines/selfmodify.html|http://chrisrae.com/...s/selfmodify.html]
I am sure that you can use it in reverse to delete lines. Look up the "Application.VBE.ActiveVBProject.VBComponents" object.
Ah yes, I think I found it: [link|http://www.excel-center.com/mailing/067699h.htm|http://www.excel-cen...iling/067699h.htm]
'Place the code below into the standard module Sub ModuleClear() MsgBox "I am not there!" With Application.VBE.ActiveVBProject .vbComponents.Remove .vbComponents("MyModule") End With End Sub
Just put in the name of your module in the "MyModule" place and it should work. Maybe remove the Msgbox as it doesn't really serve a purpose.
I know the example is for Excel, but I think the VBE object also exists in Word.
"Lady I only speak two languages, English and Bad English!" - Corbin Dallas "The Fifth Element"
Edited by orion
Nov. 11, 2003, 04:09:36 PM EST
|
Post #125,183
11/11/03 4:16:55 PM
|
And this is why I never filtered you. (new thread)
Created as new thread #125182 titled [link|/forums/render/content/show?contentid=125182|And this is why I never filtered you.]
The universe makes no sense until you accept that God uses base 13.
Even then, it's still pretty dodgy.
|
Post #125,278
11/12/03 6:53:04 AM
|
RTF Conversion.
I don't know about Word 2000 and newer, but in Word 97, RTF-save didn't support any custom UI elements (menubars, keybindings, etc), macros or form-mode. Everything else saved and loaded a-okay. I had Word 97 default to RTF format for quite a while.
Wade.
Is it enough to love Is it enough to breathe Somebody rip my heart out And leave me here to bleed
| | Is it enough to die Somebody save my life I'd rather be Anything but Ordinary Please
| -- "Anything but Ordinary" by Avril Lavigne. |
|
Post #125,304
11/12/03 9:31:05 AM
|
Boy, was *that* not what I expected
I read your subject line as "RTF Conversation". And of course mentally converted "RTF" to the other well known acronym. Was wondering, "Geez, why is Wade going off on Norm?"
===
Implicitly condoning stupidity since 2001.
|
Post #125,369
11/12/03 2:33:39 PM
|
You're working too hard.
Peter [link|http://www.debian.org|Shill For Hire] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Blog]
|
Post #125,373
11/12/03 2:41:46 PM
|
Nope, thought the same too...
"All men are like grass, and all their glory is like the flowers of the field; the grass withers and the flowers fall, but the word of the Lord stands forever." 1 Peter 1:24-25
|
Post #125,402
11/12/03 4:44:52 PM
|
I had done a "double take" to get it right.
Alex
"The illegal we do immediately. The unconstitutional takes a little longer." - Henry Kissinger
|
Post #125,645
11/14/03 5:42:46 AM
|
LOL.
Is it enough to love Is it enough to breathe Somebody rip my heart out And leave me here to bleed
| | Is it enough to die Somebody save my life I'd rather be Anything but Ordinary Please
| -- "Anything but Ordinary" by Avril Lavigne. |
|