-
January 29th, 2010, 06:34 PM
#1
Excel Automation in C++
This the code I have from a book which I am not able to run. I have figured out other errors but it is still stuck in with linker errors.
I am using Excel 2002.
These are the linker errors:
Code:
------ Build started: Project: test, Configuration: Debug Win32 ------
Compiling...
testExcelDriver.cpp
Linking...
testExcelDriver.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall ExcelDriver::~ExcelDriver(void)" (??1ExcelDriver@@UAE@XZ) referenced in function _main
testExcelDriver.obj : error LNK2019: unresolved external symbol "public: void __thiscall ExcelDriver::SetActiveSheet(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?SetActiveSheet@ExcelDriver@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
testExcelDriver.obj : error LNK2019: unresolved external symbol "public: void __thiscall ExcelDriver::AddWorkbook(void)" (?AddWorkbook@ExcelDriver@@QAEXXZ) referenced in function _main
testExcelDriver.obj : error LNK2019: unresolved external symbol "public: void __thiscall ExcelDriver::OpenExcel(void)" (?OpenExcel@ExcelDriver@@QAEXXZ) referenced in function _main
testExcelDriver.obj : error LNK2019: unresolved external symbol "public: __thiscall ExcelDriver::ExcelDriver(void)" (??0ExcelDriver@@QAE@XZ) referenced in function _main
C:\Documents and Settings\Saket1\Desktop\C++ Practice\test\Debug\test.exe : fatal error LNK1120: 5 unresolved externals
Build log was saved at "file://c:\Documents and Settings\Saket1\Desktop\C++ Practice\test\Debug\BuildLog.htm"
test - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The rest of the code are:
--------------Imports.hpp---------------------------------
Code:
#import "C:\Program Files\Common Files\Microsoft Shared\office10\mso.dll" rename("DocumentProperties", "DocumentPropertiesXL") rename("RGB", "RBGXL")
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\vbe6ext.olb"
#import "C:\Program Files\Microsoft Office\Office10\EXCEL.EXE" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") rename("ReplaceText", "ReplaceTextXL") rename("CopyFile", "CopyFileXL") no_dual_interfaces
-----------------ExcelDriver.hpp---------------------------------------
Code:
#ifndef ExcelDriver_hpp
#define ExcelDriver_hpp
#include <iostream>
#include "imports.hpp"
class ExcelDriver
{
private:
Excel::_ApplicationPtr m_pXL;
public:
ExcelDriver();
ExcelDriver(const ExcelDriver& source);
virtual ~ExcelDriver();
Excel::_ApplicationPtr Application() const;
// Operations
void OpenExcel();
void CloseExcel();
void AddWorkbook();
void SetActiveSheet(const std::string& name);
};
#endif
------------------------ExcelDriver.cpp--------------------
Code:
#include "ExcelDriver.hpp"
// Constructors & destructor
ExcelDriver::ExcelDriver()
{ // Default constructor
}
ExcelDriver::ExcelDriver(const ExcelDriver& source)
{ // Copy constructor
}
ExcelDriver::~ExcelDriver()
{ // Destructor
}
// Selectors
Excel::_ApplicationPtr ExcelDriver::Application() const
{ // Return Excel application
return m_pXL;
}
// Modifiers
// Operations
void ExcelDriver::OpenExcel()
{ // Open Excel application
try
{
m_pXL.CreateInstance(L"Excel.Application");
m_pXL->Visible=VARIANT_TRUE;
}
catch(_com_error&)
{ // For now ignore error
}
}
void ExcelDriver::CloseExcel()
{ // Close Excel
try
{
m_pXL->Quit();
}
catch(_com_error&)
{ // For now ignore error
}
}
void ExcelDriver::AddWorkbook()
{ // Add a workbook
try
{
m_pXL->Workbooks->Add((long)Excel::xlWorksheet);
}
catch(_com_error&)
{ // For now ignore error
}
}
void ExcelDriver::SetActiveSheet(const std::string& name)
{ // Make a sheet active
Excel::_WorkbookPtr pWorkbook=m_pXL->ActiveWorkbook;
Excel::_WorksheetPtr pWorksheet=pWorkbook->Worksheets->GetItem(name.c_str());
pWorksheet->Activate();
}
-------------testExcelDriver.cpp---------------
Code:
#include "exceldriver.hpp"
int main()
{
CoInitialize(NULL);
ExcelDriver myFirst;
myFirst.OpenExcel();
myFirst.AddWorkbook();
myFirst.SetActiveSheet("Daniel");
CoUninitialize();
return 0;
}
-
January 30th, 2010, 05:44 AM
#2
Re: Excel Automation in C++
Are you compiling ExcelDriver.cpp?
This code seems like it shouldn't compile.
Originally Posted by Learned
Code:
void ExcelDriver::OpenExcel()
{ // Open Excel application
try
{
m_pXL.CreateInstance(L"Excel.Application");
m_pXL->Visible=VARIANT_TRUE;
}
catch(_com_error&)
{ // For now ignore error
}
}
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
January 30th, 2010, 09:00 AM
#3
Re: Excel Automation in C++
Yes it compiles fine. But why would you say that it should not compile? What change do you suggest?
-
January 30th, 2010, 10:33 AM
#4
Re: Excel Automation in C++
Originally Posted by Learned
Yes it compiles fine. But why would you say that it should not compile?
Because the code below is not valid C++, unless the operator -> has been overloaded:
Code:
m_pXL.CreateInstance(L"Excel.Application");
m_pXL->Visible=VARIANT_TRUE;
If operator -> has been overloaded, you need to tell us what it has been overloaded to do. Otherwise, you can't use "." and "->" at the same time in the context you are showing us.
"Visible" is just a member variable, and if you create an object, you access members and functions of that object using ".", so what would have been expected is:
Code:
m_pXL.Visible=VARIANT_TRUE;
If you have a pointer to an object, then you access members using "->". Then the expected code would have looked like this:
Code:
Excel::_ApplicationPtr* m_pXL;
//...
m_pXL->CreateInstance(L"Excel.Application");
m_pXL->Visible=VARIANT_TRUE;
As to the error, your linker is telling you that it cannot find those functions. This could mean that you do not have your project or makefile set up correctly to include the file(s) you're compiling.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|