Click here to Skip to main content
15,886,737 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hello,

This would be the first time I've ever posted about a coding problem I have, usually I'm pretty good at researching the problem and finding my solutions but I'm at a loss now. My skills in programming have almost left me, so I guess you could deem me as a beginner for now till it comes back.

The problem:
I'm trying to create an embedded office document, that being excel into my windows form using the web browser control. Using this as a how to: http://support.microsoft.com/?scid=kb%3Ben-us%3B304662&x=11&y=11[^]

I can use a button event to open my file and the file will populate into my window. Now for my first problem though not as serious, I cannot figure out how to hide the Excel application. When my app opens the file into the web browser, Microsoft Excel still opens though it's blank with no workbook. I've tried many different methods to make this hidden, but I just can't seem to understand how the web browser is using the object, when I try to declare a new one it will compile fine but throws out an error when it gets used.

My second problem, once I can understand how to use this object that the web browser is using, or have to activate it into my own object, I need to be able to use the SaveAs(), I just can't save it because I'm unsure of what object is being used.


I appreciate any help, I haven't programmed in over a year so please bare with me, I may not understand too much lingo but I have no problems looking what you might have to say up to better understand it :) PS I'm using Office 2007 and running Windows 7
Sorry for the long post, hope I didn't leave anything out :cool:

~Nick

Here's my source code:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;


namespace TestIntegratedExcel
{
    public partial class Form1 : Form
    {
        private Object oDocument;
        // Contains a reference to the hosting application
        String strFileName;
        CSV_DAT csv_dat = new CSV_DAT("TestFile");

        public Form1()
        {
            InitializeComponent();
            this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Closed += new System.EventHandler(this.Form1_Closed);
        }
        private void button1_Click(object sender, System.EventArgs e)
        {

            //Find the Office document.
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            strFileName = openFileDialog1.FileName;
            //If the user does not cancel, open the document.
            if (strFileName.Length != 0)
            {
                Object refmissing = System.Reflection.Missing.Value;
                oDocument = null;
                axWebBrowser1.Navigate(strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
            }
        }
        public void Form1_Load(object sender, System.EventArgs e)
        {
            openButton.Text = "Browse";
            openFileDialog1.Filter = "Office Documents(*.xls, *.xlsx, *.csv)|*.xls;*.xlsx;*.csv";
            openFileDialog1.FilterIndex = 1;
        }
        public void Form1_Closed(object sender, System.EventArgs e)
        {
            oDocument = null;
        }
        public void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
        {
            //Note: You can use the reference to the document object to
            //      automate the document server.
            Object o = e.pDisp;
            oDocument = o.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, o, null);
            Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null);
            Object oName = o.GetType().InvokeMember("Name", BindingFlags.GetProperty, null, oApplication, null);
        }
        private void clearButton_Click(object sender, EventArgs e)
        {
            axWebBrowser1.Navigate("about:blank");
            strFileName = null;
        }
Posted
Comments
Dalek Dave 15-Jan-11 9:40am    
An excellently phrased question.

1 solution

Hi Nick,

Good to see your question. Thats kind of a question that even bothered me sometimes.

Well what you are asking needs some time of research with the code. But for now please checkout this article[^] for reference.

Good luck :thumbsup:
 
Share this answer
 
v2
Comments
Dalek Dave 15-Jan-11 9:41am    
Good Link.
MrNickP 15-Jan-11 17:42pm    
Thanks for the Link though sadly that's yet another walk through that I've been through over and over again, I just can't seem to figure out how to use the object to do what I want it to do Cry If I can just figure out how to use the workbook object that the web browser is using here or to even copy it to a new object of the same type I believe I can make it work. The same for the blank Excel instance that opens when I launch a file into my web browser.

I'll be checking back though periodically for any other suggestions anyone might have. I can work on a couple other things for now, thanks for looking into it Smile
MrNickP 15-Jan-11 17:43pm    
Hmm It didn't like the copy of the emoticons. NM the Cry and Smile then :O

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900