Solved

How can I use ApplicationClass and DsoFramerControl together?

Posted on 2008-06-11
9
670 Views
Last Modified: 2013-12-17
I have embedded an instance of MS Word into a c# application using the DSOframer control from Microsoft.  How can I programmatically insert text into the embedded instance?

I've included a simple code snippet to demonstrate what I'm trying to do.
/* Note: I've broken some of the statements into multiple
         lines, hopefully this improves readability */
 
private void btnOpen_Click(object sender, EventArgs e)
{
    if (_openFileDialog == null)
    {
        _openFileDialog = new OpenFileDialog();
        _openFileDialog.Filter = "Microsoft Office Files|*.doc;*.docx;
            *.docm;*.rtf;*.xls;*.xlsx;*.xlsm;*.xlsb;*.csv;*.ppt;
            *.pptx;*.pptm;*.vsd;*.vdx|All Files|*.*";
    }
    DialogResult result = _openFileDialog.ShowDialog();
    if (result == System.Windows.Forms.DialogResult.OK)
    {
        try
        {
            axFramerControl1.Select();
            axFramerControl1.Open(_openFileDialog.FileName);
            Form1.ActiveForm.Text = "Host Word Test - " + axFramerControl1.DocumentName;
 
        }
        catch (Exception)
        {
            
        }
    } 
}
 
 
/* Here I'd like this button to insert text into the instance of word opened above */
ApplicationClass WordApp;
private void btnInsert_Click(object sender, EventArgs e)
{
    WordApp = new ApplicationClass();
    
    /* Do something here? The only way I've been able to do
       something is to use:
       object fileName = _openFileDialog.FileName;
       object readOnly = false;
       object isVisible = true;
       object missing = System.Reflection.Missing.Value;
       WordApp.Visible = true;
       WordApp.Documents.Open(ref fileName, 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);
       
       which opens up another window, and then is only in read-only mode
       */     
    
    // print to cursor location
    WordApp.Selection.TypeText("Insert1");
}

Open in new window

0
Comment
Question by:guxiyou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 21768522
Are you saying that this isn't doing anything?

      WordApp.Selection.TypeText("Insert1");
0
 

Author Comment

by:guxiyou
ID: 21769665
Well, if I use the code as is (ie with parts of the code commented out), then I get an error, "Object reference not set to an instance of an object." If I use the code with the commented lines back in, then it will open up a separate instance of MS Word (outside the application) and then insert into that instance.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 21772427
Is it WordApp or WordApp.Selection that is null?
0
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

Author Comment

by:guxiyou
ID: 21772817
It is the WordApp.Selection that is null. As the code is written there is no way for WordApp to know which document it is associated with.  In the commented out code I can use WordApp.Documents.Open(...) to solve this problem, but this opens a new instance, which is not the behavior that I want.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 21774566
So, you are using the DSOframer to open a Word document, using the file association?
0
 

Author Comment

by:guxiyou
ID: 21778939
I give the user a prompt to select which file they want to open, and then open the file they selected using the following command (as shown in the code snippet):

axFramerControl1.Open(_openFileDialog.FileName);
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 21779073
You can't use the separate ApplicationClass, you would need to get a reference to the embedded instance, and that I am not sure of how to do.
0
 

Author Comment

by:guxiyou
ID: 21779107
Yeah, thats the problem I'm hoping that someone can help with :)
0
 

Accepted Solution

by:
guxiyou earned 0 total points
ID: 21779430
Ok, I figured out the solution!! In order to get control of the embedded instance you use the following steps:

1. Open the file using axFramerControl
2. Cast axFramerControl into a DocumentClass
3. Cast the DocumentClass.Application to ApplicationClass

In case anyone is interested, I'm attaching another code snippet that shows it working.
ApplicationClass WordApp;
private void btnOpen_Click(object sender, EventArgs e)
{
    if (_openFileDialog == null)
    {
        _openFileDialog = new OpenFileDialog();
        _openFileDialog.Filter = "Microsoft Office Files|*.doc;*.docx;
               *.docm;*.rtf;*.xls;*.xlsx;*.xlsm;*.xlsb;*.csv;
               *.ppt;*.pptx;*.pptm;*.vsd;*.vdx|All Files|*.*";
    }
    DialogResult result = _openFileDialog.ShowDialog();
    if (result == System.Windows.Forms.DialogResult.OK)
    {
        try
        {
            axFramerControl1.Select();
            axFramerControl1.Open(_openFileDialog.FileName);
            Form1.ActiveForm.Text = "Host Word Test - " + 
                                    axFramerControl1.DocumentName;
 
        }
        catch (Exception)
        {
            
        }
	DocumentClass doc = DocumentClass)axFramerControl1.ActiveDocument;
	WordApp = (ApplicationClass)doc.Application;
    } 
}
 
private void btnInsert_Click(object sender, EventArgs e)
{
    WordApp.Selection.TypeText("Insert1");
}

Open in new window

0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question