ویژگی های سند سفارشی یک راه عالی برای ذخیره اطلاعات منحصر به فرد است که با یک سند مرتبط است. به عنوان مثال، ممکن است یک شماره سند اختصاص داده شده توسط شرکت داشته باشید که باید همراه با یک سند ذخیره شود. یک ویژگی سفارشی برای این منظور کاملاً مناسب است.
همانطور که ویژگی های سفارشی را به یک سند اضافه می کنید، ممکن است به این فکر کنید که آیا راه آسانی برای کپی کردن آنها از یک سند به سند دیگر وجود دارد. متاسفانه هیچ راهی برای این کار وجود ندارد. (به نظر من، این قابلیت میتواند به سازماندهنده اضافه شود.) با این حال، میتوانید یک ماکرو ایجاد کنید که کپی را برای شما انجام دهد. ماکرو زیر دقیقاً این کار را انجام می دهد:
Sub CopyDocProps()
Dim dp() As DocumentProperty
Dim CustomPropCount As Integer
Dim i As Integer
Dim iResponse 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
iResponse = MsgBox("Are you currently in the source document?", _
vbYesNoCancel, "Copy Custom Properties")
If iResponse = 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
iResponse = 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 iResponse
Case vbCancel
End
Case vbYes
ActiveDocument.CustomDocumentProperties(dp(i).Name).Value _
= dp(i).Value
Resume Next
Case vbNo
Resume Next
End Select
End Sub
این کد نمونه ای از نحوه کپی کردن ویژگی های سفارشی است، اما ضد گلوله نیست. به عنوان مثال، بررسی نمی کند که آیا واقعاً ویژگی های سفارشی در سند منبع وجود دارد یا خیر. فقط فرض می کند که وجود دارد. با این حال، چنین کدنویسی می تواند به راحتی اضافه شود.
برای استفاده از ماکرو، مطمئن شوید که فقط اسناد مبدأ و مقصد باز است و باید فقط یک پنجره در هر سند باز داشته باشید. وقتی ماکرو به پایان رسید، باید سند مورد نظر را ذخیره کنید.