|
 |
สอบถามปัญหา การทำ word automation ของ c++ ใครรู้ช่วยตอบหนูที ช่วยด้วย ได้โปรด |
|
 |
|
|
 |
 |
|
คือเขียน ภาษา c++ อ่ะค่ะ ใช้ VC 2010 ถ้าใครมีความสามารถ คิด เรื่อง word automation ได้ช่วยตอบด้วยนะคะ
จะทำ replace word หรือ book mark อย่างใดอย่างหนึ่งค่ะ ตอนนี้ที่ทำได้คือ
-ได้ลองสร้างโปรแกรมมา มันสามารถ เขียน และสามารถบันทึกลงไปได้ โดยใช้ หลักการของ MSDN ของ Microsoft แบบ Word Automation และใช้ Lib ของ MSword.OLB และปัญหาที่เกิด มันไม่สามารถทำงานในส่วนของ Find And Replace ได้ โดยได้นำ code ที่เขียนขึ้นมาสอบถามตามนี้ค่ะ ใครรู้ช่วยบอกทีค่ะ
Code (C#)
// MSWordConsole.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "MSWordConsole.h"
#include "Capplication.h"
#include "CDocument0.h"
#include "CDocuments.h"
#include "CSelection.h"
#include "CFind.h"
#include "CWords.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
COleVariant m_True((short)TRUE), m_False((short)FALSE),
m_Long((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
CApplication oWordApp;
CoInitialize(0);
if (!oWordApp.CreateDispatch ("Word.Application", NULL))
{
AfxMessageBox("CreateDispatch failed.", MB_OK | MB_SETFOREGROUND);
}
//CDocuments myDocs(oWordApp.get_Documents ());
//CDocument0 myDoc;
//myDoc.AttachDispatch(myDocs.Open(COleVariant("C:\\Users\\kittiphan\\Desktop\\Sipa\\Report_Project_form.dot"),
// m_False,m_False,m_False,m_Long,m_Long,m_False,
// m_Long,m_Long,m_Long,m_Long,m_Long,m_Long,m_Long,m_Long ,m_Long ));
//Create a new document
CDocuments oDocs;
CDocument0 oDoc;
oDocs = oWordApp.get_Documents();
oDoc = oDocs.Add(m_Long, m_Long, m_Long, m_Long);
//Note for Word 2000: The Add method has 4 arguments in Word 2000. If
//you wrapped the classes from the Word type library (msword9.olb),
//modify the Add method to provide 4 optional arguments:
// oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt);
//Add text to the document
CSelection oSel;
oSel = oWordApp.get_Selection();
oSel.TypeText("one");
oSel.TypeParagraph();
oSel.TypeText("two");
oSel.TypeParagraph();
oSel.TypeText("three");
//Find text to the document
CFind oFind;
oFind = oWordApp.get_Selection();
int intFound = 0;
//while (oFind.get_Found()){
intFound++;
oFind.Execute(COleVariant("one"), m_False, m_True, m_False,
m_False, m_False, m_True, COleVariant((short)1), m_False,
COleVariant("aaaa"), COleVariant((short)2),
m_False, m_False, m_False, m_False);
//}
//THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
CDocument0 oActiveDoc;
oActiveDoc = oWordApp.get_ActiveDocument();
oActiveDoc.SaveAs(COleVariant("c:\\SampleDoc.doc"),
COleVariant((short)0),
m_False, COleVariant(""), m_True, COleVariant(""),
m_False, m_False, m_False, m_False, m_False,
m_False, m_False, m_False, m_False, m_False);
oWordApp.Quit(m_Long,m_Long,m_Long);
}
return nRetCode;
}
Tag : VS 2010 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2011-12-12 11:01:35 |
By :
roboto |
View :
1130 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|
|

|
Load balance : Server 02
|