我在 MSWord 中有一个包含名称、日期和非数字数据的表。我想编写一个宏来提取这些数据并使其成为当用户点击另存为时,建议的文件名以特定顺序排列数据,以句点分隔。
表格如下所示:
第一栏:
Date 04/10/13
Name 1 Arthur Z
Name 2 Bea Y
Title 1 Cars
第二栏:
Title 2 Boats
Company Burger King
Color Red
Name 3 Caroline X
我需要文件名采用以下格式:
Burger King.Red.Y.Bea.04-10-13.Arthur Z.(extension)
我的代码:
Sub FileSaveAs()
ActiveDocument.Fields.Update
ActiveDocument.Fields.Update
'Updated twice because some of the fields that need
'to be updated rely on fields below it and since it
'doesn't take too long I didn't bother figuring out
'how to make it update backwards--but if anyone knows
'how, please lmk
Dim r As Range
Set r = ActiveDocument.Range
Dim fld As Field
Dim iCnt As Integer
For Each fld In ActiveDocument.Fields
'All this field and highlight stuff is to edit the
'document down--I have all this done
If fld.Type = wdFieldFormTextInput Then iCnt = iCnt + 1
Next
If iCnt >= 1 Then
Dim Response As VbMsgBoxResult
Response = MsgBox("Delete notes and shading?", vbYesNo + vbQuestion)
If Response = vbYes Then
With r.Find
.Highlight = True
.Forward = True
While .Execute
r.Delete
Wend
End With
For Each fld In ActiveDocument.Fields
fld.Select
If fld.Type = wdFieldFormTextInput Then
fld.Unlink
End If
Next
With Dialogs(wdDialogFileSaveAs)
.Name = "Burger King.Red.Y.Bea.04-10-13.Arthur Z.docm"
.Show
End With
EndUndoSaver
Exit Sub
ElseIf Response = vbNo Then
With Dialogs(wdDialogFileSaveAs)
.Name = "Burger King.Red.Y.Bea.04-10-13.Arthur Z.docm"
.Show
End With
End If
EndUndoSaver
Exit Sub
ElseIf iCnt = 0 Then
With Dialogs(wdDialogFileSaveAs)
.Name = "Burger King.Red.Y.Bea.04-10-13.Arthur Z.docm"
.Show
End With
End If
Set fld = Nothing
End Sub