用excel vba 控制word 並將userform的資料填入word的表格中
- 帖子
- 47
- 主題
- 19
- 精華
- 0
- 積分
- 82
- 點名
- 0
- 作業系統
- win
- 軟體版本
- xp
- 閱讀權限
- 20
- 註冊時間
- 2014-7-4
- 最後登錄
- 2021-9-4
|
用excel vba 控制word 並將userform的資料填入word的表格中
本帖最後由 ciboybj 於 2019-7-30 22:25 編輯
大家好:)
我寫了一個小程式在Excel VBA中
主要是有一個Userform 裡面有一個『按鈕』,使用者按下『按鈕』後
vba會開啟同一路徑下名為『範本』的word檔
開啟word檔後,將尋找 word檔案中的『 編號』 字串,找到『 編號』 的字串後,向右邊移動兩個字元(移到隔壁的格子中),並插入userform中某一個標間的字串- 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
複製代碼 但是,我執行上面的程式碼後,出現了以下的錯誤訊息
想請問大家
為何有上面的錯誤訊息 我要怎麼修改程式才可以運作阿~~~{:3_50:}
test.rar (31.64 KB)
|
|
|
|
|
|
|
- 帖子
- 47
- 主題
- 19
- 精華
- 0
- 積分
- 82
- 點名
- 0
- 作業系統
- win
- 軟體版本
- xp
- 閱讀權限
- 20
- 註冊時間
- 2014-7-4
- 最後登錄
- 2021-9-4
|
2#
發表於 2019-8-1 21:18
| 只看該作者
目前找到的解決方式是:
在vba的工具列中選擇『工具』->『設定引用項目』
把Microsoft 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
複製代碼 由於在word中找到的字是在表格中,因此,要用wdCell 這個移動單位來移動鼠標位置 |
|
|
|
|
|
|