广告广告
  加入我的最爱 设为首页 风格修改
首页 首尾
 手机版   订阅   地图  繁体 
您是第 6879 个阅读者
 
发表文章 发表投票 回覆文章
  可列印版   加为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.016927 second(s),query:16 Gzip disabled
本站由 瀛睿律师事务所 担任常年法律顾问 | 免责声明 | 本网站已依台湾网站内容分级规定处理 | 连络我们 | 访客留言