vba怎樣打開word
① 用vba打開word文檔
1 遍歷已經打開的word對象的文件名,如果跟你要打開的一致,那就是已經打開了。2 打開新文件,監控打開新文件時word的提示,如果有文件已打開的提示,那也是已經打開了。
② excel中如何通過VBA打開word文件和ppt文件
一、打開word文件代碼:Set wo = CreateObject("Word.Application")
wo.Documents.Open ThisWorkbook.Path & "\流程.doc"
wo.Visible = True
二、打開ppt文件代碼:方法1:
Set wo = CreateObject("Powerpoint.Application")
wo.Visible = True
wo.Presentations.Open ThisWorkbook.Path & filename方法2:Sub dd()Dim filepath$, filename$
filepath = Chr(34) & ThisWorkbook.Path & filename & Chr(34)
Shell "POWERPNT.EXE " & filepathEnd Sub附:雙擊打開PPS文件,在演示完後退出PPS時並沒有PowerPoint主窗口保留,但在Excel中使用VBA打開的PPS文件,在演示完PPS退出後,PowerPoint主窗口仍然打開。
這里使用一個循環判斷演示窗口是否存在,加上錯誤捕捉程序來處理上面這個問題。
PrivateSub CommandButton1_Click()
Dim wo AsObject Dim app AsObject
' 創建PowerPoint應用實例
Set app = CreateObject("Powerpoint.Application")
' 使PowerPoint可見
app.Visible = True ' 打開PPS文件
Set wo = app.Presentations.Open(ThisWorkbook.Path & "\a.pps")
' 當PPS演示結束時,wo對象的SlideShowWindow不存在,捕捉到錯誤
OnErrorGoTo errHandle
' PPS演示時全屏
③ word 如何進入VBA
第一步:打開word,如果功能區有開發工具選項,跳到步驟四,如果沒有,繼續
第二步:打開【文件】選項,點選【選項】
第三步:選擇「自定義功能區」中點選「開發工具」,然後點擊確定
第四步:點擊菜單欄的【開發工具】,點擊「Visual
Basic」選項,完成。
④ 如何在EXECL中用VBa打開Word,並輸出數據到WORD中,保存,關閉
1、首先打開EXECL表格,然後在工作表中,點擊菜單欄【開發工具】。
⑤ 用vba打開word模板並修改後保存
1、打開Word文件的 VBA編輯器,快捷鍵 Alt+F11,右擊【ThisDocument】-》 【插入模塊】;
⑥ 怎樣用VBA打開WORD文檔
Fx="D:\文件.doc"
Set Wd = CreateObject("word.application")
Wd.Documents.Open mPath & "\" & Fx
⑦ 如何用vba代碼打開文件夾內的word文檔
Sub Read_Word()
Dim worDoc As object
Dim wordappl As object
Dim mydoc As String
Dim myappl As String
mydoc = thisworkbook.path & "\" & "文件名.doc"
'本文檔目錄下的doc文件,這里可以直接改成路徑+文件名的形式
Set wordappl = CreateObject("Word.application")'設置wordappl對象
Set worDoc = wordappl.Documents.Open(mydoc)
'打開word文檔,mydoc變數指定路徑和文件名
worDoc.Activate'激活打開的文檔
wordappl.Selection.WholeStory '全選文檔
wordappl.Selection.Copy'復制選擇內容到剪貼板
worDoc.Application.Quit'關閉word文檔
Set WordApp = Nothing'釋放對象變數的內存
Workbooks(1).Sheets(2).Activate '激活excel第一個工作簿的第二個工作表
ActiveSheet.UsedRange.Clear'把當前工作表清空,如果有重要數據,這條刪除
Cells(1, 1).Select'選擇A1單元格
ActiveSheet.Paste'粘貼復制的內容
wordappl.quit
set wordappl =nothing
End Sub
⑧ 如何在Excel VBA 中讀寫word文檔 步驟
1.庫的配置
在默認情況下,新創建的excel vba中不支持定義word對象。
所以需要先引入word庫,操作步驟如下:
1.1 打開excel vba 界面
1.2 選中其中的一個Mole
1.3 選擇菜單, Tools --> References
在打開的對話框中選擇類似 "Microsoft Word 14.0 Object Library".
1.4 點擊OK保存配置。
2. 打開文檔
Set wordApplication = CreateObject("Word.Application")
wordApplication.Visible = False
Dim hasOpenDoc As Boolean
hasOpenDoc = IsOpen(filePath) ' is a self-defined function to check file is opend
If hasOpenDoc = True then
Set wordDoc = GetObject(filePath)
End if
If hasOpenDoc = False Then
Set wordDoc = wordApplication.Documents.Open(filePath)
End if
wordDoc.Active
With wordApplication
Dim aParagraph As Word.Paragraph
For Each aParagraph In wordDoc.Paragraphs
' do some thing to every paragraph.
Next aParagraph
End with
wordDoc.Close
Set wordDoc = nothing
' 如下這段代碼引用某位牛人的,非常感謝他。由於路徑丟失,不能給出鏈接, 抱歉
' 如下的找尋方式,能夠正確的找出文件是否被打開
Function IsOpen(fileName As String) As Boolean
IsOpen = False
Dim findFile As Integer
findFile = FreeFile()
On Error GoTo ErrOpen
Open fileName For Binary Lock Read Write As findFile
Close findFile
Exit Function
ErrOpen:
If Err.Number <> 70 Then
Msg = "Error # " & Str(Err.Number) & "was generated by " & Err.Source & Chr(13) & Err.Description
MsgBox Msg, "Error", Err.HelpFile, Err.HelpContext
Else
IsOpen = True
End If
End Function