廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 6812 個閱讀者
 
發表文章 發表投票 回覆文章
  可列印版   加為IE收藏   收藏主題   上一主題 | 下一主題   
sfzvzfbv
個人文章 個人相簿 個人日記 個人地圖
小人物
級別: 小人物 該用戶目前不上站
推文 x1 鮮花 x127
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片
推文 x0
[Basic][求助] Excel VBA 自動另存Word檔案到指定路徑
如標題,我有一隻程式是用Excel VBA 去寫的,但需要以下要求:


程式碼必須在Excel VBA 裡面執 ..

訪客只能看到部份內容,免費 加入會員 或由臉書 Google 可以看到全部內容



[ 此文章被sfzvzfbv在2015-02-13 20:55重新編輯 ]


獻花 x0 回到頂端 [樓 主] From:臺灣中華電信股份有限公司 | Posted:2015-02-13 02:06 |
ebolaman 手機 會員卡
個人文章 個人相簿 個人日記 個人地圖
特殊貢獻獎

級別: 副版主 該用戶目前不上站
版區: 程式設計
推文 x38 鮮花 x458
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

說明

1. 用 ActiveWorkbook.Path 取得 Excel 活頁簿的所在資料夾
2. Excel 2010 要用副檔名 xlsm 儲存才能存 Macros (程式碼)
3. 開啟 Word 先用 instances = OpenWord() 返回 Word 的實體,操作完後用 CloseWord(instances) 關閉
4. 程式碼需要先加入參考 Microsoft Word XXX Object Library,如果用 2010 版 XXX 是 14.0
5. 底下程式碼開啟和 Excel 同資料夾的 Word 文件名稱 "Document1.docx"
6. 程式碼如和測試如附件所示,請將 test 資料夾放到 "C:\test\" 進行測試

圖片

▼ 如何執行 Macro


▼ Document1.docx 內容


▼ 執行 TestOpenWord() 結果


如果執行 TestGetCurrentWorkingDirectory() 你應該會看到 "C:\test\"

程式碼

複製程式
Option Explicit

' Test getting current working directory and opening word files.
'
' Note: Add the following references:
' Microsoft Word XXX Object Library
'
' Author: Shawn Chang
' Tested on Excel 2010 and Word 2010

Private Type WordInstances
    wordApp As Word.Application
    wordDoc As Word.Document
End Type

Public Sub TestGetCurrentWorkingDirectory()
    MsgBox GetCurrentWorkingDirectory()
End Sub

Public Sub TestOpenWord()
    ' Open word instances by document path
    Dim documentPath As String
    Dim instances As WordInstances
    
    documentPath = GetCurrentWorkingDirectory() & "\Document1.docx"

    instances = OpenWord(documentPath)

    ' Get first paragraph text
    Dim firstParagraphText As String
    
    firstParagraphText = GetWordParagraphText(instances.wordDoc, 1)
    
    ' Show first paragraph text
    MsgBox firstParagraphText
    
    ' Close word instances
    CloseWord instances
End Sub

Private Function GetCurrentWorkingDirectory() As String
    GetCurrentWorkingDirectory = ActiveWorkbook.Path
End Function

Private Function GetWordParagraphText(doc As Word.Document, paragraphIndex As Integer) As String
    Dim docParagraph As Word.Paragraph

    Set docParagraph = doc.Paragraphs(paragraphIndex)

    GetWordParagraphText = docParagraph.Range.Text
End Function

Private Function OpenWord(docPath As String) As WordInstances
    Dim instances As WordInstances

    With instances
        Set .wordApp = CreateObject("Word.Application")
        Set .wordDoc = GetObject(docPath)
    End With
    
    OpenWord = instances
End Function

Private Sub CloseWord(instances As WordInstances)
    With instances
        .wordDoc.Close
        .wordApp.Quit

        Set .wordApp = Nothing
        Set .wordDoc = Nothing
    End With
End Sub

參考

How to use Automation with Word 2002


本帖包含附件
zip test.zip   (2022-06-09 14:21 / 26 KB)   下載次數:8


[ 此文章被ebolaman在2015-02-16 16:32重新編輯 ]


My BOINC stats :

獻花 x1 回到頂端 [1 樓] From:台灣寬頻通訊顧問股份有限公司 | Posted:2015-02-16 16:25 |

首頁  發表文章 發表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.016771 second(s),query:16 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言