user2618115
user2618115

Reputation: 5

Paste file name from clipboard

I have a macro in MS word to rename a file after copying the file name from the header.

I recorded the macro but while I am doing a save as and pasting the file name (Ctrl + V) the macro is hard coding the file name. I instead want to copy the content from the clip board which has the name of the file stored.

Please help me in changing the code as required.

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

Upvotes: 0

Views: 2057

Answers (1)

eyeinthebrick
eyeinthebrick

Reputation: 548

if you want to save the file you are working on, try this:

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

Upvotes: 1

Related Questions