用宏来拷贝Word文件里面的自定义样式

来源:互联网 发布:java多线程死锁面试题 编辑:程序博客网 时间:2024/06/03 15:02

'使用下面的宏,可以把D:/db/a.doc文档的所有自定义样式拷贝到当前文档

'如何关闭打开的D:/db/a.doc,现在还不知道怎么会出错误,以后再看看.

Sub CopyMyStyles()
'
' 拷贝自定义样式 Macro
' 宏在 2005-03-10 由 胡丹 创建
'

'要应用的文档(当前文档)
Dim strDest As String
strDest = ActiveDocument.FullName

'模板文档位置
Dim strTemplete As String
strTemplete = "D:/db/a.doc"

Dim wrd As Application
Set wrd = GetObject(, "Word.Application")
wrd.Documents.Open (strTemplete)
wrd.Visible = True

Dim styleLoop As Style
 
On Error GoTo THEERROR

For Each styleLoop In wrd.ActiveDocument.Styles
    If styleLoop.BuiltIn = False Then
        Application.OrganizerCopy Source:=strTemplete, _
            Destination:=strDest, _
            Name:=styleLoop.NameLocal, _
            Object:=wdOrganizerObjectStyles
    End If
Next styleLoop

MsgBox "导入成功"
GoTo THELAST

THEERROR:
MsgBox "发生错误"

THELAST:
'Dim d As Document
'For Each d In wrd.Documents
'    If d.FullName = strTemplete Then
'        d.Close (wdDoNotSaveChanges)
'    End If
'Next d
'Set wrd = Nothing
End Sub

原创粉丝点击