Word Automation Help

L

lltaylor

Hello,

I have an application that I use to scan through some word documents.
I have successfully managed to get my app, to open the relevant
documents and usings Word's find function, find the relevant part.

However when it starts its search an instance of word opens up, and I
thought I had invoked a close but the application stays open.

How do I close word once it has been opened, or better still how do I
carry out my find with word not opening at all.

Thanks in advance...

Lloyd

Here is some of the code.

//Open File paramters
object filename;
object confirmConversions = Type.Missing;
object readOnly = true;
object addToRecentFiles = Type.Missing;
object passwordDocument = Type.Missing;
object passwordTemplate = Type.Missing;
object revert = Type.Missing;
object writePasswordDocument = Type.Missing;
object writePasswordTemplate = Type.Missing;
object format = Type.Missing;
object encoding = Type.Missing;
object visible = Type.Missing;
object openConflictDocument = Type.Missing;
object openAndRepair = Type.Missing;
object documentDirection = Type.Missing;
object noEncodingDialog = Type.Missing;

//Close File Paramters
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat = Type.Missing;
object routeDocument = Type.Missing;

My method which opens the relevant file, searches it and "closes" it.

public void MSWordSearch()
{
oWord = new Word.Application();
try
{
oWord.Documents.Open(ref filename, ref confirmConversions, ref
readOnly, ref addToRecentFiles, ref passwordDocument,
ref passwordTemplate, ref revert, ref writePasswordDocument, ref
writePasswordTemplate, ref format, ref encoding, ref
visible, ref openConflictDocument, ref openAndRepair, ref
documentDirection, ref noEncodingDialog);

oDoc = oWord.ActiveDocument;

int intFound = 0;
Word.Find fnd = oWord.Selection.Find;

fnd.ClearFormatting();
fnd.Forward = true;
fnd.Wrap = Word.WdFindWrap.wdFindContinue;
fnd.Text = matchPattern;

ExecuteFind(fnd);
while(fnd.Found)
{
intFound++;
ExecuteFind(fnd);
}

UIupdate(" *** " + intFound + " occurences of " + matchPattern + "
found in " +this.filename.ToString()+ "\n");
oWord.Documents.Close(ref saveChanges, ref originalFormat, ref
routeDocument);
}
catch(Exception ex)
{
UIupdate("Error occurred reading file " +this.filename.ToString()+ "
" + ex.ToString());
}
}
 
N

Nick Weekes

Lloyd,

try adding:

oWord.visible = false;

to MSWordSearch. You cannot close word (or dispose of the object) as
this stores the Find method you need to use, and also is the parent
class for your Word Document objects. But hide it using the above
property, and you wont know its there...

Nick
 
L

Lloyd Taylor

Thanks,

That certainly helped....however word still appears briefly in the
status bar, causing a flicker like effect.

Is there anyway round that?

This is the order I have my code

oWord = new Word.Application();
oWord.Visible = false;
oWord.Documents.Open(.................);

Any ideas.

Lloyd
 
N

Nick Weekes

Lloyd,

What version of Office do you have? I have 2003, and my Word object is
visible by default, so I dont get the flicker.

The only way I can think to get around that for you, is to use the
'LockWindowUpdate' API call. This sends a message to windows to freeze
screen painting, so you could call this before creating oWord, set to
visible then turn unlock window updates (by calling the same api with a
value of 0).

Very messy, but your only solution I think...

Nick
 
N

Nick Weekes

oops...too early, and not enough caffeine....

I meant to say:

"and my Word object is INvisible by default, so I dont get the flicker."
 
L

Lloyd Taylor

Thanks Nick,

I am using Office 2003 too, how to you set your Word object to be
invisible by default?

Lloyd
 
N

Nick Weekes

thats the weird thing, I didnt need to. and Ive got no idea why the
behaviour is different on my machine to yours...

Stupid question, but did you declare oWord as Word.Application? Its
not in the sample you posted...
 
L

Lloyd Taylor

These are just code snippets as that actual class is quite long.

Most of the arguments for the Open Method are of Type.Missing;

Everything works perfect, just that I get an annoying flicker everytime
word opens up and as the actual app is scanning a directory it can be a
little annoying.

I appreciate all your help though.

-------My Code--------

using Microsoft.Office.Core;
using System.Reflection;

Word.Application oWord;
Word.Document oDoc;

oWord = new Word.Application();
oWord.Visible = false;

oWord.Documents.Open(ref filename, ref confirmConversions, ref readOnly,
ref addToRecentFiles, ref passwordDocument,
ref passwordTemplate, ref revert, ref writePasswordDocument, ref
writePasswordTemplate, ref format, ref encoding, ref visible, ref
openConflictDocument, ref openAndRepair, ref documentDirection, ref
noEncodingDialog);

oDoc = oWord.ActiveDocument;
Word.Find fnd = oWord.Selection.Find;

And so on.........
 
N

Nick Weekes

Final thought, maybe its an OS thing? Im running XP SP2, maybe you
have win2k? long shot, but your code behaves itself on my machine...or
maybe there is something screwy with the install of Office...

Check this thread too Lloyd: http://tinyurl.com/9f8s4 , could be
something to do with the normal.dot (word template) you have?
 

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