我在excel中有一个带有按钮的“用户窗体”。
单击该按钮时,VBA将打开Word文档并在Word表中找到一个字符串。
找到字符串时,光标应移至下一行,但不起作用。
这是我的文件。 https://drive.google.com/file/d/1dhGoWxdaBxL2WmqsfFT6wllJ5z1d9csH/view?usp=sharing
Private Sub CommandButton2_Click()
Dim path As String
path = ThisWorkbook.path & "\範本.docx"
'Debug.Print (path)
Set WordApp = CreateObject("Word.Application")
WordApp.Documents.Open (path)
WordApp.Visible = True
WordApp.Activate
WordApp.Selection.homekey unit:=6
WordApp.Selection.Find.Execute FindText:="編號" ', Forward:=True, Wrap:=wdFindStop
WordApp.Selection.MoveRight = 2
WordApp.Selection.InsertAfter = LB_Num.Caption
End Sub
答案 0 :(得分:0)
我发现这种方式可以工作。
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库!