Excel cannot be closed normally during Excel Automation

Source: Internet
Author: User

 

In recent projects, you need to use VC to Operate Excel for automatic output. During this period, you may encounter a problem on the Internet and cannot close Excel normally. After consideration, the summary is as follows:

1. Cause: Because the Range GetItem () function is used to obtain the content in the Excel table, the Excel process cannot be closed immediately after the program calls the Quit () function of _ Application, when the entire program exits, Excel will be closed. If you comment out the code that calls GetItem (), it can be disabled normally. Internal reasons are unknown.

2. Solution: use other methods instead of GetItem () to obtain the content in Excel. The following code describes how to read data:

 

View plaincopy to clipboardprint?
BOOL CAutomationExcel: GetValue (int iRow, int iColumnStart, int iColumnStop, CStringArray & strarray)
{
Int iElementCount = iColumnStop-iColumnStart + 1;
If (iElementCount <= 0)
Return FALSE;

CString strStart, strEnd;
_ Variant_t start, stop;

StrStart = GetItemName (iRow, iColumnStart );
Start. vt = VT_BSTR;
Start. bstrVal = strStart. AllocSysString (); // The _ bstr_t conversion is not required.

StrEnd = GetItemName (iRow, iColumnStop );
Stop. vt = VT_BSTR;
Stop. bstrVal = strEnd. AllocSysString ();

// Create a security array to read content from Excel
_ Variant_t varReturn;
VarReturn. vt = VT_ARRAY | VT_VARIANT;

SAFEARRAYBOUND sab [2]; // it must be a dimension.
Sab [0]. lLbound = 1;
Sab [0]. cElements = iElementCount;
Sab [1]. lLbound = 1;
Sab [1]. cElements = iElementCount;
VarReturn. parray = SafeArrayCreate (VT_VARIANT, 2, sab );

Range;
Range. AttachDispatch (m_Range.GetRange (start, stop ));
If (range. m_lpDispatch = NULL)
Return FALSE;

VarReturn = range. GetValue2 ();
Range. ReleaseDispatch ();

Strarray. RemoveAll ();
// Read data from the security array
CString str;
If (iElementCount = 1)
{
CVariantCast (varReturn). GetValue (str );
Strarray. Add (str );
}
Else
{
_ Variant_t tmp;
For (int I = 1; I <= iElementCount; ++ I)
{
Long indices [] = {1, I };
SafeArrayGetElement (varReturn. parray, indices, (void *) & tmp );

CVariantCast (tmp). GetValue (str );
Strarray. Add (str );
}
}

Return TRUE;
}
<Type = "application/x-shockwave-flash" width = "0" height = "0" src =" http://hi.images.csdn.net/js/blog/SyntaxHighlighter/Scripts/clipboard.swf "Flashvars =" clipboard = BOOL % 20 CAutomationExcel % 3A % 3 AGetValue (int % 20 iRow % 2 Cint % 20 iColumnStart % 2 Cint % 20% 09% 09% 09% 20% 20% 20% 09% 09% 09% 09% 09% 09% 09% 09 iColumnStop % 2 CCStringArray % 26% 20 strarray) % 0A % 09% 7B % 0A % 09% 09int % 20 iElementCount % 20% 3D % 20 iColumnStop % 20-% 20 iColumnStart % 20% 2B % 201% 3B % 0A % 09% 09if (iElementCount % 20% 3C % 3D % 200) % 0A % 09% 09% 09 return % 20 FALSE % 3B % 0A % 09% 0A % 09% 09 CString % 20 strStart % 2 CstrEnd % 3B % 0A % 09% 09_variant_t % 20 start % 2 cstop % 3B % 0A % 09% 09% 0A % 09% 09 strStart % 20% 3D % 20 GetItemName (iRow % 2 CiColumnStart) % 3B % 0A % 09% 09start. vt % 20% 3D % 20VT_BSTR % 3B % 0A % 09% 09start. bstrVal % 20% 3D % 20strStart. allocSysString () % 3B % 09% 2F % 2F % 20% E4 % B8 % 8D % E9 % 9C % 80% E8 % A6 % 81% E8 % BF % 9B % E8 % A1 % 8C_bstr_t % E8 % BD % AC % E6 % 8D % A2 % 0A % 09% 09% 0A % 09% 09 strEnd % 20% 3D % 20 GetItemName (iRow % 2 CiColumnStop) % 3B % 0A % 09% 09stop. vt % 20% 3D % 20VT_BSTR % 3B % 0A % 09% 09stop. bstrVal % 20% 3D % 20strEnd. allocSysString () % 3B % 0A % 09% 09% 0A % 09% 09% 2F % 2F % 20% E5 % 88% 9B % E5 % BB % BA % E5 % AE % 89% E5 % 85% A8 % E6 % 95% b0 % E7 % BB % 84% EF % BC % 8C % E4 % BB % 8 EExcel % E4 % B8 % AD % E8 % AF % BB % E5 % 8F % 96% E5 % 86% 85% E5 % AE % B9 % 0A % 09% 09_variant_t % 20 varReturn % 3B % 0A % 09% 09varReturn. vt % 20% 3D % 20VT_ARRAY % 20% 7C % 20VT_VARIANT % 3B % 0A % 09% 0A % 09% 09 SAFEARRAYBOUND % 20sab % 5B2% 5D % 3B % 09% 2F % 2F % 09% E5 % BF % 85% E9 % A1 % BB % E4 % B8 % BA % E7 % BB % B4 % 0A % 09% 09sab % 5B0% 5D. lLbound % 20% 3D % 201% 3B % 0A % 09% 09sab % 5B0% 5D. cElements % 20% 3D % 20 iElementCount % 3B % 0A % 09% 09sab % 5B1% 5D. lLbound % 20% 3D % 201% 3B % 0A % 09% 09sab % 5B1% 5D. cElements % 20% 3D % 20 iElementCount % 3B % 0A % 09% 09varReturn. parray % 20% 3D % 20 SafeArrayCreate (VT_VARIANT % 2C % 202% 2C % 20sab) % 3B % 0A % 09% 0A % 09% 09 Range % 20 range % 3B % 0A % 09% 09range. attachDispatch (m_Range.GetRange (start % 2 Cstop) % 3B % 0A % 09% 09if (range. m_lpDispatch % 20% 3D % 3D % 20 NULL) % 0A % 09% 09% 09 return % 20 FALSE % 3B % 0A % 09% 0A % 09% 09 varReturn % 09% 3D % 20range. getValue2 () % 3B % 0A % 09% 09range. releaseDispatch () % 3B % 0A % 09% 0A % 09% 09strarray. R

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.