粘贴剪贴板中的文件名

时间:2013-08-06 10:58:04

标签: ms-word word-vba

我在MS word中有一个宏,用于在从标题中复制文件名后重命名文件。

我录制了宏但是在进行保存和粘贴文件名(Ctrl + V)时,宏正在对文件名进行硬编码。我想要从剪贴板中复制具有存储文件名称的内容。

请帮我根据需要更改代码。

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.MoveDown Unit:=wdLine, count:=2
    Selection.EndKey Unit:=wdLine
    Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
    Selection.Copy
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.EscapeKey
    ChangeFileOpenDirectory "C:\Documents and Settings\ssankees\Desktop\"
    ActiveDocument.SaveAs2 FileName:= _
        "KP27 Display Plan Data for Activity Types.doc", FileFormat:= _
        wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
        True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
        False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=0

1 个答案:

答案 0 :(得分:1)

如果要保存正在处理的文件,请尝试以下操作:

Dim DataObj As New MSForms.DataObject
DataObj.GetFromClipboard
Dim my_filename as String 

my_filename = DataObj.GetText

ActiveDocument.SaveAs2 FileName:= _
    my_filename, FileFormat:= _
    wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
    True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
    False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
    SaveAsAOCELetter:=False, CompatibilityMode:=0