Corby111

I have read alot of information about what I want to do, and no one has offered me a clear cut solution. I have seen a few posts that come close to this, and then they do not state how they did it, or which is my beset solution. I hope this is not off topic, or too vague but this is my best hope for an answer so please help. I have read all FAQ and read this first, and every article about Interop and the new VSTO SE projects (really cool, but more external communication is a must).

I have a multi-tier Windows Application, (Client Tier DataTier, BusinessObject / Application Logic Tier) in the client when I have a record (a contact) and that contact has a linked word document (a resume for that person). I know the very easiest way to launch that file is to shell the path and let windows use word and open it (cant use this choice, explained next).

Using a VSTO project I easily can open the file using this code in the ResumeDoc_Startup event:

this.Application.Documents.Open(ref fileName,

ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing);

(As a former Visual Basic developer that was forced to switch to C# - and thank god for the money I make now, but the VB.Net comparison is attractive although I hate VB.Net)

My question is similiar to a post from Cindy about something similiar to this, but how can I launch the VSTO Word project (document) with a parameter for the file name. I am considering creating a MSMQ message queue if I have to, but that still doesnt tell me how to instanciate the VSTO doc.

And If you know who wrote the help article for why IServiceProvider was not meant to be instanciated by the outside world, I would like to thank him for absolutly nothing and please tell his boss that page is a waste of disk space and bandwitth to anyone looking for real answers as to how to instanciate a VSTO Word document (project).

My last question is and I have seen alot of posts of this, but after 3 days of reading I didint book mark this one, how can I grab the handle of the running VSTO Word doc (assuming what I want to do above is possible) and place that instance of the doc in a Windows Control User Control.

Any help will be greatly appreciated,




Re: Visual Studio Tools for Office Windows App launching a Word Project (document)

Cindy Meister

Hi Corby

I'm somewhat uncertain as to the role VSTO + Word should be playing in your scenario. As a general rule, a Word document (or Excel workbook) created as a VSTO project is presented to the user for editing/input/analysis. I'm having trouble understanding why a VSTO document's Startup event should be opening an additional document

Or are you thinking about combining a VSTO Word Add-in and a VSTO document project (still not understanding where the document project comes into the picture)

Can you perhaps describe what you need to do from a user's perspective The steps you plan that the user will be executing and the result he should get (without worrying too much about exactly which technology will be involved)






Re: Visual Studio Tools for Office Windows App launching a Word Project (document)

Corby111

Ok, first off the reason I am using VSTO Project for Word, and if you know a better way please tell me,

I need to access the Words collection, and highlight words that match the search results from a Windows App (similiar to the way Google

highlights your results pane), and that seemed to me to be the easiest way to accomplish this.

What I did as a work around, is I created an xml file:

< xml version="1.0" encoding="utf-8" >

<ResumeViewer>

<FileName value="d:\temp\myresume.doc"></FileName>

</ResumeViewer>

In the Word Project I have renamed ThisDocument to ResumeDoc

and in the startup event I added code to get the file name from the xml file

private void ResumeDoc_Startup(object sender, System.EventArgs e)

{

// variable for fileName

string fileName = GetFileName();

// verify fileName exists

if((!String.IsNullOrEmpty(fileName)) && (System.IO.File.Exists(fileName)))

{

// call LoadDocument method

this.LoadDocument(fileName);

}

}

/// <summary>

/// This method reads the XML file ResumeViewer.xml

/// located in the same directory as the file

/// ResumeViewer.doc

/// </summary>

/// <returns></returns>

private string GetFileName()

{

// initial value

string fileName = null;

// local

XmlNode fileNameNode = null;

// Read a document

string xmlFileName = @"D:\Temp\ResumeViewer\ResumeViewer\resumeviewer.xml";

// verify xmlFileName exists

if(System.IO.File.Exists(xmlFileName))

{

XmlTextReader reader = new XmlTextReader(xmlFileName);

// Create xmlDoc

XmlDocument xmlDoc = new XmlDocument();

//Load the file into the XmlDocument

xmlDoc.Load(reader);

// close reader

reader.Close();

// get docNode

XmlNode docNode = xmlDoc.DocumentElement;

// if docNode exists

if(docNode != null)

{

// verify docNode has child nodes

if(docNode.HasChildNodes)

{

// get fileNameNode

fileNameNode = docNode.FirstChild;

}

// verify fileNameNode exists

if(fileNameNode != null)

{

// set fileName

fileName = fileNameNode.Attributes[0].Value.ToString();

}

}

}

// return value

return fileName;

}

/// <summary>

/// Loads a document based on the filename

/// </summary>

/// <param name="fileName"></param>

public void LoadDocument(object fileName)

{

// Create Word Doc Instance To A Certian File

Word.Document doc = this.Application.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

}

This works for what I wanted to do, I can write to an xml file and use that to open the file I want with System.Process.Start, the problem with that is it creates Two Instances of Word, One that is my blank Word Project.doc (resume.doc) and the other is the file I want.

Is there an easier way to open Word to a certain file, and give me access to the Words collection Is an Addin a better option sorry I new to Office programming. I no part of the problem is Word is Com and I am using .Net but there both MS products on a MS operating system, I wish there was a Word Object Model Bridge between .Net and VSTO.

Thanks for replying, sorry if this information is out there, I couldnt find it.

Thank you,

Corby Nichols

Houston, TX






Re: Visual Studio Tools for Office Windows App launching a Word Project (document)

Cindy Meister

Hi Corby

The suggested approach is a good one if you need to pass a information and there's no way to use a parameter, yes.

In order to use Word (or any Office application) you do not have to use a VSTO project. It's quite possible to automate the application directly from within your WinForms or Console app. In this case, unless there's an important reason to use System.Process.Start, it's more common to create an instance of the application. For instance (off the top of my head):

using Word = Microsoft.Office.Interop.Word;
Word.Application wdApp = New Word.Application();
wdApp.Visible = true;
Word.Document wdDoc = wdApp.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

Going this route, there'd be no need to write the name of the document to an external file :-) And this should also solve the problem about two documents appearing.

The best place to get help with general Office automation (as opposed to VSTO projects), is the office.developer.automation newsgroup, BTW.






Re: Visual Studio Tools for Office Windows App launching a Word Project (document)

Corby111

Thank you for the information, In a windows app I tried the code below

Word.Application wdApp = new Word.Application();

wdApp.Visible = true;

object fileName = @"d:\temp\corbynicholsresume.doc";

object missing = null;

// open doc

Word.Document wdDoc = wdApp.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

The code compiles, but when I run the app, I get a type mismatch error,

System.Runtime.InteropServices.ComException (hex numbers) : Type mismatch.

Exception from HResult (more hex numbers, if you need them let me know) DISP_E_TypeMismatch.

at Microsoft.Office.Interop.Word.Documents.Open(list of parameters), if you need them or If I can email a screen shot I can).

The code I am showing above is in a button click of a WindowsForm.

Thanks,

Corby Nichols






Re: Visual Studio Tools for Office Windows App launching a Word Project (document)

Corby111

Ok, I found the solution to the code above, in the Office.Automation sample, thought I would post it here since I started it here.

private void button1_Click(object sender, EventArgs e)

{

try

{

// variable for fileName

string fileName = @"d:\temp\corbynicholsresume.doc";

// Open File

OpenFile(fileName);

}

catch (Exception error)

{

MessageBox.Show(error.ToString());

}

}

private void OpenFile(string fileName)

{

Word.Application wordApp = null;

Word.Document wordDoc = null;

object missing = System.Reflection.Missing.Value;

object fileName2 = (object) fileName;

object readOnly = false;

object isVisible = true;

wordApp = new Word.ApplicationClass();

wordApp.Visible = true;

wordApp.Height = wordApp.System.VerticalResolution;

wordApp.Width = wordApp.System.HorizontalResolution;

wordDoc = wordApp.Documents.Open(ref fileName2, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

wordDoc.Activate();

}

Thanks for your help, the next step I am going to repost in the Automation group, but what I need now is to grab the handle of Word and place it in a user control, you have been so helpful (knowledgable) already, so I thought I would ask you (Cindy) have you ever done this If not anyone

Thanks,

Corby Nichols

Houston, TX






Re: Visual Studio Tools for Office Windows App launching a Word Project (document)

Cindy Meister

Hi Corby

object missing = System.Reflection.Missing.Value;

null isn't a valid value to pass to optional parameters. The VSTO tool declares that for you; if you're working "on your own", then you have to do it.

If you're going to be doing a lot of Office automation, I highly recommend ".NET Development for Microsoft Office " by Andrew Whitechape, from MS Press :-)