急,大侠们帮忙!!!!!!!!!!!!!!!!!!!!!
我写了一段vc在word中添加表格的代码,是根据word中录制的宏写的,运行的时候有异常,不能添加,请帮忙!!!
是在mfc下onok()中的
_Application wordApp;
Documents docs;
_Document doc;
Shapes shapes;
Shape shape;
Range range;
Tables tables;
wordApp.CreateDispatch (_T("Word.Application"));
wordApp.SetVisible (TRUE);
docs=wordApp.GetDocuments ();
CComVariant Template(_T("D:\\Program Files\\Microsoft Office\\Templates\\1033\\ENNORMAL.DOT"));
CComVariant NewTemplate(false),DocumentType(0),Visible;
docs.Add(&Template,&NewTemplate,&DocumentType,&Visible);
//´Ó³ÌÐòÏòWORD·¢ËÍ×Ö·û
AfxMessageBox(_T("write some characters to the document"));
Selection sel=wordApp.GetSelection();
sel.TypeText (_T("插入文字As the computer industry’s reliance on component software increases"));
sel.TypeText (_T("插入文字As the computer industry’s reliance on component software increases"));
sel.TypeText (_T("插入文字As the computer industry’s reliance on component software increases"));
sel.TypeText (_T("插入文字As the computer industry’s reliance on component software increases"));
sel.TypeText (_T("插入文字As the computer industry’s reliance on component software increases"));
//以下是添加表格的句子
CComVariant Start(5), End(5);
range=doc.Range(&Start,&End);
tables=doc.GetTables();
CComVariant DefaultTableBehavior(0),AutoFitBehavior(2);
tables.Add(range,(long)3,(long)4,&DefaultTableBehavior,&AutoFitBehavior);
//以下是保存退出的代码,保存完了会有一个提示对话框,说这个程序遇到问题,困惑中
doc=wordApp.GetActiveDocument();
CComVariant FileName1(_T("C:\\Documents and Settings\\Administrator\\桌面\\aaa.doc"));
// CComVariant FileName1(_T("c:\\Hello.doc"));
CComVariant FileFormat(0);
CComVariant LockComments(false),Password(_T(""));
CComVariant AddToRecentFiles(true),WritePassword(_T(""));
CComVariant ReadOnlyRecommended(false),EmbedTrueTypeFonts(false);
CComVariant SaveNativePictureFormat(false),SaveFormsData(false);
CComVariant SaveAsAOCELetter(false);
CComVariant Encoding;
CComVariant InsertLineBreaks(false);
CComVariant AllowSubstitutions(false);
CComVariant LineEnding;
CComVariant AddBiDiMarks(false);
doc.SaveAs(&FileName1,&FileFormat,&LockComments,&Password,&AddToRecentFiles,&WritePassword,&ReadOnlyRecommended,
&EmbedTrueTypeFonts,&SaveNativePictureFormat,&SaveFormsData,&SaveAsAOCELetter, &Encoding,
&InsertLineBreaks, &AllowSubstitutions,&LineEnding, &AddBiDiMarks);
sel.ReleaseDispatch();
doc.ReleaseDispatch();
docs.ReleaseDispatch();
CComVariant SaveChanges(-1),OriginalFormat,RouteDocument(false);
wordApp.Quit(&SaveChanges,&OriginalFormat,&RouteDocument);
wordApp.ReleaseDispatch();
大家帮忙,多谢!