Word & C#

B

Bllich

hello,
I have winForm app and I have some text and pictures that I
want to save into a word file when I read it from a database.
I don't know how many text or pictures do I have for one
value in a database, it varies from record to record..

I've seen some classes on the net, but I want to use one from
Microsoft, so I Added a reference Microsoft Word 9.0 object library
and done:

private void button1_Click(object sender, EventArgs e)
{
Word.DocumentClass nw = new Word.DocumentClass();

nw.Content.InsertAfter("fff");

Bitmap MyBitmap = new Bitmap("C:\\a.jpg");
Clipboard.SetImage(MyBitmap);
nw.Content.Paste();
Clipboard.Clear();

nw.Save();
}

and I don't see text
"fff"
when Word window comes up, I just see the "a.jpg" image...
though when I do - undo (Ctrl+Z), I get the "fff"
so I know it pasted the image over the text!

I know that using clipboard is out of the question ;)
and obvieously doesn't work..

Please help!
 
H

hardono

If the picture pasted over the text, does it means that you can drag
the picture and see the text?
 
B

Bllich

(e-mail address removed) wrote in @e1g2000hsg.googlegroups.com:
If the picture pasted over the text, does it means that you can drag
the picture and see the text?

No,

nw.Content.Paste();

selects all the previous content of the document and then
it does the Paste.. so it changes everything to what
is in the clipboard at the moment..
So when I do UNDO in Word I actually see the things that
were before Pasting..

Some kind of overriding the method would be in order but ..
I don't have te certain knowledge to do so..

I believe I'm missing some easy method to insert picture
after last line..
 
B

bob

Hi,
Had a bit of a muck around with this.
The following code inserts the bitmap beside the text.
Needs more work obviously. But the key seems to be using the
inlineshapes collection.
I would trample little old ladies underfoot to reach a decent book
that gave advanced use of the word PIA.
Beyond the basic open and insert text it appears there are just
snippets in articles.
Code follows:
hth
Bob
private void button1_Click(object sender, EventArgs e)
{
if (this.oFD1.ShowDialog() == DialogResult.OK)
{
string sDoc;
sDoc = this.oFD1.FileName;
InsertPicture(sDoc);

}
}
void InsertPicture(string sDoc)
{
try
{
word.Application mapp = new word.Application();
mapp.Visible = false;
object template = Type.Missing;
object newTemplate = Type.Missing;
object documentType = Type.Missing;
object optional = Type.Missing;
object visible = false;
object lTrue = true;
object lFalse = false;
object oDoc = sDoc;
Bitmap MyBitmap = new Bitmap("C:\\MyImage.jpg");



word._Document doc2 = mapp.Documents.Open(ref oDoc,
ref optional, ref lTrue, ref lFalse, ref optional,
ref optional, ref optional, ref optional, ref
optional,
ref optional, ref optional, ref lTrue, ref lFalse,
ref optional, ref lTrue);
IEnumerator en = doc2.Paragraphs.GetEnumerator();



en.MoveNext();

word.Paragraph p1 = (word.Paragraph)en.Current;
word.Range r = p1.Range;
object ro = r;
r.InsertAfter("Test String");
//doc2.Content.InsertFile("C:\\ADR Logo.jpg", ref
optional, ref lTrue, ref lFalse, ref lFalse);
doc2.InlineShapes.AddPicture("C:\\ADR Logo.jpg", ref
lFalse, ref lTrue, ref ro);




object saveChanges = true;
object originalFormat = Type.Missing;
object routeDocument = Type.Missing;

doc2.Close(ref saveChanges, ref originalFormat, ref
routeDocument);
mapp.Quit(ref saveChanges, ref originalFormat, ref
routeDocument);


}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}

}
 
B

Bllich

Hi,
Had a bit of a muck around with this.
The following code inserts the bitmap beside the text.
Needs more work obviously. But the key seems to be using the
inlineshapes collection.
I would trample little old ladies underfoot to reach a decent book
that gave advanced use of the word PIA.
Beyond the basic open and insert text it appears there are just
snippets in articles.

I will check it out. thanks bob!
 
T

Todos Menos [MSFT]

for the record:

24-7

I used to work at 'Sharis' for years and years and years

and one day we had a gas leak or something; we had to shut down the
store.. and we realized that we didn't have locks on the door.

we had been open almost 10 years without ever shutting down


now re: C# and Office?

WTF; why don't you just use VBA I mean seriously here... Record a
macro; put it in word

and screw C#
 
B

Bllich

now re: C# and Office?

WTF; why don't you just use VBA I mean seriously here... Record a
macro; put it in word

and screw C#

not that simple, its much more complex than the problem I
described, I need C# .. 'cause it's a segment of a whole system
 
T

Todos Menos [MSFT]

it's a SEGMENT OF A SYSTEM?


C# is a kids programming language

'lets all jump on the trendy programming language of the month bus'

ROFL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top