Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用;选择“;在word中使用excel中的VBA代码移动光标?_Excel_Vba_Ms Word - Fatal编程技术网

如何使用;选择“;在word中使用excel中的VBA代码移动光标?

如何使用;选择“;在word中使用excel中的VBA代码移动光标?,excel,vba,ms-word,Excel,Vba,Ms Word,我在excel中有一个“Userform”,它有一个按钮 单击按钮时,VBA将打开Word文档并在Word表中找到字符串 查找字符串时,光标应移动到下一行,但它不起作用 这是我的文件。 我发现这种方法可行 Private Sub CommandButton1_Click() Dim str As String str = TextBox1.Text Dim WordApp As Word.Application Set WordApp = New Word.A

我在excel中有一个“Userform”,它有一个按钮

单击按钮时,VBA将打开Word文档并在Word表中找到字符串

查找字符串时,光标应移动到下一行,但它不起作用

这是我的文件。


我发现这种方法可行

Private Sub  CommandButton1_Click()
    Dim str As String
    str = TextBox1.Text
    Dim WordApp As Word.Application
    Set WordApp = New Word.Application

    WordApp.Documents.Open ThisWorkbook.Path & "\test.docm"
    WordApp.Visible = True

    WordApp.Selection.Find.Execute FindText:="編號"
    WordApp.Selection.Move Unit:=wdCell, Count:=1
    WordApp.Selection.InsertAfter str
    WordApp.Documents.Save

    Set WordApp = Nothing
End Sub
并且需要先添加Microsoft word库


使用
WordApp.Selection.MoveRight 2
(删除
=
)。我使用
WordApp.Selection.Move Unit:=wdCell,Count:=1
,但它仍然无法转到下一个单元格。这是一个英语场地,错误信息应以纯文本形式提供,而不是以图像形式提供。请使用问题下的链接提供错误消息的英文翻译,或在日语场所提问。FWIW您没有正确使用
MoveRight
。我建议你仔细阅读语言参考资料,了解如何使用它。您需要指定参数名称或按正确顺序使用参数。我的建议是同时使用
单位
计数
参数,以便VBA清楚了解您需要什么。你所说的都是
2
——但是有两个什么?在VBA项目中添加了对Word的引用?否则,您应该声明正在使用的常量。
Private Sub  CommandButton1_Click()
    Dim str As String
    str = TextBox1.Text
    Dim WordApp As Word.Application
    Set WordApp = New Word.Application

    WordApp.Documents.Open ThisWorkbook.Path & "\test.docm"
    WordApp.Visible = True

    WordApp.Selection.Find.Execute FindText:="編號"
    WordApp.Selection.Move Unit:=wdCell, Count:=1
    WordApp.Selection.InsertAfter str
    WordApp.Documents.Save

    Set WordApp = Nothing
End Sub