The 1C:Enterprise developers forum

#1
People who like this: 0 Yes / 0 No
Active user
Rating: 2
Joined: Jul 3, 2013
Company: TRIAX

Hi All!
My enterprise have a lot of format of contract by file word document, the information on contract was merge with field data in excel file. And sometime format of contract is edited. If I design template for contracts on 1C to print customer information (from database) put to contract; then i always update format. Then I has a idea, use file word document with information merge was description by string between symbol "<>",  and then uses "Find and Replace" method with Microsoft Word COM object to do this. For example code:

Code
   WordApp = New ComObject("Word.Application");
   WordApp.Visible = False;
   WordApp.Documents.Open("Test.doc");

   WordApp.Selection.Find.Text = "<CustomerName>";//SearchString;
   WordApp.Selection.Find.Replacement.Text = "Hoàng Minh Trí"; //ReplaceString;
   WordApp.Selection.Find.Forward = True;
   WordApp.Selection.Find.Wrap = 1;
   WordApp.Selection.Find.Format = False;
   WordApp.Selection.Find.MatchCase = False;
   WordApp.Selection.Find.MatchWholeWord = False;
   WordApp.Selection.Find.MatchWildcards = False; 
   WordApp.Selection.Find.MatchAllWordForms = False;
   /// Perform the search and Replace
   WordApp.Selection.Find.Execute(Replace = 1);

When bug has a error: {DataProcessor.ImportExcel.Form.FormMain.Form(236,36)}: Biến chưa được xác định (Replace)
WordApp.Selection.Find.Execute(<<?>>Replace = 1); (Kiểm tra: Server)

I used this way with another programing language as Delphi is ok:
Code
function Word_StringReplace(wrdDocument: OLEVariant; SearchString, ReplaceString: string; Flags: TWordReplaceFlags): Boolean;
const
  wdFindContinue = 1;
  wdReplaceOne = 1;
  wdReplaceAll = 2;
  //wdDoNotSaveChanges = 0;
var
  WordApp: OLEVariant;
begin
  Result := False;
(*
  { Check if file exists }
  if not FileExists(ADocument) then
  begin
    ShowMessage('Specified Document not found.');
    Exit;
  end;

  { Create the OLE Object }
  try
    WordApp := CreateOLEObject('Word.Application');
  except
    on E: Exception do
    begin
      E.Message := 'Word is not available.';
      raise;
    end;
  end;

  try
    { Hide Word }
    WordApp.Visible := False;
    { Open the document }
    WordApp.Documents.Open(ADocument);
*)
    WordApp:=wrdDocument;
    { Initialize parameters}
    WordApp.Selection.Find.ClearFormatting;
    WordApp.Selection.Find.Text := SearchString;
    WordApp.Selection.Find.Replacement.Text := ReplaceString;
    WordApp.Selection.Find.Forward := True;
    WordApp.Selection.Find.Wrap := wdFindContinue;
    WordApp.Selection.Find.Format := False;
    WordApp.Selection.Find.MatchCase := wrfMatchCase in Flags;
    WordApp.Selection.Find.MatchWholeWord := False;
    WordApp.Selection.Find.MatchWildcards := wrfMatchWildcards in Flags;
    WordApp.Selection.Find.MatchSoundsLike := False;
    WordApp.Selection.Find.MatchAllWordForms := False;
    { Perform the search}
    if wrfReplaceAll in Flags then
      WordApp.Selection.Find.Execute(Replace := wdReplaceAll)
    else
      WordApp.Selection.Find.Execute(Replace := wdReplaceOne);
(*    { Save word }
    WordApp.ActiveDocument.SaveAs(ADocument);
    { Assume that successful }
    Result := True;
    { Close the document }
    WordApp.ActiveDocument.Close(wdDoNotSaveChanges);
  finally
    { Quit Word }
    WordApp.Quit;
    WordApp := Unassigned;
  end;
*)
end;

Someone can give me advice.
Thanks a lot!

 
#2
People who like this: 0 Yes / 0 No
Active user
Rating: 2
Joined: Jul 3, 2013
Company: TRIAX

Somebody tell me why can't call method "WordApp.Selection.Find.Execute()" of Word.Application ComObject in 1C please?

 
#3
People who like this: 0 Yes / 0 No
Just came
Rating: 0
Joined: Oct 10, 2012
Company:

Hello, Hoang Minh Tri,

When you working with a COM object in the 1C:Enterprise, everything you pass to a COM object method as a parameter is considered as a 1C:Enterprise object. That is why the platform attempts to find "Replace" object.

To solve your problem, try to pass all paramerets separated with commas.

Code
WordApp.Selection.Find.Execute(,,,,,,,,,,1,,,,);

 
#4
People who like this: 0 Yes / 0 No
Active user
Rating: 2
Joined: Jul 3, 2013
Company: TRIAX

Thank Sergey Polikarpov!
I found another way, used properties "Selection.Text" of Word.App ComObject. I code is ok (but slow):

Code
    try
         WordApp = New ComObject("Word.Application");
    except
         Message("Word is not available.");
    endtry;
    WordApp.Documents.Open("c:\test.doc");
    FindObject = WordApp.Selection.Find;
    FindObject.Text = "<<CustomerName>>";//SearchString;
    FindObject.Replacement.Text = "Hoàng Minh Trí"; //ReplaceString;
    FindObject.Replacement.ClearFormatting();
    FindObject.Execute();
    If FindObject.Found Then
        WordApp.Selection.Text = "Hoàng Minh Trí";
    EndIf;
    WordApp.Documents(1).SaveAs("c:\test.doc");
    WordApp.Quit;

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)