【发布时间】:2013-04-10 21:10:26
【问题描述】:
我在 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
【问题讨论】:
-
您对其中的哪一部分有疑问?你知道任何VBA吗?如果是,到目前为止您尝试过什么?
-
我有,但不足以获取特定的文件名。到目前为止,我已经编写了足够多的代码来让它工作,这样当我单击“另存为”时,我可以将建议的文件名设为我想要的任何文本在宏中,比如“CRAZY.doc”。我试图弄清楚如何让宏使用表格自定义建议的文件名。
-
说实话相对容易。显示您拥有的代码,这会更容易......
-
发布您现有的代码:没有任何代码的问题通常在此处获得答案的机会要低得多。
-
好的,当然。谢谢。稍后将使用代码编辑帖子...