2013-08-06 100 views
0

我有一个MS字中的宏,用于在从头中复制文件名后重命名文件。从剪贴板中粘贴文件名

我记录了这个宏,但是我正在做一个保存并粘贴文件名(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

,如果你想救你正在使用的文件,试试这个:

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 
+0

由于它的工作! – user2618115