D
D
docbrain2016-10-02 15:58:07
Delphi
docbrain, 2016-10-02 15:58:07

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;

But I have one problem: I don't know how to work with the project's ThisDocument module. How to access it and write something there. Maybe someone will tell me? I will be very grateful.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question