Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem using TWordApplication, TWordDocument and TWordFont

194 views
Skip to first unread message

Vishal Khandelwal

unread,
Jul 8, 2004, 9:21:13 AM7/8/04
to
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]

unread,
Jul 8, 2004, 9:34:37 AM7/8/04
to
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

unread,
Jul 8, 2004, 2:59:33 PM7/8/04
to
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

"Vishal Khandelwal" <vishal_k...@persistent.co.in> wrote in message
news:40ed...@newsgroups.borland.com...

Vishal Khandelwal

unread,
Jul 9, 2004, 2:15:55 AM7/9/04
to
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


"BigStew" <big_...@talk21.com> wrote in message

BigStew

unread,
Jul 9, 2004, 6:38:09 AM7/9/04
to
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)

"Vishal Khandelwal" <vishal_k...@persistent.co.in> wrote in message

news:40ee...@newsgroups.borland.com...

Vishal Khandelwal

unread,
Jul 9, 2004, 7:00:19 AM7/9/04
to
Hi Stew,

i'll try this out straight away.

Thanks,
Vishal

"BigStew" <Big_...@talk21.com> wrote in message

Vishal Khandelwal

unread,
Jul 12, 2004, 9:42:21 AM7/12/04
to
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.
"BigStew" <Big_...@talk21.com> wrote in message

BigStew

unread,
Jul 13, 2004, 3:49:38 AM7/13/04
to
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

"Vishal Khandelwal" <vishal_k...@persistent.co.in> wrote in message

news:40f2...@newsgroups.borland.com...

Vishal Khandelwal

unread,
Jul 13, 2004, 6:10:02 AM7/13/04
to
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

"BigStew" <Big_...@talk21.com> wrote in message

news:40f394cd$1...@newsgroups.borland.com...

amitbe...@gmail.com

unread,
May 23, 2016, 5:21:45 AM5/23/16
to
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
0 new messages