Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / General MS InfoPath Questions / July 2004

Tip: Looking for answers? Try searching our database.

Invoke Print Preview from c# code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike Surface - 12 Jul 2004 18:21 GMT
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#
Mike Surface - 13 Jul 2004 00:35 GMT
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.

> Has anyone figured this out yet?
>
[quoted text clipped - 26 lines]
> ps thisApplication.ActiveWindow.CommandBars("standard").Controls("Print
> Preview").Execute() does not work in c#
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.