 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
HD Guest
|
Posted: Thu Apr 14, 2005 1:10 pm Post subject: I need help creating a Word doc |
|
|
Hi,
I am trying to create a Word document with Delphi 7, using late binding... I
am using late binding because I have found out that the code usually works,
whatever the Word version on the client's computer.
What I usually do is that I go to word and record a macro with the actions I
need. Then, I am usually able to make it work in Delphi, using the
properties of the macro.
Here is my problem... The macro in Word is using the property "Selection"
and in Delphi, I am having a hard time using this property. I have tried
with the "Range" property and it seems to work, but that would mean that I
cannot use the Macro to help me with properties and methods.
Here is a part of my code using the "selection" property (it does not work):
oWord := CreateOleObject('word.application');
oWord.visible := False;
try
if FileExists('C:Test.doc') then
DeleteFile('C:Test.doc');
oWord.Documents.Open('C:Test.doc');
oDocument := oWord.Documents.add;
oSelection := oDocument.Select;
oSelection.TypeText('This is a line of text I want to write in the
document');
oSelection.TypeParagraph;
oDocument.SaveAs('c:Test.doc');
finally
if not varIsEmpty(oWord) then
oWord.Quit;
end;
|
|
| Back to top |
|
 |
George Birbilis Guest
|
Posted: Thu Apr 14, 2005 7:37 pm Post subject: Re: I need help creating a Word doc |
|
|
you mean the .Select method fails?
what error do you get (use try/try/except/finally block so that you can get
some error message or something at the except section)
| Quote: | oSelection := oDocument.Select;
oSelection.TypeText('This is a line of text I want to write in the
document');
oSelection.TypeParagraph;
oDocument.SaveAs('c:Test.doc');
finally
if not varIsEmpty(oWord) then
oWord.Quit;
end;
|
|
|
| Back to top |
|
 |
HD Guest
|
Posted: Mon Apr 18, 2005 1:21 pm Post subject: Re: I need help creating a Word doc |
|
|
Thanks George for helping me out.
I finally got it to work... I do not need to use oDocument variable
anymore... it seems that when I add the document, it automatically recognize
the oWord.Selection as being the document itself. I have part of the code
below... But I am having another problem when I want to change the
properties of the table...
When I write:
oWord.Selection.Tables(1).Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
I get an error message telling me that tables(1) is not a method. Any ideas
as to why and how to fix it?
Thanks,
HDumas
oWord := CreateOleObject('word.application');
oWord.visible := False;
try
oWord.documents.add;
oWord.Selection.Font.Bold := True;
oWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
oWord.Selection.Font.Size := 16;
oWord.Selection.TypeText('LISTE DES ATTRIBUTIONS INTERVENANTS/TABLES' +
#13+#10 +#13+#10);
oWord.ActiveDocument.Tables.Add(oWord.Selection.Range,
sqlTouteTableWord.RecordCount, 2,
wdWord9TableBehavior,
wdAutoFitContent);
..........
oWord.ActiveDocument.SaveAs('c:TouteTable.doc')
finally
if not varIsEmpty(oWord) then
oWord.Quit;
end;
"George Birbilis" <birbilis (AT) kagi (DOT) com> a écrit dans le message de
news:425ec675 (AT) newsgroups (DOT) borland.com...
| Quote: | you mean the .Select method fails?
what error do you get (use try/try/except/finally block so that you can
get
some error message or something at the except section)
oSelection := oDocument.Select;
oSelection.TypeText('This is a line of text I want to write in the
document');
oSelection.TypeParagraph;
oDocument.SaveAs('c:Test.doc');
finally
if not varIsEmpty(oWord) then
oWord.Quit;
end;
|
|
|
| Back to top |
|
 |
George Birbilis Guest
|
Posted: Mon Apr 18, 2005 2:08 pm Post subject: Re: I need help creating a Word doc |
|
|
| Quote: | When I write:
oWord.Selection.Tables(1).Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
I get an error message telling me that tables(1) is not a method. Any
ideas
as to why and how to fix it?
|
you need to use Tables[1] since in Delphi array indexes are specified not
using () as in VB. The () is used for functions/methods only, that's why its
says "Tables" is not a method
whether the 1st element is at 0-index or 1-index depends on the automation
server (the Word app that is). I think Word is using 1 for the 1st element
of indexed properties, but don't remember for sure
--
-----
George Birbilis (birbilis (AT) kagi (DOT) com)
Microsoft Most Valuable Professional
MVP J# for 2004 & 2005
http://www.kagi.com/birbilis
--------------
|
|
| Back to top |
|
 |
HD Guest
|
Posted: Mon Apr 18, 2005 3:16 pm Post subject: Re: I need help creating a Word doc |
|
|
Thanks again George,
I don't know why I did not think of that... I changed the () for [] and used
the 1 and the 0 index.
Now I get a different error message: "Item" is not a property.
I have tried:
oWord.ActiveDocument.Tables[0].Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
oWord.ActiveDocument.Tables[1].Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
oWord.Selection.Tables[0].Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
oWord.Selection.Tables[1].Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
but regardless, it always gives me the same message.
You have any ideas on this one??? Thanks.
HDumas
"George Birbilis" <birbilis (AT) kagi (DOT) com> a écrit dans le message de
news:4263bfa7 (AT) newsgroups (DOT) borland.com...
| Quote: | When I write:
oWord.Selection.Tables(1).Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
I get an error message telling me that tables(1) is not a method. Any
ideas
as to why and how to fix it?
you need to use Tables[1] since in Delphi array indexes are specified not
using () as in VB. The () is used for functions/methods only, that's why
its
says "Tables" is not a method
whether the 1st element is at 0-index or 1-index depends on the automation
server (the Word app that is). I think Word is using 1 for the 1st element
of indexed properties, but don't remember for sure
--
-----
George Birbilis (birbilis (AT) kagi (DOT) com)
Microsoft Most Valuable Professional
MVP J# for 2004 & 2005
http://www.kagi.com/birbilis
--------------
|
|
|
| Back to top |
|
 |
Mike Shkolnik Guest
|
Posted: Mon Apr 18, 2005 8:37 pm Post subject: Re: I need help creating a Word doc |
|
|
yourTable.Borders.Item(wdBorderLeft).LineStyle := wdLineStyleNone;
--
With best regards, Mike Shkolnik
E-mail: [email]mshkolnik (AT) scalabium (DOT) com[/email]
WEB: http://www.scalabium.com
"HD" <dumh (AT) hotmail (DOT) com> wrote
| Quote: | Thanks again George,
I don't know why I did not think of that... I changed the () for [] and
used
the 1 and the 0 index.
Now I get a different error message: "Item" is not a property.
I have tried:
oWord.ActiveDocument.Tables[0].Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
oWord.ActiveDocument.Tables[1].Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
oWord.Selection.Tables[0].Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
oWord.Selection.Tables[1].Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
but regardless, it always gives me the same message.
You have any ideas on this one??? Thanks.
HDumas
"George Birbilis" <birbilis (AT) kagi (DOT) com> a écrit dans le message de
news:4263bfa7 (AT) newsgroups (DOT) borland.com...
When I write:
oWord.Selection.Tables(1).Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
I get an error message telling me that tables(1) is not a method. Any
ideas
as to why and how to fix it?
you need to use Tables[1] since in Delphi array indexes are specified
not
using () as in VB. The () is used for functions/methods only, that's why
its
says "Tables" is not a method
whether the 1st element is at 0-index or 1-index depends on the
automation
server (the Word app that is). I think Word is using 1 for the 1st
element
of indexed properties, but don't remember for sure
--
-----
George Birbilis (birbilis (AT) kagi (DOT) com)
Microsoft Most Valuable Professional
MVP J# for 2004 & 2005
http://www.kagi.com/birbilis
--------------
|
|
|
| Back to top |
|
 |
George Birbilis Guest
|
Posted: Tue Apr 19, 2005 12:03 pm Post subject: Re: I need help creating a Word doc |
|
|
| Quote: | yourTable.Borders.Item(wdBorderLeft).LineStyle := wdLineStyleNone;
|
indeed if Item is an indexed property, you need to write:
yourTable.Borders.Item[wdBorderLeft].LineStyle := wdLineStyleNone;
if not and Item is a function (more rare), you write:
yourTable.Borders.Item(wdBorderLeft).LineStyle := wdLineStyleNone;
if you can also write
yourTable.Borders[wdBorderLeft].LineStyle := wdLineStyleNone;
that is skip the "Item", then it sure is a property (Item is the "default"
property of the object help by Borders) and needs the square brackets
syntax.
Maybe VB has more wise syntax, using () instead of [], why should the user
care (or know) if it's a function or an indexed property after all?
-----
George Birbilis (birbilis (AT) kagi (DOT) com)
Microsoft Most Valuable Professional
MVP J# for 2004 & 2005
http://www.kagi.com/birbilis
--------------
|
|
| Back to top |
|
 |
Nikolay Guest
|
Posted: Wed Apr 20, 2005 1:09 am Post subject: Re: I need help creating a Word doc |
|
|
yourtable.bordes.item(ITEMCONSTANT) is the way
and it is a principle on each collection for example
oword.documents.item(ITEM) and so on.
"HD" <dumh (AT) hotmail (DOT) com> wrote:
| Quote: | Thanks George for helping me out.
I finally got it to work... I do not need to use oDocument variable
anymore... it seems that when I add the document, it automatically recognize
the oWord.Selection as being the document itself. I have part of the code
below... But I am having another problem when I want to change the
properties of the table...
When I write:
oWord.Selection.Tables(1).Borders(wdBorderLeft).LineStyle :=
wdLineStyleNone;
I get an error message telling me that tables(1) is not a method. Any ideas
as to why and how to fix it?
Thanks,
HDumas
oWord := CreateOleObject('word.application');
oWord.visible := False;
try
oWord.documents.add;
oWord.Selection.Font.Bold := True;
oWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
oWord.Selection.Font.Size := 16;
oWord.Selection.TypeText('LISTE DES ATTRIBUTIONS INTERVENANTS/TABLES' +
#13+#10 +#13+#10);
oWord.ActiveDocument.Tables.Add(oWord.Selection.Range,
sqlTouteTableWord.RecordCount, 2,
wdWord9TableBehavior,
wdAutoFitContent);
.........
oWord.ActiveDocument.SaveAs('c:TouteTable.doc')
finally
if not varIsEmpty(oWord) then
oWord.Quit;
end;
"George Birbilis" <birbilis (AT) kagi (DOT) com> a écrit dans le message de
news:425ec675 (AT) newsgroups (DOT) borland.com...
you mean the .Select method fails?
what error do you get (use try/try/except/finally block so that you can
get
some error message or something at the except section)
oSelection := oDocument.Select;
oSelection.TypeText('This is a line of text I want to write in the
document');
oSelection.TypeParagraph;
oDocument.SaveAs('c:Test.doc');
finally
if not varIsEmpty(oWord) then
oWord.Quit;
end;
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|