Late Bound Access to NormalTemplate.Saved property in Word (C#)

E

Evan Stone

Hi,

How would one go about accessing the Application.NormalTemplate.Saved
property in Word via C# and LATE bound?

I'm currently getting the infamous "do you want to save Normal.dot" error on
certain systems from the following code (it seems to be from fresh
installations of Word, since some default settings are being saved to
Normal.dot on the first run), and I'd like to be able to set the
NormalTemplate.Saved property to true to inhibit the dialog in that
situation...

// Code Snippet Begin ----------------------
Type WordType = Type.GetTypeFromProgID("Word.Application");

//Create instance of word
wordObject = Activator.CreateInstance(WordType);
if (wordObject != null)
{
parameter[0] = true;

//get the Version property
version = (string) WordType.InvokeMember("Version",
BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty, null,
wordObject, null);

// close the application
WordType.InvokeMember("Quit",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.InvokeMethod, null,
wordObject, null);
}
wordObject = null;
// Code Snippet End ----------------------

My question is how do I get at that property if I'm trying to work late
bound? Do I need to get the NormalTemplate object first then call an
InvokeMember on it to get the value of its Saved property?

Thanks!

evan k. stone | software engineer
 
T

Tony Jollans

I don't know C#, but late binding shouldn't make a difference to how you
access the property (it may affect things behind the scenes but not the
source code needed).
 
C

Cindy M -WordMVP-

Hi Evan,
How would one go about accessing the Application.NormalTemplate.Saved
property in Word via C# and LATE bound?
Something like this works for me:

wd.ApplicationClass wdApp;
//Assign it to an Word application; not shown here
//Variable to which value will be assigned
bool NT_Saved = true;

//Test with early binding in order to compare result
//Remove later
MessageBox.Show(wdApp.NormalTemplate.Saved.ToString());

try
{
//You'll have already done this...
Type wdApp_Type = wdApp.GetType();
object NT = wdApp_Type.InvokeMember("NormalTemplate",
BindingFlags.GetProperty, null, wdApp, null);
Type NT_Type = NT.GetType();
NT_Saved = (bool) NT_Type.InvokeMember("Saved",
BindingFlags.Default |
BindingFlags.GetProperty,
null, NT, null );
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
//Show the result of late-binding
MessageBox.Show(NT_Saved.ToString());

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 

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