Answer the question
In order to leave comments, you need to log in
How to write to the ThisDocument module in VBA from Delphi?
Greetings. I have a code that creates macros in MS Office documents:
const
vbext_ct_StdModule = 1;
wdFormatXMLDocumentMacroEnabled = 13;
var
wdApp, wdDoc, wdVbProj, wdVbModule, wdVbCode : OleVariant;
FName : String;
I : Integer;
begin
try
wdApp := CreateOleObject('Word.Application');
finally
end;
wdApp.Visible := False;
for I := 0 to 100 do
wdDoc := wdApp.Documents.Open(Prey[I]);
wdVbProj := wdDoc.VBProject;
wdVbModule := wdVbProj.VBComponents.Add(vbext_ct_StdModule);
//wdVbModule.Name := 'VBModule1';
wdVbCode := wdVbModule.CodeModule;
wdApp.DisplayAlerts := False;
try
wdVbCode.DeleteLines(1, wdVbCode.CountOfLines);
wdVbCode.AddFromString('SUB END');
finally
wdApp.DisplayAlerts := True;
//wdDoc.Range.InsertBefore('??????? ?????????? ? ???? ????????? ?????? ?????? ? ?????? "Sub1".');
end;
if wdDoc.SaveFormat = wdFormatXMLDocumentMacroEnabled Then
wdDoc.Save
else
begin
for I := 0 to 100 do
begin
FName := Prey[I];
FName := LeftStr(FName, Length(FName) - Length(ExtractFileExt(FName))) + '.docm';
wdApp.DisplayAlerts := False;
wdDoc.SaveAs(FileName:=FName, FileFormat:=wdFormatXMLDocumentMacroEnabled);
wdApp.DisplayAlerts := True;
end;
end;
wdDoc.Close;
wdApp.Quit;
end;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question