Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delphi and microsoft word

Status
Not open for further replies.

adrive

Programmer
Oct 14, 2007
12
0
0
MY
hi,

i'd like to know if it's possible for delphi to apply bullets when using the OLE object to create a word file.

something like..

(i) phase 1
- description, description, description,
- description, description, description,
description.

(ii) phase 2
- description, description, description,
description, description, description,
description.
 
how are you accessing word? are you using the component or late binding?

Leslie

In an open world there's no need for windows and gates
 
i found this piece of code which will work with the component (I think!)...in any case, this may point you in the right direction!

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  ListFmt: ListFormat;
  ContinueList: OleVariant;
begin
  ListFmt := WordDocument1.Application.Selection.Range.ListFormat;
  ListFmt.ApplyBulletDefault;
  ContinueList := False;
  ListFmt.ApplyListTemplate(ListFmt.ListTemplate, ContinueList, EmptyParam);
end;

which according to the Delphi OLE Automation Google Group post works but doesn't allow changes to the default bullet type. There were several other posts on the thread that may have the solution to changing the default bullet.

HTH



Leslie

In an open world there's no need for windows and gates
 
hi paul, thanks for replying. I've been finding an active delphi forum for few days now!

first of all..yes im using late binding, and i've tried various ways even those found from google groups but nothing can seem to work or fully compatible with what im doing.

By the way, i'm trying to get these working, it kept saying incompatible types between listtemplate, listlevel against variant types. Is there any way to get it working? because i'd like to see the output of this code! :(



procedure TForm3.Button1Click(Sender: TObject);
var
vListTemplate: ListTemplate;
vLevel: OleVariant;
OutlineNum: OleVariant;
Level: ListLevel;
ContinuePrev: OleVariant;
var
wrdApp, wrdDoc, wrdSelect : OleVariant;
begin

// Create an instance of Word and make it visible
wrdApp := CreateOleObject('Word.Application');
wrdApp.Visible := True;
// Create a new document
wrdDoc := wrdApp.Documents.Add();
wrdSelect := wrdApp.Selection;
//
wrdSelect.TypeText('(i) phase 1' + chr(13));
wrdSelect.TypeText(' - description, description, description.' + chr(13));
wrdSelect.TypeText(' - description, description, description,');
wrdSelect.TypeText('description, description, description.' + chr(13));
wrdSelect.TypeText('(ii) phase 2' + chr(13));
wrdSelect.TypeText(' - description, description, description,');
wrdSelect.TypeText('description, description, description,');
wrdSelect.TypeText('description, description, description.' + chr(13));

//
wrdDoc.Application.ActiveDocument.Select;

OutLineNum := True;
vListTemplate := wrdDoc.ListTemplates.Add(OutLineNum, EmptyParam);

Level := vListTemplate.ListLevels.Item(1);

Level.NumberFormat := '?'; //ChrW(61607);
Level.TrailingCharacter := wdTrailingTab;
Level.NumberStyle := wdListNumberStyleBullet;
Level.Alignment := wdListLevelAlignLeft;

Level.Font.Bold := wdUndefined;
Level.Font.Italic := wdUndefined;
Level.Font.StrikeThrough := wdUndefined;
Level.Font.Subscript := wdUndefined;
Level.Font.Superscript := wdUndefined;
Level.Font.Shadow := wdUndefined;
Level.Font.Outline := wdUndefined;
Level.Font.Emboss := wdUndefined;
Level.Font.Engrave := wdUndefined;
Level.Font.AllCaps := wdUndefined;
Level.Font.Hidden := wdUndefined;
Level.Font.Underline := wdUndefined;
Level.Font.ColorIndex := wdUndefined;
Level.Font.Size := 10;
Level.Font.Animation := wdUndefined;
Level.Font.DoubleStrikeThrough := wdUndefined;
Level.Font.Name := 'Wingdings';

Level.LinkedStyle := '';

ContinuePrev := False;
wrdDoc.Range.ListFormat.ApplyListTemplate(vListTemplate,
ContinuePrev, EmptyParam); //wdListApplyToWholeList);

end;
 
so if you are using late binding you can't use the ListTemplate and ListLevel variable types, you would also have to declare those as OLEVariants...let me play around with this a little bit and I'll get back to you.

Leslie]
 
Ok I got this to work:
Code:
procedure TForm1.Button1Click(Sender: TObject);
 var
  vListTemplate: OleVariant;
  vLevel: OleVariant;
  OutlineNum: OleVariant;
  Level: OleVariant;
  ContinuePrev: OleVariant;
var
  wrdApp, wrdDoc, wrdSelect : OleVariant;
begin

  // Create an instance of Word and make it visible
  wrdApp := CreateOleObject('Word.Application');
  wrdApp.Visible := True;
  // Create a new document
  wrdDoc := wrdApp.Documents.Add();
  wrdSelect := wrdApp.Selection;
//
  wrdSelect.TypeText('(i) phase 1' + chr(13));
  wrdSelect.TypeText('    - description, description, description.' + chr(13));
  wrdSelect.TypeText('    - description, description, description,');
  wrdSelect.TypeText('description, description, description.' + chr(13));
  wrdSelect.TypeText('(ii) phase 2' + chr(13));
  wrdSelect.TypeText('    - description, description, description,');
  wrdSelect.TypeText('description, description, description,');
  wrdSelect.TypeText('description, description, description.' + chr(13));

//
  wrdDoc.Application.ActiveDocument.Select;

  OutLineNum := True;
  vListTemplate := wrdDoc.ListTemplates.Add(OutLineNum, EmptyParam);

  Level := vListTemplate.ListLevels.Item(1);

  Level.NumberFormat := '?'; //ChrW(61607);
  Level.TrailingCharacter := 0;
  Level.NumberStyle := 23;
  Level.Alignment := 0;

  Level.Font.Bold := 9999999;
  Level.Font.Italic := 9999999;
  Level.Font.StrikeThrough := 9999999;
  Level.Font.Subscript := 9999999;
  Level.Font.Superscript := 9999999;
  Level.Font.Shadow := 9999999;
  Level.Font.Outline := 9999999;
  Level.Font.Emboss := 9999999;
  Level.Font.Engrave := 9999999;
  Level.Font.AllCaps := 9999999;
  Level.Font.Hidden := 9999999;
  Level.Font.Underline := 9999999;
  Level.Font.ColorIndex := 9999999;
  Level.Font.Size := 10;
  Level.Font.Animation := 9999999;
  Level.Font.DoubleStrikeThrough := 9999999;
  Level.Font.Name := 'Wingdings';

  Level.LinkedStyle := '';

  ContinuePrev := False;
  wrdDoc.Range.ListFormat.ApplyListTemplate(vListTemplate,
ContinuePrev, EmptyParam); //wdListApplyToWholeList);

end;

I changed the variable types to OLEVariants and replaced all the wd* constants with the value instead...

Leslie

In an open world there's no need for windows and gates
 
Thanks so much lespaul! it really worked!

But now im still tinkering with the code as it seems that If I apply the bullets the second time on similiar text, the alignments would go crazy as seen from this example :

procedure TForm1.Button1Click(Sender: TObject);
var
vListTemplate: OleVariant;
vLevel: OleVariant;
OutlineNum: OleVariant;
Level: OleVariant;
ContinuePrev: OleVariant;
Range: OleVariant;
OutLineNumber : OleVariant;
paraStart, paraEnd: Integer;
var
wrdApp, wrdDoc, wrdSelect, wrdSelect2: OleVariant;
begin

// Create an instance of Word and make it visible
wrdApp := CreateOleObject('Word.Application');
wrdApp.Visible := True;
// Create a new document
wrdDoc := wrdApp.Documents.Add();
wrdSelect := wrdApp.Selection;
//

paraStart := wrdApp.Documents.Item(1).Paragraphs.Count;
wrdSelect.TypeText('(i) phase 1' + chr(13));
wrdSelect.TypeText(' - description, description, description.' + chr(13));
wrdSelect.TypeText(' - description, description, description,');
wrdSelect.TypeText('description, description, description.' + chr(13));
wrdSelect.TypeText('(ii) phase 2' + chr(13));
wrdSelect.TypeText(' - description, description, description,');
wrdSelect.TypeText('description, description, description,');
wrdSelect.TypeText('description, description, description.' + chr(13));
paraEnd := wrdApp.Documents.Item(1).Paragraphs.Count;

Range :=
wrdApp.Documents.Item(1).Range(
wrdApp.Documents.Item(1).Paragraphs.Item(paraStart).Range.Start,
wrdApp.Documents.Item(1).Paragraphs.Item(paraEnd).Range.End
);

OutLineNum := True;
vListTemplate := wrdDoc.ListTemplates.Add(OutLineNum, EmptyParam);

Level := vListTemplate.ListLevels.Item(1);

Level.NumberFormat := '(%1)'; //ChrW(61607);
Level.TrailingCharacter := wdTrailingTab;
Level.NumberStyle := wdListNumberStyleLowercaseRoman;
Level.Alignment := wdListLevelAlignLeft;
Level.ResetOnHigher := 0;
Level.StartAt := 1;
Level.Font.Size := 10;
Level.Font.Name := 'Arial';

Level.LinkedStyle := '';

ContinuePrev := False;

Range.ListFormat.ApplyListTemplate(vListTemplate,
ContinuePrev, EmptyParam);

paraStart := wrdApp.Documents.Item(1).Paragraphs.Count;
wrdSelect.TypeParagraph;
wrdSelect.TypeParagraph;
paraEnd := wrdApp.Documents.Item(1).Paragraphs.Count;

Range :=
wrdApp.Documents.Item(1).Range(
wrdApp.Documents.Item(1).Paragraphs.Item(paraStart).Range.Start,
wrdApp.Documents.Item(1).Paragraphs.Item(paraEnd).Range.End
);

//clears formatting
wrdSelect2 := Range.Find;
wrdSelect2.ClearFormatting;


paraStart := wrdApp.Documents.Item(1).Paragraphs.Count;
wrdSelect.TypeText('(i) phase 1' + chr(13));
wrdSelect.TypeText(' - description, description, description.' + chr(13));
wrdSelect.TypeText(' - description, description, description,');
wrdSelect.TypeText('description, description, description.' + chr(13));
wrdSelect.TypeText('(ii) phase 2' + chr(13));
wrdSelect.TypeText(' - description, description, description,');
wrdSelect.TypeText('description, description, description,');
wrdSelect.TypeText('description, description, description.' + chr(13));
paraEnd := wrdApp.Documents.Item(1).Paragraphs.Count;

Range :=
wrdApp.Documents.Item(1).Range(
wrdApp.Documents.Item(1).Paragraphs.Item(paraStart).Range.Start,
wrdApp.Documents.Item(1).Paragraphs.Item(paraEnd).Range.End
);

OutLineNum := True;
vListTemplate := wrdDoc.ListTemplates.Add(OutLineNum, EmptyParam);

Level := vListTemplate.ListLevels.Item(1);

Level.NumberFormat := '(%1)'; //ChrW(61607);
Level.TrailingCharacter := wdTrailingTab;
Level.NumberStyle := wdListNumberStyleLowercaseRoman;
Level.Alignment := wdListLevelAlignLeft;
Level.ResetOnHigher := 0;
Level.StartAt := 1;
Level.Font.Size := 10;
Level.Font.Name := 'Arial';

Level.LinkedStyle := '';

ContinuePrev := False;

Range.ListFormat.ApplyListTemplate(vListTemplate,
ContinuePrev, EmptyParam);

wrdSelect.TypeParagraph;
wrdSelect.TypeParagraph;


end;
 
forgot to mention that i'm now applying the bullets by the range objects, and there's actually a line that clears formatting of the range before applying bullets the 2nd time on 2nd portion of the text.

Maybe the object is 'remembering' something from previous bullets?
 
I want to say that this is a bug in Word. I have been creating a multipage document that has several outline lists and I have been finding that when I move to a new one this same thing happens. The alignment of the subsequent sections is not the same as the previous one. It's been very frustrating.

Is there a particular reason you need to use the bullet list/outline? I use bookmarks quite a bit to place text in a specific location in a document. You can insert horizontal and vertical tabs within the text and you can do all the "outline" stuff in Delphi and just pass text to Word to put in a specific location.

Open Word and go to Insert -> Bookmark and name it Outline.doc and then save the document to a specific location (I've coded it below to the root C drive, but you can change that).

Here's a bit of code to give you an idea:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  wrdApp, wrdDoc: OleVariant;
  strData : widestring;
begin

  // Create an instance of Word and make it visible
  wrdApp := CreateOleObject('Word.Application');
  wrdApp.Visible := True;
  // Create a new document
  wrdDoc := wrdApp.Documents.Add('C:\Outline.doc');

  strData := '(i) phase 1' + chr(13) +
             chr(9) + '- description, description, description.' + chr(13) +
             chr(9) + '- description, description, description,' + chr(13) +
             chr(9) + 'description, description, description.' + chr(13) +
             '(ii) phase 2' + chr(13) +
             chr(9) +  '- description, description, description, ' + chr(13) +
             chr(9) +  'description, description, description,' + chr(13) +
             chr(9) +  'description, description, description.' + chr(13);

  if wrdApp.ActiveDocument.Bookmarks.Exists('StartOutline') = True then
      wrdApp.ActiveDocument.Bookmarks.Item('StartOutline').Range.Text:= strData;

end;

Leslie

In an open world there's no need for windows and gates
 
lespaul,
originally i had to do bullet because i didn't want text that were wrapped go out of its alignment. Bulleting auto aligns and indents each paragraph uniformly.

Also, Peculiarly if you tried applying ApplyBulletDefault to its range.ListFormat object, it seems to be able to align properly!

But even with that, the bulleting behavior is too unpredictable and i've decided to move on writing my text in RTF format.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top