Link to home
Start Free TrialLog in
Avatar of Mutley2003
Mutley2003

asked on

Getting "styles" for paragraphs in a Word document (from Delphi)

Suppose I have a Word document with user defined styles for paragraphs. Like "headline" and "comment" as well as the inbuilt Heading1 etc.

I want to iterate across paragraphs and, for each, extract (to some file)
a) the text of the paragraph
b) the name of the associated style
c) the characteristics of that style (eg font name and size)

How can I do that in delphi? ( I know how to open a Word doc in delphi with automation, but it is the various collections that confuse me)

thanks

Avatar of bernani
bernani
Flag of Belgium image

Hi,

You need a range to do what you're asking. See the vba help for all the option (if needed, Alt+F11 in Word give you access to the vba editor - vba help isn't installed by default, you need install it)

Maybe this could help:


var vParam: OleVariant;
    FPara: Paragraph;
    FRange: Range;
    cDoc: string;

begin
  cDoc := 'c:\document.doc';
  vParam := cDoc;
  WordApplication1.Connect;
  try

    WordApplication1.Documents.Open(vParam);
    WordDocument1.ConnectTo(WordApplication1.ActiveDocument);

// working with the opened document    
// chose your paragraph
    FPara := WordDocument1.Paragraphs.Item(3);
 // choose a word
    FRange := FPara.Range.Words.Item(2);
 // Word in bold
    FRange.Bold := 1;

and so on ....


For the styles, the vba help give a sample

// vba code
// Create the new style named 'Introduction'
Set myStyle = ActiveDocument.Styles.Add(Name:="Introduction",  Type:=wdStyleTypeCharacter)
With myStyle.Font
    .Bold = True
    .Italic = True
    .Name = "Arial"
    .Size = 12
End With

// apply the style to the selected paragraph
Selection.Range.Style = "Introduction"
ASKER CERTIFIED SOLUTION
Avatar of bernani
bernani
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mutley2003
Mutley2003

ASKER

thanks Bernani, I will try to do some tests .. I think that should be enough to get me started.