Invoke Print Preview from c# code

M

Mike Surface

Has anyone figured this out yet?

I've seen a post that says you need to use InvokeByName method of
Object Wrapper like so...

....
BindingFlags flags = BindingFlags.GetProperty |
BindingFlags.DeclaredOnly
| BindingFlags.Public | BindingFlags.Instance;

ObjectWrapper commandBars =
(ObjectWrapper)thisApplication.ActiveWindow.CommandBars;
int commandBarsCount = (int)commandBars.InvokeByName(
"Count", // prop
flags,
null, // arguments
null); // Culture

thisXDocument.UI.Alert("Got " + commandBarsCount.ToString() + "
commandBars");
....

Silviu

But I can't figure out how to access the "Print Preview" command bar.

Please help :)

ps thisApplication.ActiveWindow.CommandBars("standard").Controls("Print
Preview").Execute() does not work in c#
 
M

Mike Surface

With help from the person who originally posted the code sample and a
little investigation myself, I was able to come up with a couple
solutions...

Here's the code from Sylviu...

BindingFlags flags = BindingFlags.GetProperty |
BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.Instance;
ObjectWrapper commandBars =
(ObjectWrapper)thisApplication.ActiveWindow.CommandBars;

int commandBarsCount = (int)commandBars.InvokeByName(
"Count", // prop
flags,
null, // arguments
null); // Culture

thisXDocument.UI.Alert("Got " + commandBarsCount.ToString() + "
commandBars");
object[] args = new object[] {"Standard"};
object commandBarItem = commandBars.InvokeByName(
"Item",
flags,
args, // arguments
null); // Culture

thisXDocument.UI.Alert("Got the 'Standard' command bar");

// cast to the one of the supported interface as defined in
// Microsoft.Office.Interop.Core and access the controls collection
CommandBarControls controls = ((CommandBar)commandBarItem).Controls;
thisXDocument.UI.Alert("Spelling ID is " +
controls["Spelling..."].Id.ToString());
controls["Save"].Enabled = false;
controls["Save"].Visible = false;
EventInfo clickEvent =
controls["Spelling..."].GetType().GetEvent("Click");

---------
make sure to add a reference to Microsoft.Office.Core and put the
following using statement in:

using Microsoft.Office.Core;

The above code outlines how to use the "InvokeByName" reflection
method to access properties of an object represented by the
ObjectWrapper.

I was having problems trying to cast Microsoft.Office.Core.CommandBars
to thisApplication.ActiveDocument.CommandBars which is why I believe
you need to use the ObjectWrapper and its InvokeByName method.

ie:
Microsoft.Office.Core.CommandBars commandBars =
(Microsoft.Office.Core.CommandBars)thisApplication.ActiveWindow.CommandBars;

Gives me an "Invalid cast" exception.

You can do this however (not pretty, but it works)...

using Microsoft.Office.Core;
..
..
..
// get the commandbar object
ObjectWrapper obj = (ObjectWrapper)thisApplication.ActiveWindow.CommandBars;

// set flags to tell how to reflect the object (ie - get a property)
BindingFlags flags = BindingFlags.Default | BindingFlags.GetProperty;

// get the Application property (same as thisApplication)
object oApplication = obj.InvokeByName("Application", flags, null,
null);

// now we can cast it to objects in the Microsoft.Office.Core
namespace
// and c# will understand how to interact with them
Microsoft.Office.Core.CommandBars bars =
(Microsoft.Office.Core.CommandBars)((Microsoft.Office.Interop.InfoPath.SemiTrust.Application)oApplication).ActiveWindow.CommandBars;
bars["standard"].Controls["Print Preview"].Execute();

Hope this helps people trying to interact with InfoPath commandbars.

Mike.
 
Top