急,大侠们帮忙!!!!!!!!!!!!!!!!!!!!!

jikfed 2008-04-05 11:33:30
我写了一段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();
大家帮忙,多谢!
...全文
106 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jikfed 2008-04-05
  • 打赏
  • 举报
回复
漏掉了一点,在保存之前要是含有这段代码,就异常,注释了就没啦
但我这段代码还是需要的

sel = wordApp.GetSelection();

range = sel.GetRange();

InlineShapes inlineshapes = range.GetInlineShapes();

LPCTSTR FileName = "E:\\Pictures\\20060925144350249.jpg";

CComVariant LinkToFile(FALSE);

CComVariant SaveWithDocument(TRUE);

CComVariant RangeT;

RangeT.vt = VT_DISPATCH;

RangeT.pdispVal = range.m_lpDispatch;

inlineshapes.AddPicture(FileName,&LinkToFile,&SaveWithDocument,&RangeT);
jameshooo 2008-04-05
  • 打赏
  • 举报
回复
RangeT.pdispVal = range.m_lpDispatch 是直接赋值接口指针,没有增加引用计数;
RangeT = range.m_lpDispatch; 这个语句调用CComVariant::operate = (LPDISPATCH)赋值操作符,里面会自动增加引用计数。
jikfed 2008-04-05
  • 打赏
  • 举报
回复
RangeT.pdispVal = range.m_lpDispatch;
这句和那一句??
能不能帮忙解释一下啊,
com技术我不太懂

我知道addref是引用加一吧,那 RangeT.pdispVal = range.m_lpDispatch; 又是起什么作用
jameshooo 2008-04-05
  • 打赏
  • 举报
回复
缺少一个AddRef调用,应该直接使用 RangeT = (IDispatch*)range.m_lpDispatch;
否则,在某个场合会导致range对象提前释放自己。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧