打开Word并插入图片

headfileName1 :图片完成路径

#region 打开Word并插入图片

      object readOnly = false;
     object isVisible = true;
     object saveDocument = true;
     object missing = System.Reflection.Missing.Value;

     //打开Word
     Word.ApplicationClass oWordApplic = null;
     Word.Document oDoc = null;
    
     try
     {
      try
      {
       oWordApplic = new Word.ApplicationClass();
       if (oWordApplic == null)
        MessageBox.Show("word为null");

       oDoc = oWordApplic.Documents.Open(ref strDocFile, ref missing,ref readOnly,
        ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing,
        ref missing, ref missing, ref isVisible);//成功打开Word文件!

      }
      catch(Exception ex)
      {
       if (oWordApplic != null)
        oWordApplic.Application.Quit(ref missing, ref missing, ref missing);

      }

      //插入图片
      if (headfileName1 != "" && oDoc != null)
      {
        // "准备插入图片1!"
       if (System.IO.File.Exists(headfileName1))
       {
        Word.Shape oShape1;

        if (width1.ToString() != "" && width1.ToString() != "0")
        {
         if (height1.ToString() != "" && height1.ToString() != "0")
          oShape1 = oDoc.Shapes.AddPicture(headfileName1,ref missing,ref saveDocument,ref left1,ref top1,ref width1,ref height1,ref missing);
         else
          oShape1 = oDoc.Shapes.AddPicture(headfileName1,ref missing,ref saveDocument,ref left1,ref top1,ref width1,ref missing,ref missing);
        }
        else
        {
         if (height1.ToString() != "" && height1.ToString() != "0")
          oShape1 = oDoc.Shapes.AddPicture(headfileName1,ref missing,ref saveDocument,ref left1,ref top1,ref missing,ref height1,ref missing);
         else
          oShape1 = oDoc.Shapes.AddPicture(headfileName1,ref missing,ref saveDocument,ref left1,ref top1,ref missing,ref missing,ref missing);
        }

        oShape1.WrapFormat.Type = Word.WdWrapType.wdWrapNone;
        oShape1.WrapFormat.AllowOverlap = 0;
       }
      }

            //保存Word
      if (oDoc != null)
      {
       oDoc.Save();
       oDoc.Close(ref saveDocument,ref missing,ref missing);

                        }
                    #endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值