Document.Open Office Example

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I want to open a Word Document in my C# Programm. I tried this :

Word.Application wordapp = new Word.Application();
object path = "TEST.DOC";
object vk_read_only = false;
object vk_visible = true;
object vk_false = false;
object vk_true = true;
object vk_dynamic = 2;

object vk_missing = Type.Missing;

Word.Document worddoc = wordapp.Documents.Open(ref path, ref
vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref
vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
ref vk_missing);

worddoc.Activate();
wordapp.Selection.TypeText("TEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEST");
wordapp.Selection.TypeParagraph();
worddoc.Save();
wordapp.Documents.Close(ref vk_missing, ref vk_missing, ref
vk_missing);


But this does not work, an Error oucurs since the Document.Open is done.
Where is my mistake ?

Please help me

Fabian
 
Fabian,
you need to ensure the document actually exists at the path specified, and
that you have all upper and lower casing exact.
For example, this path:

object path = "C:\\temp\\Test.doc";

would open "Test.doc" from the C:\temp folder.
--Peter
 
Hi,

Error :

ComException was Unhandled
Exception from HRESULT: 0x800A1436

and it occurs in this line :

Word.Document worddoc = wordapp.Documents.Open(ref path, ref
vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref
vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
ref vk_missing);


The Variables are implemented in this way :

Word.Application wordapp = new Word.Application();
object path = "TEST.DOC";
object vk_missing = Type.Missing;


The Doc Data was generated by a Crystal Report, but now i must add an Image
to the Document, because my CR Version does not allow dynamic Image Data
Sources. So I generate an CR, save it in Word. And now I want to open this
Word Document, and insert the Image.
I hope that this is possible.

But I cant open the DOC File. The CR is closed before, the Filename is
right. I dont know where my mistake is.

Please help me (and sorry for my english)

Greets

Fabian


Nicholas Paldino said:
Fabian,

What is the error and what line does it occur on?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fabian said:
Hello,

I want to open a Word Document in my C# Programm. I tried this :

Word.Application wordapp = new Word.Application();
object path = "TEST.DOC";
object vk_read_only = false;
object vk_visible = true;
object vk_false = false;
object vk_true = true;
object vk_dynamic = 2;

object vk_missing = Type.Missing;

Word.Document worddoc = wordapp.Documents.Open(ref path, ref
vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref
vk_missing,
ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref
vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref
vk_missing,
ref vk_missing);

worddoc.Activate();

wordapp.Selection.TypeText("TEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEST");
wordapp.Selection.TypeParagraph();
worddoc.Save();
wordapp.Documents.Close(ref vk_missing, ref vk_missing, ref
vk_missing);


But this does not work, an Error oucurs since the Document.Open is done.
Where is my mistake ?

Please help me

Fabian
 
Sorry, I forgot to say :

I use
VS2005.NET C#
Office2003
Windows2000




Fabian said:
Hi,

Error :

ComException was Unhandled
Exception from HRESULT: 0x800A1436

and it occurs in this line :

Word.Document worddoc = wordapp.Documents.Open(ref path, ref
vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref
vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
ref vk_missing);


The Variables are implemented in this way :

Word.Application wordapp = new Word.Application();
object path = "TEST.DOC";
object vk_missing = Type.Missing;


The Doc Data was generated by a Crystal Report, but now i must add an Image
to the Document, because my CR Version does not allow dynamic Image Data
Sources. So I generate an CR, save it in Word. And now I want to open this
Word Document, and insert the Image.
I hope that this is possible.

But I cant open the DOC File. The CR is closed before, the Filename is
right. I dont know where my mistake is.

Please help me (and sorry for my english)

Greets

Fabian


Nicholas Paldino said:
Fabian,

What is the error and what line does it occur on?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Fabian said:
Hello,

I want to open a Word Document in my C# Programm. I tried this :

Word.Application wordapp = new Word.Application();
object path = "TEST.DOC";
object vk_read_only = false;
object vk_visible = true;
object vk_false = false;
object vk_true = true;
object vk_dynamic = 2;

object vk_missing = Type.Missing;

Word.Document worddoc = wordapp.Documents.Open(ref path, ref
vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref
vk_missing,
ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref
vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref
vk_missing,
ref vk_missing);

worddoc.Activate();

wordapp.Selection.TypeText("TEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEST");
wordapp.Selection.TypeParagraph();
worddoc.Save();
wordapp.Documents.Close(ref vk_missing, ref vk_missing, ref
vk_missing);


But this does not work, an Error oucurs since the Document.Open is done.
Where is my mistake ?

Please help me

Fabian
 
Back
Top