By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

Starting Word or Excel application via COM

P: n/a
Hi together,
I am using the following code in Visual Basic to open Word or Excel
applications:

Word:
Dim w As Word.Application
w = CType(CreateObject("Word.application"), Word.Application)
w.Application.Documents.Open("test.doc")
With w.Application.Selection
[... whatever ...]
End With
w.Application.ActiveDocument.SaveAs("test2.doc")
w.Documents.Close()
w.quit

Excel:
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
xlapp = CType(CreateObject("Excel.application"), Excel.Application)
xlbook = CType(xlapp.Workbooks.Add, Excel.Workbook)
xlsheet = CType(xlbook.Worksheets(1), Excel.Worksheet)
with xlssheet
[... whatever ...]
end with
xlapp.quit()

Now I'm trying to do the same thing in Visual C++ (Visual Studio .net 2003).

Word:
Interop::Word::Application* w=new Interop::Word::ApplicationClass();
Interop::Word::Document* wd =new Interop::Word::DocumentClass();

This starts a new instance of Word. OK.

Then I tried to open an existing document or to create a new one:
Interop::Word::Documents::Add
w->Application->Documents->Open
but these functions ask for a lot of parameters. I've got no idea what
to use. I tried to put some "NULL" in it, but it was not working.

The same problem with Excel:
Interop::Excel::Application* ex=new Interop::Excel::ApplicationClass();
starts Excel.
But everything like this is not working:
Interop::Excel::Workbook* wb=ex->add_NewWorkbook();
Interop::Excel::Workbook* wb=new Interop::Excel::Workbook();

I spent a lot of time searching MSDN and Google, but I did not find any
good example that was working.

Thank you for tips and/or examples, Bernd
Danke für Tips und Beispiele, Bernd

--
BM Computer-Services, Bergmannstr. 66, 10961 Berlin
Webdesign, Internet, Layout und Grafik
Tel.: 030/20649400, mobil 0175/7419517, Fax: 030/20649401
Web: http://www.bmservices.de, eMail: ko*****@bmservices.de
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Declare:
object missing = System.Type.Missing;

and then put "ref missing" instead of null.

- Abhijeet Dev
Andale Inc

"Bernd Muent" <ko*****@bmservices.de> wrote in message
news:41*********************@news.highwayone.de...
Hi together,
I am using the following code in Visual Basic to open Word or Excel
applications:

Word:
Dim w As Word.Application
w = CType(CreateObject("Word.application"), Word.Application)
w.Application.Documents.Open("test.doc")
With w.Application.Selection
[... whatever ...]
End With
w.Application.ActiveDocument.SaveAs("test2.doc")
w.Documents.Close()
w.quit

Excel:
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
xlapp = CType(CreateObject("Excel.application"), Excel.Application)
xlbook = CType(xlapp.Workbooks.Add, Excel.Workbook)
xlsheet = CType(xlbook.Worksheets(1), Excel.Worksheet)
with xlssheet
[... whatever ...]
end with
xlapp.quit()

Now I'm trying to do the same thing in Visual C++ (Visual Studio .net
2003).

Word:
Interop::Word::Application* w=new Interop::Word::ApplicationClass();
Interop::Word::Document* wd =new Interop::Word::DocumentClass();

This starts a new instance of Word. OK.

Then I tried to open an existing document or to create a new one:
Interop::Word::Documents::Add
w->Application->Documents->Open
but these functions ask for a lot of parameters. I've got no idea what to
use. I tried to put some "NULL" in it, but it was not working.

The same problem with Excel:
Interop::Excel::Application* ex=new Interop::Excel::ApplicationClass();
starts Excel.
But everything like this is not working:
Interop::Excel::Workbook* wb=ex->add_NewWorkbook();
Interop::Excel::Workbook* wb=new Interop::Excel::Workbook();

I spent a lot of time searching MSDN and Google, but I did not find any
good example that was working.

Thank you for tips and/or examples, Bernd
Danke für Tips und Beispiele, Bernd

--
BM Computer-Services, Bergmannstr. 66, 10961 Berlin
Webdesign, Internet, Layout und Grafik
Tel.: 030/20649400, mobil 0175/7419517, Fax: 030/20649401
Web: http://www.bmservices.de, eMail: ko*****@bmservices.de

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.