automate word, create a new document, add a picture 
Author Message
 automate word, create a new document, add a picture

Two questions, first I have try
http://www.*-*-*.com/ ;EN-
US;316384
but it doesn't work :(

The compiler says:
"Namespace" already contains an alias definition
for 'Word'"
and point to this line:
"using Word = Microsoft.Office.Interop.Word;"

Next problem. I have wrote a console application to
create a new word document by using com interop. Insert
text is no Problem. Now I want to add a picture.

VB Code is:
Selection.InlineShapes.AddPicture
FileName:=stringImagePath, _
LinkToFile:=False, SaveWithDocument:=True

I have try the following in C# without luck:
// add image
object imagePath= stringImagePath;
object saveWithObject=true;
object range=Missing.Value;
doc.Words.First.InlineShapes.AddPicture("Bild", ref
imagePath, ref saveWithObject, ref range);

the compiler says:
"An unhandled exception of
type 'System.Runtime.InteropServices.COMException'
occurred in WDconsole.exe
Additional information:  typ conflict."
and point to the doc.Words..... line.

What is wrong?
Thanks for assistance.

REFERENCES I used:
- VS.NET documentation
- http://www.*-*-*.com/
url=/library/en-us/dnexcl2k2/html/odc_offcs.asp
- http://www.*-*-*.com/ ;en-
us;311452



Sun, 22 May 2005 16:31:25 GMT  
 automate word, create a new document, add a picture
Michael,

    What version of word are you using?  The PIA's that MS provides are for
Office XP only and do not seem to work for earlier versions (and the type
conflict you are getting would seem to indicate you have a previous version
installed).

    Hope this helps.

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> Two questions, first I have try
> http://support.microsoft.com/default.aspx?scid=kb;EN-
> US;316384
> but it doesn't work :(

> The compiler says:
> "Namespace" already contains an alias definition
> for 'Word'"
> and point to this line:
> "using Word = Microsoft.Office.Interop.Word;"

> Next problem. I have wrote a console application to
> create a new word document by using com interop. Insert
> text is no Problem. Now I want to add a picture.

> VB Code is:
> Selection.InlineShapes.AddPicture
> FileName:=stringImagePath, _
> LinkToFile:=False, SaveWithDocument:=True

> I have try the following in c# without luck:
> // add image
> object imagePath= stringImagePath;
> object saveWithObject=true;
> object range=Missing.Value;
> doc.Words.First.InlineShapes.AddPicture("Bild", ref
> imagePath, ref saveWithObject, ref range);

> the compiler says:
> "An unhandled exception of
> type 'System.Runtime.InteropServices.COMException'
> occurred in WDconsole.exe
> Additional information:  typ conflict."
> and point to the doc.Words..... line.

> What is wrong?
> Thanks for assistance.

> REFERENCES I used:
> - VS.NET documentation
> - http://msdn.microsoft.com/library/default.asp?
> url=/library/en-us/dnexcl2k2/html/odc_offcs.asp
> - http://support.microsoft.com/default.aspx?scid=kb;en-
> us;311452



Sun, 22 May 2005 21:30:28 GMT  
 automate word, create a new document, add a picture

Quote:
> The compiler says:
> "Namespace" already contains an alias definition
> for 'Word'"
> and point to this line:
> "using Word = Microsoft.Office.Interop.Word;"

in VB.NET you do not have any problems to write:

Dim o as object
o = CreateObject("Word.Application")
o.Visible = true

note that this could not be even compiled in C# (because type 'object' does
not have the member 'Visible') but it works in VB.NET.

so, my BIG advice about automation: write a VB.NET module that does all
automation and call proper function from C#! under the same solution you do
not have any problems with data sharing.

automation is the only place I've found VB.NET REALLY useful.

Regards,
Wiktor Zychla



Sun, 22 May 2005 22:03:39 GMT  
 automate word, create a new document, add a picture
At http://www16.brinkster.com/ctina/csharp%5Csubject%
5Coffice%5Coffice02.asp
I have found working source code according to my first
question.

But I still haven't a solution for the other problem.

Using Office XP. I have add the COM to my project by
right click 'references' -> 'Add Reference' -> go to the
COM tap -> selected Mircosoft Word 10.0 Object Library -

Quote:
> 'OK' button

Please further advice.

Quote:
>-----Original Message-----
>Michael,

>    What version of word are you using?  The PIA's that
MS provides are for
>Office XP only and do not seem to work for earlier

versions (and the type
Quote:
>conflict you are getting would seem to indicate you have
a previous version
>installed).

>    Hope this helps.

>--
>               - Nicholas Paldino [.NET/C# MVP]



>> Two questions, first I have try
>> http://support.microsoft.com/default.aspx?scid=kb;EN-
>> US;316384
>> but it doesn't work :(

>> The compiler says:
>> "Namespace" already contains an alias definition
>> for 'Word'"
>> and point to this line:
>> "using Word = Microsoft.Office.Interop.Word;"

>> Next problem. I have wrote a console application to
>> create a new word document by using com interop. Insert
>> text is no Problem. Now I want to add a picture.

>> VB Code is:
>> Selection.InlineShapes.AddPicture
>> FileName:=stringImagePath, _
>> LinkToFile:=False, SaveWithDocument:=True

>> I have try the following in c# without luck:
>> // add image
>> object imagePath= stringImagePath;
>> object saveWithObject=true;
>> object range=Missing.Value;
>> doc.Words.First.InlineShapes.AddPicture("Bild", ref
>> imagePath, ref saveWithObject, ref range);

>> the compiler says:
>> "An unhandled exception of
>> type 'System.Runtime.InteropServices.COMException'
>> occurred in WDconsole.exe
>> Additional information:  typ conflict."
>> and point to the doc.Words..... line.

>> What is wrong?
>> Thanks for assistance.

>> REFERENCES I used:
>> - VS.NET documentation
>> - http://msdn.microsoft.com/library/default.asp?
>> url=/library/en-us/dnexcl2k2/html/odc_offcs.asp
>> - http://support.microsoft.com/default.aspx?scid=kb;en-
>> us;311452

>.



Mon, 23 May 2005 00:10:43 GMT  
 automate word, create a new document, add a picture
Michael,

    It should be pointed out that while this document will help you to
automate word, if you perform this repeatedly, it will leak references
everywhere.  You need to release every object that is returned to you from
word, as it is a COM object.  The example gets around this because it closes
the form, ending the application once the button is clicked.

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> At http://www16.brinkster.com/ctina/csharp%5Csubject%
> 5Coffice%5Coffice02.asp
> I have found working source code according to my first
> question.

> But I still haven't a solution for the other problem.

> Using Office XP. I have add the COM to my project by
> right click 'references' -> 'Add Reference' -> go to the
> COM tap -> selected Mircosoft Word 10.0 Object Library -
> > 'OK' button

> Please further advice.

> >-----Original Message-----
> >Michael,

> >    What version of word are you using?  The PIA's that
> MS provides are for
> >Office XP only and do not seem to work for earlier
> versions (and the type
> >conflict you are getting would seem to indicate you have
> a previous version
> >installed).

> >    Hope this helps.

> >--
> >               - Nicholas Paldino [.NET/C# MVP]



> >> Two questions, first I have try
> >> http://support.microsoft.com/default.aspx?scid=kb;EN-
> >> US;316384
> >> but it doesn't work :(

> >> The compiler says:
> >> "Namespace" already contains an alias definition
> >> for 'Word'"
> >> and point to this line:
> >> "using Word = Microsoft.Office.Interop.Word;"

> >> Next problem. I have wrote a console application to
> >> create a new word document by using com interop. Insert
> >> text is no Problem. Now I want to add a picture.

> >> VB Code is:
> >> Selection.InlineShapes.AddPicture
> >> FileName:=stringImagePath, _
> >> LinkToFile:=False, SaveWithDocument:=True

> >> I have try the following in c# without luck:
> >> // add image
> >> object imagePath= stringImagePath;
> >> object saveWithObject=true;
> >> object range=Missing.Value;
> >> doc.Words.First.InlineShapes.AddPicture("Bild", ref
> >> imagePath, ref saveWithObject, ref range);

> >> the compiler says:
> >> "An unhandled exception of
> >> type 'System.Runtime.InteropServices.COMException'
> >> occurred in WDconsole.exe
> >> Additional information:  typ conflict."
> >> and point to the doc.Words..... line.

> >> What is wrong?
> >> Thanks for assistance.

> >> REFERENCES I used:
> >> - VS.NET documentation
> >> - http://msdn.microsoft.com/library/default.asp?
> >> url=/library/en-us/dnexcl2k2/html/odc_offcs.asp
> >> - http://support.microsoft.com/default.aspx?scid=kb;en-
> >> us;311452

> >.



Mon, 23 May 2005 00:12:40 GMT  
 automate word, create a new document, add a picture
Nicholas,

you are right of course.

I have replaced this lines:

//Close this form.
this.Close();

with that:

//      Save the file, use default values except for
filename  
object fileName =
Environment.CurrentDirectory+"\\testDoc";
        object optional = Missing.Value;
oDoc.SaveAs2000( ref fileName,
        ref optional, ref optional, ref optional,
        ref optional, ref optional, ref optional,
        ref optional, ref optional, ref optional, ref
optional);

//      Now use the Quit method to cleanup like a good
citizen
object saveChanges = true;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
oWord.Quit(ref saveChanges, ref originalFormat, ref
routeDocument);

and it work fine.

Do you have any suggestions regarding to my second
problem?
I have tried a lot of various code, but no luck.
My last idea was that:

Word.InlineShape oPicture;
object imagePath= "D:\\tmp\\screenshot.bmp";
object saveWithObject=true;
object myRange = oDoc.Bookmarks.Item(ref oEndOfDoc).Range;
oPicture = wrdRng.InlineShapes.AddPicture("Bild", ref
imagePath, ref saveWithObject, ref myRange);

But, the compiler alway says "typ conflict" at line:
oPicture = wrdRng. ...

Thank you for assistance.

Quote:
>-----Original Message-----
>Michael,

>    It should be pointed out that while this document
will help you to
>automate word, if you perform this repeatedly, it will
leak references
>everywhere.  You need to release every object that is

returned to you from
Quote:
>word, as it is a COM object.  The example gets around

this because it closes
Quote:
>the form, ending the application once the button is
clicked.

>--
>               - Nicholas Paldino [.NET/C# MVP]



>> At http://www16.brinkster.com/ctina/csharp%5Csubject%
>> 5Coffice%5Coffice02.asp
>> I have found working source code according to my first
>> question.

>> But I still haven't a solution for the other problem.

>> Using Office XP. I have add the COM to my project by
>> right click 'references' -> 'Add Reference' -> go to
the
>> COM tap -> selected Mircosoft Word 10.0 Object
Library -
>> > 'OK' button

>> Please further advice.

>> >-----Original Message-----
>> >Michael,

>> >    What version of word are you using?  The PIA's
that
>> MS provides are for
>> >Office XP only and do not seem to work for earlier
>> versions (and the type
>> >conflict you are getting would seem to indicate you
have
>> a previous version
>> >installed).

>> >    Hope this helps.

>> >--
>> >               - Nicholas Paldino [.NET/C# MVP]



>> >> Two questions, first I have try
>> >> http://support.microsoft.com/default.aspx?
scid=kb;EN-
>> >> US;316384
>> >> but it doesn't work :(

>> >> The compiler says:
>> >> "Namespace" already contains an alias definition
>> >> for 'Word'"
>> >> and point to this line:
>> >> "using Word = Microsoft.Office.Interop.Word;"

>> >> Next problem. I have wrote a console application to
>> >> create a new word document by using com interop.
Insert
>> >> text is no Problem. Now I want to add a picture.

>> >> VB Code is:
>> >> Selection.InlineShapes.AddPicture
>> >> FileName:=stringImagePath, _
>> >> LinkToFile:=False, SaveWithDocument:=True

>> >> I have try the following in c# without luck:
>> >> // add image
>> >> object imagePath= stringImagePath;
>> >> object saveWithObject=true;
>> >> object range=Missing.Value;
>> >> doc.Words.First.InlineShapes.AddPicture("Bild", ref
>> >> imagePath, ref saveWithObject, ref range);

>> >> the compiler says:
>> >> "An unhandled exception of
>> >> type 'System.Runtime.InteropServices.COMException'
>> >> occurred in WDconsole.exe
>> >> Additional information:  typ conflict."
>> >> and point to the doc.Words..... line.

>> >> What is wrong?
>> >> Thanks for assistance.

>> >> REFERENCES I used:
>> >> - VS.NET documentation
>> >> - http://msdn.microsoft.com/library/default.asp?
>> >> url=/library/en-us/dnexcl2k2/html/odc_offcs.asp
>> >> - http://support.microsoft.com/default.aspx?
scid=kb;en-
>> >> us;311452

>> >.

>.



Mon, 23 May 2005 16:39:24 GMT  
 automate word, create a new document, add a picture
The problem is solved ;)

My mistake was that I have false parameters.

Right source code:

Word.InlineShape oPicture;
string imagePath= "D:\\tmp\\screenshot.bmp";
object saveWithObject=true;
object LinkToFile=true;
object myRange = System.Reflection.Missing.Value;
oPicture = wrdRng.InlineShapes.AddPicture(imagePath, ref
LinkToFile, ref saveWithObject, ref myRange);

Thanks.

Quote:
>-----Original Message-----
>Nicholas,

>you are right of course.

>I have replaced this lines:

>//Close this form.
>this.Close();

>with that:

>//  Save the file, use default values except for
>filename  
>object fileName =
>Environment.CurrentDirectory+"\\testDoc";
>    object optional = Missing.Value;
>oDoc.SaveAs2000( ref fileName,
>    ref optional, ref optional, ref optional,
>    ref optional, ref optional, ref optional,
>    ref optional, ref optional, ref optional, ref
>optional);

>//  Now use the Quit method to cleanup like a good
>citizen
>object saveChanges = true;
>object originalFormat = Missing.Value;
>object routeDocument = Missing.Value;
>oWord.Quit(ref saveChanges, ref originalFormat, ref
>routeDocument);

>and it work fine.

>Do you have any suggestions regarding to my second
>problem?
>I have tried a lot of various code, but no luck.
>My last idea was that:

>Word.InlineShape oPicture;
>object imagePath= "D:\\tmp\\screenshot.bmp";
>object saveWithObject=true;
>object myRange = oDoc.Bookmarks.Item(ref
oEndOfDoc).Range;
>oPicture = wrdRng.InlineShapes.AddPicture("Bild", ref
>imagePath, ref saveWithObject, ref myRange);

>But, the compiler alway says "typ conflict" at line:
>oPicture = wrdRng. ...

>Thank you for assistance.

>>-----Original Message-----
>>Michael,

>>    It should be pointed out that while this document
>will help you to
>>automate word, if you perform this repeatedly, it will
>leak references
>>everywhere.  You need to release every object that is
>returned to you from
>>word, as it is a COM object.  The example gets around
>this because it closes
>>the form, ending the application once the button is
>clicked.

>>--
>>               - Nicholas Paldino [.NET/C# MVP]



>>> At http://www16.brinkster.com/ctina/csharp%5Csubject%
>>> 5Coffice%5Coffice02.asp
>>> I have found working source code according to my first
>>> question.

>>> But I still haven't a solution for the other problem.

>>> Using Office XP. I have add the COM to my project by
>>> right click 'references' -> 'Add Reference' -> go to
>the
>>> COM tap -> selected Mircosoft Word 10.0 Object
>Library -
>>> > 'OK' button

>>> Please further advice.

>>> >-----Original Message-----
>>> >Michael,

>>> >    What version of word are you using?  The PIA's
>that
>>> MS provides are for
>>> >Office XP only and do not seem to work for earlier
>>> versions (and the type
>>> >conflict you are getting would seem to indicate you
>have
>>> a previous version
>>> >installed).

>>> >    Hope this helps.

>>> >--
>>> >               - Nicholas Paldino [.NET/C# MVP]



>>> >> Two questions, first I have try
>>> >> http://support.microsoft.com/default.aspx?
>scid=kb;EN-
>>> >> US;316384
>>> >> but it doesn't work :(

>>> >> The compiler says:
>>> >> "Namespace" already contains an alias definition
>>> >> for 'Word'"
>>> >> and point to this line:
>>> >> "using Word = Microsoft.Office.Interop.Word;"

>>> >> Next problem. I have wrote a console application to
>>> >> create a new word document by using com interop.
>Insert
>>> >> text is no Problem. Now I want to add a picture.

>>> >> VB Code is:
>>> >> Selection.InlineShapes.AddPicture
>>> >> FileName:=stringImagePath, _
>>> >> LinkToFile:=False, SaveWithDocument:=True

>>> >> I have try the following in c# without luck:
>>> >> // add image
>>> >> object imagePath= stringImagePath;
>>> >> object saveWithObject=true;
>>> >> object range=Missing.Value;
>>> >> doc.Words.First.InlineShapes.AddPicture("Bild", ref
>>> >> imagePath, ref saveWithObject, ref range);

>>> >> the compiler says:
>>> >> "An unhandled exception of
>>> >> type 'System.Runtime.InteropServices.COMException'
>>> >> occurred in WDconsole.exe
>>> >> Additional information:  typ conflict."
>>> >> and point to the doc.Words..... line.

>>> >> What is wrong?
>>> >> Thanks for assistance.

>>> >> REFERENCES I used:
>>> >> - VS.NET documentation
>>> >> - http://msdn.microsoft.com/library/default.asp?
>>> >> url=/library/en-us/dnexcl2k2/html/odc_offcs.asp
>>> >> - http://support.microsoft.com/default.aspx?
>scid=kb;en-
>>> >> us;311452

>>> >.

>>.

>.



Mon, 23 May 2005 21:27:15 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Automating Word: _Document vs. Document

2. Inserting a picture from a file into a word document

3. MDI: Create a new document from within another document

4. How to manually create a new document without click on the File new

5. Howto add property to a document (like Word does)

6. How to open a new word document using Vc++

7. Routines to create word documents

8. Creating Word Document form C#.net

9. Creating a thumbnail of a word document/template

10. Creating word 97 document with text boxes using automation

11. Creating Word Documents from VC++

12. create a word document using only text

 

 
Powered by phpBB® Forum Software