自定义文档属性是存储与文档关联的唯一信息的好方法。例如,您可能有一个公司分配的文件编号,需要与文件一起存储。为此目的,定制属性非常适合该法案。

在将自定义属性添加到文档时,您可能会开始想知道是否存在一种简单的方法将它们从一个文档复制到另一个文档。

不幸的是,没有办法做到这一点。 (据我估计,此功能可以很好地补充管理器。)但是,您可以创建一个宏来为您执行复制。下面的宏可以做到这一点:

Sub CopyDocProps()

Dim dp() As DocumentProperty     Dim CustomPropCount As Integer     Dim i As Integer     Dim intResponse As Integer

If Windows.Count > 2 Then         MsgBox "There are more than two windows. Please " & _           "close the others and re-run the macro.", , _           "Too many windows"

Exit Sub     End If

On Error GoTo Err_Handler

intResponse = MsgBox("Are you currently in the source document?", _       vbYesNoCancel, "Copy Custom Properties")



If intResponse = vbNo Then Application.Run MacroName:="NextWindow"



CustomPropCount = ActiveDocument.CustomDocumentProperties.Count     ReDim dp(1 To CustomPropCount)



For i = 1 To CustomPropCount         Set dp(i) = ActiveDocument.CustomDocumentProperties(i)

Next i

Application.Run MacroName:="NextWindow"



For i = 1 To CustomPropCount         If dp(i).LinkToContent = True Then             ActiveDocument.CustomDocumentProperties.Add _               Name:=dp(i).Name, _               LinkToContent:=True, _               Value:=dp(i).Value, _               Type:=dp(i).Type, _               LinkSource:=dp(i).LinkSource         Else             ActiveDocument.CustomDocumentProperties.Add _               Name:=dp(i).Name, _               LinkToContent:=False, _               Value:=dp(i).Value, _               Type:=dp(i).Type         End If     Next i

MsgBox "The properties have been copied."

Exit Sub

Err_Handler:

' if Word raises an error, then allow the user     ' to update the custom document property          intResponse = MsgBox("The custom document property (" & _       dp(i).Name & ") already exists." & vbCrLf & vbCrLf & _       "Do you want to update the value?", vbYesNoCancel, _       "Copy Custom Properties")



Select Case Response         Case vbCancel             End         Case vbYes             ActiveDocument.CustomDocumentProperties(dp(i).Name).Value _               = dp(i).Value             Resume Next         Case vbNo             Resume Next     End Select End Sub

此代码是如何复制自定义属性的示例,但不是安全的。例如,它不会检查源文档中是否确实存在任何自定义属性;它只是假设有。但是,可以很容易地添加这种编码。

为了使用该宏,请确保仅打开源文档和目标文档,并且每个文档只应打开一个窗口。宏完成后,您将需要保存目标文档。

注意:

如果您想知道如何使用此页面(或_WordTips_网站上的任何其他页面)中描述的宏,我准备了一个包含有用信息的特殊页面。

_WordTips_是您进行经济有效的Microsoft Word培训的来源。

(Microsoft Word是世界上最流行的文字处理软件。)本技巧(1340)适用于Microsoft Word 97、2000、2002和2003。您可以为Word(Word 2007)的功能区界面找到此技巧的版本。和更高版本)在这里: