How do I delete code from a templates thisDocument???

J

Jeff

Hi,

I have a program that replaces the code of templates with new up to date
templates. It deletes all the old code modules and inserts the new ones.
It does this fine. However some of the old templates have code behind
the thisDocument part of the template that needs deleting for the new
templates to work. I can do this manually but is there a way to do it
via vba? I have well over 2000 documents to convert!!! I'm using Delphi
and the COM but I can convert the vba solution into Delphi. (the object
model is the same).

Code so far (Delphi but you should reconise the objects)

{ Removes Old VBA Code }
NoOfVBComps := OldDoc.VBProject.VBComponents.Count;
finished := false;
VBCompPos := 1;
count := 0;

while (not finished) do
begin
oleVar := VBCompPos;

try
oleVar2 := OldDoc.VBProject.VBComponents.Item(oleVar).Name;
OldDoc.VBProject.VBComponents.Remove
(OldDoc.VBProject.VBComponents.Item(oleVar2));

except
inc(VBCompPos);
end;

inc(count);

if count = NoOfVBComps then
finished := True;

end;

{ Add new code }
OldDoc.VBProject.VBComponents.Import(ExtractFilePath(ParamStr(0)) +
'NewCode1.bas');
OldDoc.VBProject.VBComponents.Import(ExtractFilePath(ParamStr(0)) +
'NewCode2.bas');

This deletes all the modules and replaces them but leaves the code behind
thisDocument untouched. Any help would be most welcome.

Thanks

Jeff
 
W

Word Heretic

G'day Jeff <[email protected]>,

delete the lines in the vbcomponent(1)

while .lines.linecount > 0
.lines(1).delete
wend

or whateve the approp syntax is for those objects



Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Jeff reckoned:
 
J

Jonathan West

Hi Jeff,

The simplest way I can think of to do this is in an automated way is as
follows

1. create a new document based on the template,
2. save it as a template,
3. delete the old template,
4. rename the new template with the name of the old template

Since a new document based on a template does not contain the VBA code from
the template, this will remove all code and not just the code in the
ThisDocument module.
 
P

Peter Hewett

Hi Jeff

To use Steve's method you need to make a reference to the "Visual Basic For
Applications Extensibility n.n" library, on my system n.n = 5.3. The file
path is: "E:\Program Files\Common Files\Microsoft Shared\VBA\VBA6
\VBE6EXT.OLB"

HTH + Cheers - Peter
 
J

Jeff

Thanks all,

Did with one line of code in the end:

OldDoc.VBProject.VBComponents.Item(1).CodeModule.DeleteLines(1,
OldDoc.VBProject.VBComponents.Item(1).CodeModule.CountOfLines);

Easy when you know how :).

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top