2013-08-06 109 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