Discussion:
Problem using TWordApplication, TWordDocument and TWordFont
(too old to reply)
Vishal Khandelwal
2004-07-08 13:21:13 UTC
Permalink
Hi ,

I have a string which I m trying to write into a Word Document. What I want
is to have every character of that string in a different font(different
color, size, underlined or not, bold or not etc etc). I am able to achieve
this when i write every character in different line. However i am not able
to do that for the characters on same line, i,e, I am not able to write
characters of different fonts on the same line. Somehow it needs a "\n" to
change font?
Can someone help me with it?

Thanks in advance,
Vishal
Andrue Cope [TeamB]
2004-07-08 13:34:37 UTC
Permalink
I'm glad to see that you did actually post this question into the
correct newsgroup section but please note newsgroup guidelines ask that
you post to the single most appropriate section.

Good luck with the solution.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
BigStew
2004-07-08 18:59:33 UTC
Permalink
How are you adding the text? Could you show your code?
The Range object has a font property to you could get each character in a
RangePtr and set it's Font property.

Stew
Post by Vishal Khandelwal
Hi ,
I have a string which I m trying to write into a Word Document. What I want
is to have every character of that string in a different font(different
color, size, underlined or not, bold or not etc etc). I am able to achieve
this when i write every character in different line. However i am not able
to do that for the characters on same line, i,e, I am not able to write
characters of different fonts on the same line. Somehow it needs a "\n" to
change font?
Can someone help me with it?
Thanks in advance,
Vishal
Vishal Khandelwal
2004-07-09 06:15:55 UTC
Permalink
Hi ,
Thanks for replying .. here is the code i m using, with comments. This is
just the test code in which i m trying to write different characters of a
string on to a word file with all characters haveing different arbitary
fonts:

OleVariant Template = EmptyParam;
OleVariant NewTemplate = False;
OleVariant ItemIndex = 1;
try
{
//get input
AnsiString temp2 = "abcdadsdadswerwerwr";
char * buf = temp2.c_str();

try
{
WordApplication->Connect();
}
catch(Exception &exception)
{
Application->ShowException(&exception);
Abort;
}

WordApplication->GetDefaultInterface()->Visible = True;
WordApplication->set_Caption(StringToOleStr("Test Word feature"));
//Create new document
WordApplication->Documents->Add(Template, NewTemplate);

//Assign WordDocument component

WordDocument->ConnectTo(WordApplication->Documents->Item(ItemIndex));

//Turn Spell checking off because it takes a long time if enabled
and slows down Winword
WordApplication->Options->CheckSpellingAsYouType = False;
WordApplication->Options->CheckGrammarAsYouType = False;
for(int i=0; i<temp2.Length(); i++)
{
//Insert data
SetFont(i+1);
WordDocument->Range(EmptyParam,
EmptyParam)->InsertAfter(StringToOleStr(buf[i]));
// if i insert this line i can get characters of different fonts
on different lines
// WordDocument->Range(EmptyParam,
EmptyParam)->InsertAfter(StringToOleStr("\n"));
}

//remove links
WordFont->Disconnect();
WordDocument->Disconnect();
WordApplication->Disconnect();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
WordApplication->Disconnect();
}

Thanks,
Vishal
Post by BigStew
How are you adding the text? Could you show your code?
The Range object has a font property to you could get each character in a
RangePtr and set it's Font property.
Stew
Post by Vishal Khandelwal
Hi ,
I have a string which I m trying to write into a Word Document. What I
want
Post by Vishal Khandelwal
is to have every character of that string in a different font(different
color, size, underlined or not, bold or not etc etc). I am able to achieve
this when i write every character in different line. However i am not able
to do that for the characters on same line, i,e, I am not able to write
characters of different fonts on the same line. Somehow it needs a "\n" to
change font?
Can someone help me with it?
Thanks in advance,
Vishal
Vishal Khandelwal
2004-07-09 11:00:19 UTC
Permalink
Hi Stew,

i'll try this out straight away.

Thanks,
Vishal
I've made a couple of mods to your code.
Basically, I insert the whole string then get each character in a Range,
and
set it's font property.
For testing purposes, I alternate the font between Arial, and Comic, but
it
shows the general principle.
RangePtr r;
_FontPtr f;
WordDocument1->Range(EmptyParam,
EmptyParam)->InsertAfter(WideString(temp2));
for(int i=0; i<temp2.Length(); ++i)
{
r = WordDocument1->Range(OleVariant(i), EmptyParam);
f = r->get_Font();
div_t d = div(i, 2);
f->set_Name(d.rem ? WideString("Arial") : WideString("Comic Sans MS"));
r->set_Font(f);
}
Stew
(BTW, watch the over-quoting)
Post by Vishal Khandelwal
Hi ,
Thanks for replying .. here is the code i m using, with comments. This is
just the test code in which i m trying to write different characters of a
string on to a word file with all characters haveing different arbitary
BigStew
2004-07-09 10:38:09 UTC
Permalink
I've made a couple of mods to your code.
Basically, I insert the whole string then get each character in a Range, and
set it's font property.
For testing purposes, I alternate the font between Arial, and Comic, but it
shows the general principle.

RangePtr r;
_FontPtr f;
WordDocument1->Range(EmptyParam,
EmptyParam)->InsertAfter(WideString(temp2));
for(int i=0; i<temp2.Length(); ++i)
{
r = WordDocument1->Range(OleVariant(i), EmptyParam);
f = r->get_Font();
div_t d = div(i, 2);
f->set_Name(d.rem ? WideString("Arial") : WideString("Comic Sans MS"));
r->set_Font(f);
}

Stew

(BTW, watch the over-quoting)
Post by Vishal Khandelwal
Hi ,
Thanks for replying .. here is the code i m using, with comments. This is
just the test code in which i m trying to write different characters of a
string on to a word file with all characters haveing different arbitary
Vishal Khandelwal
2004-07-12 13:42:21 UTC
Permalink
Hi Stew,

I still have one problem.. in case i have a string of say 100,000
characters.. then storing it at once and the changinthe font of each
character one by one is a very time consuming task.. it takes really long
time.. more than even 30 mins. whats the workard or m i doing something
wrong.

Thanks,
Vishal.

whats the work ard... m i doing something wrong. hers my code now.
I've made a couple of mods to your code.
Basically, I insert the whole string then get each character in a Range, and
set it's font property.
For testing purposes, I alternate the font between Arial, and Comic, but it
shows the general principle.
RangePtr r;
_FontPtr f;
WordDocument1->Range(EmptyParam,
EmptyParam)->InsertAfter(WideString(temp2));
for(int i=0; i<temp2.Length(); ++i)
{
r = WordDocument1->Range(OleVariant(i), EmptyParam);
f = r->get_Font();
div_t d = div(i, 2);
f->set_Name(d.rem ? WideString("Arial") : WideString("Comic Sans MS"));
r->set_Font(f);
}
Stew
(BTW, watch the over-quoting)
Post by Vishal Khandelwal
Hi ,
Thanks for replying .. here is the code i m using, with comments. This is
just the test code in which i m trying to write different characters of a
string on to a word file with all characters haveing different arbitary
BigStew
2004-07-13 07:49:38 UTC
Permalink
No, you aren't doing anything wrong.
I noticed that the example I posted was slow, but I didn't imagine you would
be filling a document with 100,000 characters each one in a different font!
Perhaps a different approach is needed, i.e. generate the file in HTML.

Stew
Post by Vishal Khandelwal
Hi Stew,
I still have one problem.. in case i have a string of say 100,000
characters.. then storing it at once and the changinthe font of each
character one by one is a very time consuming task.. it takes really long
time.. more than even 30 mins. whats the workard or m i doing something
wrong.
Vishal Khandelwal
2004-07-13 10:10:02 UTC
Permalink
hey i found a solution. i m using TRichEdit now so everything is done
in-memory and later i dump it into the .doc file. and its much faster.

Thanks for ur help,
Vishal
Post by BigStew
No, you aren't doing anything wrong.
I noticed that the example I posted was slow, but I didn't imagine you would
be filling a document with 100,000 characters each one in a different font!
Perhaps a different approach is needed, i.e. generate the file in HTML.
Stew
Post by Vishal Khandelwal
Hi Stew,
I still have one problem.. in case i have a string of say 100,000
characters.. then storing it at once and the changinthe font of each
character one by one is a very time consuming task.. it takes really long
time.. more than even 30 mins. whats the workard or m i doing something
wrong.
a***@gmail.com
2016-05-23 09:21:44 UTC
Permalink
Post by Vishal Khandelwal
hey i found a solution. i m using TRichEdit now so everything is done
in-memory and later i dump it into the .doc file. and its much faster.
Thanks for ur help,
Vishal
Post by BigStew
No, you aren't doing anything wrong.
I noticed that the example I posted was slow, but I didn't imagine you
would
Post by BigStew
be filling a document with 100,000 characters each one in a different
font!
Post by BigStew
Perhaps a different approach is needed, i.e. generate the file in HTML.
Stew
Post by Vishal Khandelwal
Hi Stew,
I still have one problem.. in case i have a string of say 100,000
characters.. then storing it at once and the changinthe font of each
character one by one is a very time consuming task.. it takes really
long
Post by BigStew
Post by Vishal Khandelwal
time.. more than even 30 mins. whats the workard or m i doing something
wrong.
Hi... I am replying to a very old thread, I hope people are still available for response.
I am developing an application on Embarcadero C++ XE3 and want to publish report in Word format which also contains records in tabular format. Please help me with the following:
1. I am able to generate report but entire report is generated in single font color. How to change font color within a single report. I am using WordRangePtr for setting font.
2. Please provide example for inserting table and graphics in word.

Thanks in advance.

Amit Behere

Loading...