如何通过Excel VBA使用单词选择方法 [英] How to use Words selection methods through Excel VBA

查看:65
本文介绍了如何通过Excel VBA使用单词选择方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Word VBA中,您可以使用Selection.MoveLeft,Selection.MovRight等设置和移动光标位置.

In Word VBA you are able to set and move the cursor position by using the Selection.MoveLeft, Selection.MovRight etc...

但是,当尝试在Excel VBA中使用相同的方法时,出现错误消息对象不支持此属性或方法."

However while trying to use this same method in Excel VBA I get an error saying "Object doesnt support this property or method."

我已经导入了Word对象库参考.

I have imported the Word Object Library reference.

如何使用Excel应用程序上的VBA在Word文档上移动光标位置.任何帮助将不胜感激.

How am I able to move the cursor position on the Word document using VBA on the Excel application. Any help will be greatly appreciated.

代码:设置Doc = ActiveDocument.Content

Code: Set Doc = ActiveDocument.Content

    With Doc.Find
    .Execute FindText:="*", ReplaceWith:="NEW*"
    End With

    Selection.HomeKey Unit:=wdStory
    Selection.MoveDown Unit:=wdParagraph, Count:=11
    Selection.MoveRight Unit:=wdWord, Count:=4
    Selection.MoveRight Unit:=wdWord, Count:=2, Extend:=wdExtend
    Selection.Font.Bold = False
    Selection.Font.Name = "Arial"
    Selection.Font.Size = 9

推荐答案

在您的代码中,关键字选择"是指excel中当前选择的单元格,而不是Word文档中的光标位置.而且Excel Selection对象没有move方法.使用Range而不是选择可能会更成功:您的示例代码转换为

As your code stands the keyword "selection" refers to the currently selected cell in excel, not the cursor position in your Word document. And the Excel Selection object does not have move methods. You might have more success using Range rather than selection: You example code translates as

With ActiveDocument

    With .Content.Find
        .Execute FindText:="*", ReplaceWith:="NEW*"
    End With


     with .Paragraphs(4).Range.Words(5).Font
         .Bold = false
         .name = "Arial"
          .size = 9
      end with
       With .Paragraphs(4).Range.Words(6)
         .Bold = false
         .name = "Arial"
          .size = 9
      end with
end with

这篇关于如何通过Excel VBA使用单词选择方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆