在WORD中用VBA实现光标移动与内容选择

在WORD中如何用VBA宏语言选定一行、一段,删除一行、一段,移动光标至行首、行尾、段首、段尾等。请看以下内容。
Sub MoveToCurrentLineStart()
    '移动光标至当前行首
    Selection.HomeKey unit:=wdLine
End Sub
Sub MoveToCurrentLineEnd()
    '移动光标至当前行尾
    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
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
复制文件可以使用系统调用函数 `open()` 、`read()` 和 `write()` ,具体步骤如下: 1. 打开源文件,使用 `open()` 函数,返回源文件的文件描述符。 2. 打开目标文件,使用 `open()` 函数,返回目标文件的文件描述符。 3. 从源文件中读取数据并写入目标文件,使用 `read()` 函数和 `write()` 函数。 4. 关闭源文件和目标文件,使用 `close()` 函数。 下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #define BUFFER_SIZE 1024 int main(int argc, char *argv[]) { if (argc != 3) { printf("Usage: %s source_file target_file\n", argv[0]); return 1; } int source_fd = open(argv[1], O_RDONLY); if (source_fd == -1) { printf("Failed to open source file %s\n", argv[1]); return 1; } int target_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); if (target_fd == -1) { printf("Failed to create target file %s\n", argv[2]); return 1; } char buffer[BUFFER_SIZE]; ssize_t n; while ((n = read(source_fd, buffer, BUFFER_SIZE)) > 0) { if (write(target_fd, buffer, n) != n) { printf("Failed to write to target file %s\n", argv[2]); return 1; } } if (n == -1) { printf("Failed to read from source file %s\n", argv[1]); return 1; } if (close(source_fd) == -1) { printf("Failed to close source file %s\n", argv[1]); return 1; } if (close(target_fd) == -1) { printf("Failed to close target file %s\n", argv[2]); return 1; } printf("File %s has been successfully copied to %s\n", argv[1], argv[2]); return 0; } ``` 移动文件可以使用 `rename()` 函数,具体步骤如下: 1. 使用 `rename()` 函数将源文件移动到目标位置。 下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc != 3) { printf("Usage: %s source_file target_file\n", argv[0]); return 1; } if (rename(argv[1], argv[2]) == -1) { printf("Failed to move file from %s to %s\n", argv[1], argv[2]); return 1; } printf("File %s has been successfully moved to %s\n", argv[1], argv[2]); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值