2020-09-28

1 篇文章 0 订阅
这篇博客详细介绍了如何使用VBA在Microsoft Word中进行光标移动、选区选择以及内容删除等操作。包括移动到文档开头、结尾,当前行或段落的首尾,以及选择和删除特定行或段落的函数示例。此外,还提供了获取光标位置和显示选择区域起止位置的方法。
摘要由CSDN通过智能技术生成

下面的供参考:
移动光标至文档开始
Selection.HomeKey unit:=wdStory
Sub MoveToCurrentLineStart()
'移动光标至当前zhi行首
Selection.HomeKey unit:=wdLine
End Sub
Sub MoveToCurrentLineEnd()
'移动光标至当前行尾dao
Selection.EndKey unit:=wdLine
End Sub
Sub SelectToCurrentLineStart()
'选择从光标至当前行首的内容
Selection.HomeKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub SelectToCurrentLineEnd()
'选择从光标至当前行尾的内容
Selection.EndKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub SelectCurrentLine()
'选择当前行
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub MoveToDocStart()
'移动光标至文档开始
Selection.HomeKey unit:=wdStory
End Sub
Sub MoveToDocEnd()
'移动光标至文档结尾
Selection.EndKey unit:=wdStory
End Sub
Sub SelectToDocStart()
'选择从光标至文档开始的内容
Selection.HomeKey unit:=wdStory, Extend:=wdExtend
End Sub
Sub SelectToDocEnd()
'选择从光标至文档结尾的内容
Selection.EndKey unit:=wdStory, Extend:=wdExtend
End Sub
Sub SelectDocAll()
'选择文档全部内容(从WholeStory可猜出Story应是当前文档的意思)
Selection.WholeStory
End Sub
Sub MoveToCurrentParagraphStart()
'移动光标至当前段落的开始
Selection.MoveUp unit:=wdParagraph
End Sub
Sub MoveToCurrentParagraphEnd()
'移动光标至当前段落的结尾
Selection.MoveDown unit:=wdParagraph
End Sub
Sub SelectToCurrentParagraphStart()
'选择从光标至当前段落开始的内容
Selection.MoveUp unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub SelectToCurrentParagraphEnd()
'选择从光标至当前段落结尾的内容
Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub SelectCurrentParagraph()
'选择光标所在段落的内容
Selection.MoveUp unit:=wdParagraph
Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub DisplaySelectionStartAndEnd()
'显示选择区的开始与结束的位置,注意:文档第1个字符的位置是0
MsgBox ("第" & Selection.Start & "个字符至第" & Selection.End & "个字符")
End Sub
Sub DeleteCurrentLine()
'删除当前行
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.Delete
End Sub
Sub DeleteCurrentParagraph()
'删除当前段落
Selection.MoveUp unit:=wdParagraph
Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
Selection.Delete
End Sub
'上下左右移动光标位
private void moveLeft()
{
object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdWord;
object moveCount = 1;
object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
thisDocument.Application.Selection.MoveLeft(ref moveUnit, ref moveCount, ref MissingValue);
}
private void moveRight()
{
if(selection==null||selection!=document.Application.Selection)
selection=document.Application.Selection;
object dummy=System.Reflection.Missing.Value;
object count=1;
object Unit=Word.WdUnits.wdCharacter;
selection.MoveRight(ref Unit,ref count,ref dummy);
}
'取得当前光标位
public void GetCursor()
{
if(selection==null||selection!=document.Application.Selection)
selection=document.Application.Selection;
object a=selection.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);
object b=selection.get_Information(Word.WdInformation.wdFirstCharacterColumnNumber);
object c=selection.get_Information(Word.WdInformation.wdActiveEndAdjustedPageNumber);
MessageBox.Show(a.ToString()+”行,”+b.ToString()+”列,”+c.ToString()+”页”);
}
'定位到指定行或相对行
/// <summary>
/// 定位到指定行
/// </summary>
/// <param name=”lineNum”>行号</param>
private void gotoAbsolutLine(int lineNum)
{
if(selection==null||selection!=document.Application.Selection)
selection=document.Application.Selection;
object dummy=System.Reflection.Missing.Value;
object what=Word.WdGoToItem.wdGoToLine;
object which=Word.WdGoToDirection.wdGoToFirst;
object count=lineNum;
selection.GoTo(ref what,ref which,ref count,ref dummy);
}
/// <summary>
/// 定位到相对行,例如+4
/// </summary>
/// <param name=”lineNum”>行数</param>
private void gotoOppositeLine(int lineNum)
{
if(selection==null||selection!=document.Application.Selection)
selection=document.Application.Selection;
object dummy=System.Reflection.Missing.Value;
object what=Word.WdGoToItem.wdGoToLine;
object which;
if(lineNum<0)
which=Word.WdGoToDirection.wdGoToPrevious;
else
which=Word.WdGoToDirection.wdGoToNext;
object count=Math.Abs(lineNum);
selection.GoTo(ref what,ref which,ref count,ref dummy);
}
'定位到文档最后一行
private void gotoLastLine(Document thisDocument)
{
object dummy = System.Reflection.Missing.Value;
object what = WdGoToItem.wdGoToLine;
object which = WdGoToDirection.wdGoToLast;
object count = 99999999;
thisDocument.Application.Selection.GoTo(ref what, ref which, ref count, ref dummy);
}
'定位到第一个字符
private void gotoFirstCharacter()
{
if(selection==null||selection!=document.Application.Selection)
selection=document.Application.Selection;
int oldLine=0;
gotoAbsolutLine(1);
object a=selection.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);//得到当前行号
while(oldLine!=int.Parse(a.ToString()))//一直按右键,直到光标不再往下了为止
{
oldLine++;
moveRight();
a=selection.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);
}
gotoAbsolutLine(int.Parse(a.ToString()));
}
'定位到最后一个字符
public void gotoLastCharacter()
{
if(selection==null||selection!=document.Application.Selection)
selection=document.Application.Selection;
gotoLastLine();
object dummy=System.Reflection.Missing.Value;
object count=99999999;
object Unit=Word.WdUnits.wdCharacter;
selection.MoveRight(ref Unit,ref count,ref dummy);
}
' 取得行、列、页信息public string WordGetRCP(){
selection=document.Application.Selection;//wd.Selection;
object a=selection.get_Information(Word.WdInformation.wdFirstCharacterLineNumber);
object b=selection.get_Information(Word.WdInformation.wdFirstCharacterColumnNumber);
object c=selection.get_Information(Word.WdInformation.wdActiveEndAdjustedPageNumber);
return a.ToString()+”,”+b.ToString()+”,”+c.ToString();
}

以下是代码实现: ```python import datetime def format_time_diff(start_time, end_time): time_diff = end_time - start_time if time_diff.days > 365: return end_time.strftime("%Y年%m月") elif time_diff.days > 30: return end_time.strftime("%Y年%m月%d日") elif time_diff.days > 0: return f"{time_diff.days}天前" elif time_diff.seconds > 3600: return f"{int(time_diff.seconds/3600)}小时前" elif time_diff.seconds > 60: return f"{int(time_diff.seconds/60)}分钟前" elif time_diff.seconds > 0: return f"{time_diff.seconds}秒前" else: return "未来时间" start_time = datetime.datetime(2018, 3, 1, 9, 0, 0) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 1, 1, 9, 0, 0) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 2, 1, 9, 0, 0) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 2, 29, 8, 0, 0) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 2, 29, 9, 29, 20) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 2, 29, 9, 29, 50) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") start_time = datetime.datetime(2020, 2, 29, 9, 30, 40) end_time = datetime.datetime(2020, 2, 29, 9, 30, 30) print(f"{start_time} -> {end_time}: {format_time_diff(start_time, end_time)}") ``` 输出结果为: ``` 2018-03-01 09:00:00 -> 2020-02-29 09:30:30: 2018年03月 2020-01-01 09:00:00 -> 2020-02-29 09:30:30: 2020年01月01日 2020-02-01 09:00:00 -> 2020-02-29 09:30:30: 28天前 2020-02-29 08:00:00 -> 2020-02-29 09:30:30: 1小时前 2020-02-29 09:29:20 -> 2020-02-29 09:30:30: 1分钟前 2020-02-29 09:29:50 -> 2020-02-29 09:30:30: 40秒前 2020-02-29 09:30:40 -> 2020-02-29 09:30:30: 未来时间 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值