设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 3015|回复: 1
打印 上一主题 下一主题

【作业】05课-CA135B_Amas

[复制链接]
跳转到指定楼层
1#
发表于 2014-4-12 22:40:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. using Microsoft.Office.Tools.Ribbon;
  2. using Word = Microsoft.Office.Interop.Word;
  3. using System.Windows.Forms;


  4. namespace Lesson05
  5. {
  6.     public partial class Ribbon1
  7.     {
  8.         private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
  9.         {

  10.         }


  11.         //删除所有嵌入式图片
  12.         private void btnDelAllInlineShapes_Click(object sender, RibbonControlEventArgs e)
  13.         {
  14.             Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
  15.             if (doc.InlineShapes.Count > 0)
  16.             {
  17.                 foreach (Word.InlineShape shape in doc.InlineShapes)
  18.                 {
  19.                     shape.Delete();
  20.                 }
  21.             }
  22.             else
  23.             {
  24.                 MessageBox.Show("文档没有嵌入式图片");
  25.             }

  26.         }


  27.         //插入签名
  28.         private void btnInsertSignature_Click(object sender, RibbonControlEventArgs e)
  29.         {
  30.             Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
  31.             Word.Range range;

  32.             //判断当前文档段落数是否满足要求
  33.             if (doc.Paragraphs.Count < 3)
  34.             {
  35.                 //段落数小于3,提示在文档尾添加
  36.                 DialogResult result = MessageBox.Show("文档段落数小于3,\n是否在文档尾添加签名", "提示", MessageBoxButtons.YesNo);
  37.                 if (result == DialogResult.Yes)
  38.                 {
  39.                     range = doc.Range(doc.Content.End - 1, doc.Content.End - 1);
  40.                     range.InsertAfter("\n西西");
  41.                 }
  42.                 else
  43.                 {
  44.                     return;
  45.                 }
  46.             }

  47.                 //在第3段尾添加签名
  48.             else
  49.             {
  50.                 int iEnd = (doc.Paragraphs[3].Range.End)-1;
  51.                 doc.Range(0, iEnd).InsertAfter("西西");
  52.             }
  53.         }
  54.     }
  55. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2014-4-13 01:11:54 | 只看该作者
选作题:统一设置嵌入式图片大小




  1. using Microsoft.Office.Tools.Ribbon;
  2. using Word = Microsoft.Office.Interop.Word;
  3. using System.Windows.Forms;


  4. namespace Lesson05
  5. {
  6.     public partial class Ribbon1
  7.     {

  8.         //统一嵌入式图片大小
  9.         private void btnSetInlineShapesSize_Click(object sender, RibbonControlEventArgs e)
  10.         {
  11.             Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
  12.             if (doc.InlineShapes.Count > 0)
  13.             {
  14.                 frmSetShapesSize setDialog = new frmSetShapesSize();
  15.                 setDialog.ShowDialog();
  16.             }
  17.             else
  18.             {
  19.                 MessageBox.Show("文档中没有嵌入式图片");
  20.             }


  21.         }


  22.     }
  23. }
复制代码
  1. using System;
  2. using System.Windows.Forms;
  3. using Microsoft.Office.Interop.Word;
  4. using System.Text.RegularExpressions;

  5. namespace Lesson05
  6. {
  7.     public partial class frmSetShapesSize : Form
  8.     {
  9.         Document doc = Globals.ThisAddIn.Application.ActiveDocument;
  10.         float height, width;


  11.         public frmSetShapesSize()
  12.         {
  13.             InitializeComponent();
  14.             radioButton1.Checked = true;
  15.             textBox1.Enabled = true;
  16.             radioButton2.Checked = false;
  17.             textBox2.Enabled = false;
  18.         }

  19.         private void textBox1_TextChanged(object sender, EventArgs e)
  20.         {
  21.             if (textBox1.Text != "" && IsNumber(textBox1.Text))
  22.             {
  23.                 height = Convert.ToSingle(textBox1.Text);
  24.             }
  25.         }

  26.         private void textBox2_TextChanged(object sender, EventArgs e)
  27.         {
  28.             if (textBox2.Text != "" && IsNumber(textBox2.Text))
  29.             {
  30.                 width = Convert.ToSingle(textBox2.Text);
  31.             }
  32.         }

  33.         //设置图片大小
  34.         private void button1_Click(object sender, EventArgs e)
  35.         {
  36.             if (textBox1.Text == "" && textBox2.Text == "")
  37.             {
  38.                 MessageBox.Show("请输入高度和宽度");
  39.                 return;
  40.             }

  41.             foreach (InlineShape shape in doc.InlineShapes)
  42.             {
  43.                 shape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
  44.                 if (radioButton1.Checked)
  45.                 {
  46.                     shape.Height = height;
  47.                     shape.ScaleWidth = shape.ScaleHeight;
  48.                 }

  49.                 if (radioButton2.Checked)
  50.                 {
  51.                     shape.Width = width;
  52.                     shape.ScaleHeight = shape.ScaleWidth;
  53.                 }
  54.             }
  55.             this.Dispose();
  56.         }

  57.         private void radioButton1_CheckedChanged(object sender, EventArgs e)
  58.         {
  59.             textBox1.Enabled = radioButton1.Checked;
  60.             textBox2.Text = "";
  61.         }

  62.         private void radioButton2_CheckedChanged(object sender, EventArgs e)
  63.         {
  64.             textBox2.Enabled = radioButton2.Checked;
  65.             textBox1.Text = "";
  66.         }


  67.         //判断内容是否为数字函数
  68.         public bool IsNumber(String strNumber)
  69.         {
  70.             Regex objNotNumberPattern = new Regex("[^0-9.-]");
  71.             Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
  72.             Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
  73.             String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
  74.             String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
  75.             Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
  76.             return !objNotNumberPattern.IsMatch(strNumber) &&
  77.             !objTwoDotPattern.IsMatch(strNumber) &&
  78.             !objTwoMinusPattern.IsMatch(strNumber) &&
  79.             objNumberPattern.IsMatch(strNumber);
  80.         }

  81.       

  82.     }
  83. }
复制代码

测试环境:WIN7 + VS2010 + OFFICE2010




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-23 19:56 , Processed in 0.096757 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表