本文介绍了在C ++中到Delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

uses Windows, SysUtils, Variants, Forms, ComObj, OleServer, WordXP;
 
...
...
...
 
 
 
function GetPowerPointText(fName:String): String;
var
  PP, PRE, SLI: OleVariant;
  i, j: Integer;

begin
  Result:='';
 

 try
    PP:=CreateOleObject('PowerPoint.Application');
  except
    MessageBox(0,'PowerPoint not Install.','Error',MB_OK);
    Exit;
  end;
 
  // 읽어올 파일이 있는지 체크
 if not FileExists(fName) then begin
    MessageBox(0,'파일이 없습니다.','Error',MB_OK);
    Exit;
 end;
 
 // 생성한 파워포인트 어플리케이션에서 파일을 읽음 = 프리젠테이션을 Open
  PRE:=PP.Presentations.Open(fName,False,True,False);
 
  
 for i:=1to PRE.Slides.Count do begin
    SLI:=PRE.Slides.Item(i);
 
   for j:=1to SLI.Shapes.Count do begin

      if (SLI.Shapes.Item(j).HasTextFrame) then
        Result:=Result+#13#10+SLI.Shapes.Item(j).TextFrame.TextRange.Text;
      Application.ProcessMessages;
    end; // for j
  end; // for i
 
 
  SLI:=Unassigned;
  PRE:=Unassigned;
  PP.Quit;
  PP:=Unassigned;
end;

推荐答案

CoInitialize(NULL);

//------------PP:=CreateOleObject('PowerPoint.Application');-------

CLSID ClassID;
CLSIDFromProgID(TEXT("PowerPoint.Application"), &ClassID);

IUnknown *pUnk;
HRESULT hr = CoCreateInstance(ClassID, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnk);
if (hr != S_OK)
	return 0;

IDispatch *pDisp;
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDisp);
if (hr != S_OK)
{
	pUnk->Release();
	return 0;
}

//------------PRE:=PP.Presentations.Open(fName,False,True,False);
//Get Presentations first
OLECHAR* szPresentations = OLESTR("Presentations");
DISPID dispid_Presentations;
hr = pDisp->GetIDsOfNames(IID_NULL, &szPresentations, 1, LOCALE_USER_DEFAULT, &dispid_Presentations);
if (hr != S_OK)
{
	pDisp->Release();
	pUnk->Release();
	return 0;
}

DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};
VARIANT vResult;
IDispatch *pDispPresentations;
hr = pDisp->Invoke(dispid_Presentations, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dpNoArgs, &vResult, NULL, NULL);
if (hr != S_OK)
{
	pDisp->Release();
	pUnk->Release();
	return 0;
}
pDispPresentations = vResult.pdispVal;

//Open the file 
VARIANT vArgsOpen[4];
for (int i = 0; i < 4; ++i)
{
	VariantInit(vArgsOpen+i);
}
vArgsOpen[0].vt = VT_BSTR;
vArgsOpen[0].bstrVal = SysAllocString(L"I need to go out for lunch.ppt");;
vArgsOpen[1].vt = VT_BOOL;
vArgsOpen[1].boolVal = FALSE;
vArgsOpen[2].vt = VT_BOOL;
vArgsOpen[2].boolVal = TRUE;
vArgsOpen[3].vt = VT_BOOL;
vArgsOpen[3].boolVal = FALSE;

DISPPARAMS dpOpen;
dpOpen.cArgs = 4;
dpOpen.cNamedArgs = 0;
dpOpen.rgvarg = vArgsOpen;

IDispatch *pDispPresentation;
OLECHAR* szOpen = OLESTR("Open");
DISPID dispid_Open;
hr = pDispPresentations->GetIDsOfNames(IID_NULL, &szOpen, 1, LOCALE_USER_DEFAULT, &dispid_Open);
if (hr != S_OK)
{
	pDisp->Release();
	pUnk->Release();
	pDispPresentations->Release();
	return 0;
}
hr = pDispPresentations->Invoke(dispid_Open, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dpOpen, &vResult, NULL, NULL);
SysFreeString(vArgsOpen[0].bstrVal);
if (hr != S_OK)
{
	pDisp->Release();
	pUnk->Release();
	pDispPresentations->Release();
	return 0;
}
pDispPresentation = vResult.pdispVal;

pDispPresentation->Release();
pDisp->Release();
pUnk->Release();
pDispPresentations->Release();

CoUninitialize();
return 1;		



这篇关于在C ++中到Delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:44