[.NET C# - Word] 以字元為最小單位的移動

摘要:[.NET C# - Word] 以字元為最小單位的移動

 

01 using Microsoft.Office.Interop.Word;  
02   
03 public class WordHelper  
04 {  
05     private _Document m_Document = null;  
06     private _Application m_wordApplication = null;  
07   
08     /// <summary>
09     /// 以字元為最小單位的移動
10     /// </summary>
11     /// <param name="strMoveDirection">移動方向(Up:上 / Down:下 / Left:左 / Right:右)</param>
12     /// <param name="intCount">移動單位</param>
13     /// <param name="bolMoveType">移動方式(true:選取移動 / false:移動)</param>

14     private void MoveByCharacter(string strMoveDirection, int intCount, bool bolMoveType)
15     {
16         object oCharacter = WdUnits.wdCharacter;
17         object oConut = intCount;
18         object oMovetype = new object();
19
20         if (bolMoveType == true)
21         {
22             // 選取移動 (有按shift + 方向鍵的移動)
23             oMovetype = WdMovementType.wdExtend;
24         }

25         else
26         {
27             // 移動
28             oMovetype = WdMovementType.wdMove;
29         }

30
31         Selection selection = this.m_wordApplication.Selection;
32         if (strMoveDirection == "Up")
33         {
34             selection.MoveUp(ref oCharacter, ref oConut, ref oMovetype);
35         }

36         else if (strMoveDirection == "Down")
37         {
38             selection.MoveDown(ref oCharacter, ref oConut, ref oMovetype);
39         }

40         else if (strMoveDirection == "Left")
41         {
42             selection.MoveLeft(ref oCharacter, ref oConut, ref oMovetype);
43         }

44         else if (strMoveDirection == "Right")
45         {
46             selection.MoveRight(ref oCharacter, ref oConut, ref oMovetype);
47         }

48     }

49 }