Click here to Skip to main content
15,891,920 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I want to read a MS Word document file in which we have some tables. I want to read data from these tables. I can only read the first column of the table.

I want to read all of the columns in the tables as well as other text.

I am new to MFC.

--Thanks


the code for doc file read is:

The code for Code to open a doc word file	

char szFilter[] =
      "Word Files (*.*)|*.doc|Text Files (*.txt)|*.txt|All Files (*.*)|*.*||";

	CFileDialog	DataRead(TRUE, // TRUE for FileOpen, FALSE for FileSaveAs
		NULL, NULL,
		OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT,
		szFilter,
		NULL);

		int nFileRead = DataRead.DoModal();
		
		if(IDOK == nFileRead)
		{
			//Get file name for opening Excel file
			CString szFileName = DataRead.GetPathName();
			if(szFileName.IsEmpty())
				return;
			//Do not make Word visible
			CEzWordAutomation MsWord(FALSE);	
			MsWord.OpenWordFile(szFileName);
				
			int i;
			int nLineCount = MsWord.GetLineCount();
			
			CString szLine;
			CString szMessage;
				
			for(i=1; i<=nLineCount; i++)
			{
					szLine = MsWord.GetLine(i);
					szMessage = szMessage + szLine  ;
			}
			
			MsWord.CloseDocument(FALSE);
			MsWord.ReleaseWord();
			MessageBox(szMessage);
}

Code read doc file line by line
CString CWordAutomation::GetLine(int nLine)
{
	CString szLine = _T("");
	if(NULL  == m_pdispWordApp)
		return szLine;

	VARIANTARG varg1, varg2;
	int wdGoToLine = 3;		//MsWord constant
	int wdGoToAbsolute = 1;	//MsWord constant
	int wdLine = 5;			//MsWord constant
	int wdExtend = 1;		//MsWord constant
	
	//Got to line
	ClearAllArgs();
	if (!WordInvoke(m_pdispWordApp, L"Selection", &varg1, DISPATCH_PROPERTYGET, 0))
		return szLine;
	ClearAllArgs();
	AddArgumentInt2(L"What", 0, wdGoToLine);
	AddArgumentInt2(L"Which", 0, wdGoToAbsolute);
	AddArgumentInt2(L"Count", 0, nLine);
	if (!WordInvoke(varg1.pdispVal, L"GoTo", NULL, DISPATCH_METHOD, 0))
		return szLine;
	
	//Selection.HomeKey Unit:=wdLine
	ClearAllArgs();
	AddArgumentInt2(L"Unit", 0, wdLine);
	if (!WordInvoke(varg1.pdispVal, L"HomeKey", NULL, DISPATCH_METHOD, 0))
		return szLine;
	//Selection.EndKey Unit:=wdLine, Extend:=wdExtend
	ClearAllArgs();
	AddArgumentInt2(L"Unit", 0, wdLine);
	AddArgumentInt2(L"Extend", 0, wdExtend);
	if (!WordInvoke(varg1.pdispVal, L"EndKey", &varg2, DISPATCH_METHOD, 0))
		return szLine;
	ClearAllArgs();
	if (!WordInvoke(varg1.pdispVal, L"Text", &varg2, DISPATCH_PROPERTYGET, 0))
		return szLine;

	//Get text from varg2
	VARTYPE Type = varg2.vt;
	switch (Type) 
		{
			case VT_UI1:
				{
					unsigned char nChr = varg2.bVal;
					szLine = nChr;
				}
				break;
			case VT_I4:
				{
					long nVal = varg2.lVal;
					szLine.Format("%i", nVal);
				}
				break;
			case VT_R4:
				{
					float fVal = varg2.fltVal;
					szLine.Format("%f", fVal);
				}
				break;
			case VT_R8:
				{
					double dVal = varg2.dblVal;
					szLine.Format("%f", dVal);
				}
				break;
			case VT_BSTR:
				{
					BSTR b = varg2.bstrVal;
					szLine = b;
				}
				break;
			case VT_BYREF|VT_UI1:
				{
					//Not tested
					unsigned char* pChr = varg2.pbVal;
					szLine = *pChr;
				}
				break;
			case VT_BYREF|VT_BSTR:
				{
					//Not tested
					BSTR* pb = varg2.pbstrVal;
					szLine = *pb;
				}
			case 0:
				{
					//Empty
					szLine = _T("");
				}
			}

	
	return szLine;

}
Other function related to GetLine()

BOOL CWordAutomation::AddArgumentInt2(LPOLESTR lpszArgName, WORD wFlags, int i)
{
	AddArgumentCommon(lpszArgName, wFlags, VT_I2);
	m_aVargs[m_iArgCount++].iVal = i;
	return TRUE;
}

void CWordAutomation::ClearAllArgs()
{
	int i;
	
	for (i = 0; i < m_iArgCount; i++) 
	{
		if (m_awFlags[i] & DISPARG_NOFREEVARIANT)
			// free the variant's contents based on type
			ClearVariant(&m_aVargs[i]);
		else
			ReleaseVariant(&m_aVargs[i]);
	}

	m_iArgCount = 0;
	m_iNamedArgCount = 0;

}

void CWordAutomation::ClearVariant(VARIANTARG *pvarg)
{
	pvarg->vt = VT_EMPTY;
	pvarg->wReserved1 = 0;
	pvarg->wReserved2 = 0;
	pvarg->wReserved3 = 0;
	pvarg->lVal = 0;

}


please give me a possible solution.... how can i read a word rather than a line.


thank you
Posted
Updated 25-Sep-11 22:20pm
v4
Comments
André Kraak 23-Sep-11 5:06am    
Please share any relevant code with us, seeing the code might us help understand the problem you are facing.

If you wish to change your question use the Improve Question button.
Slacker007 23-Sep-11 6:50am    
Edited for readability and title.
LaxmikantYadav 23-Sep-11 7:14am    
Please share the code
Slacker007 27-Sep-11 6:12am    
I deleted all 3 of your comments, where you posted the same code as in your original question. You don't need to put code in your comments. Just in your original question. Thanks.

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