none
Save a Word Document using Clipboard RRS feed

  • Question

  • Please, How can I get a Macro working to use the contents of the clipboard as the name of a new file I want to save. 

    We are using Microsoft Word 2007 here at work.  I can record a Macro that will select a person's name from places in the Word document and I want to use this person's name as the name of the new document and save it in a folder.  While recording a Macro, I select the name to get it to the clipboard,  click on "Save as," then I click on a folder, then I paste the contents of the clipboard on the "File name"  then I click save.

    But the resulting Maco puts the contents of the clipboard Permanantly in the File name and Will Not Allow A Variable.  So if the Macro is run again, it just saves to the same place again and again.

    I want to save three different files and use three different file names by selecting a whole line which will contain the peron's name or number. 

    Thanks a lot for your help, 

    And, I could use a good reference for learning this stuff too.  Do you know of any good books or online sources for a newbie?  And what reference is the most comprehensive with good examples?- Thanks!

    Wednesday, January 25, 2012 2:51 PM

Answers

  • Hi Stephen,

    You might try below code, the Folder is a hardcoded string, while the Filename will be the you Selection in the Document.

    Sub test()
    
    Dim strPath As String
    Dim strFileName As String
    
    'set pathname accordingly
    strPath = "\\mokansasc2s103\users\stephen.andre\0000AAAAAAARemedy Tickets\"
    
    'create the Filename with your selection in Document
    strFileName = Trim(Selection.Text) & ".docx"
    
    
    ActiveDocument.SaveAs FileName:= _
            strPath & strFileName _
            , FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
            AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
            EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
            :=False, SaveAsAOCELetter:=False
    
    
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Wednesday, February 15, 2012 12:55 AM
    Moderator

All replies

  • My advice would be to use the Find (Ctrl + F) when recording the macro instead of manually selecting something. This will allow you to replace the Find criteria in the code.

    Once you've recorded the macro, simply hit Alt+F11 to see the code.


    Try to look ahead so you're not forced to look behind.
    Wednesday, January 25, 2012 2:55 PM
  • Adam, the selection is recorded in the macro.  I will go copy it and paste it here.  This may take a while.  Thanks.


    Stephen.Andre
    • Edited by StephenAndre Wednesday, January 25, 2012 3:05 PM
    Wednesday, January 25, 2012 2:59 PM
  • Here it is, the document had "Joan Jones" on the third line and the Macro selected that.  But it put it in there permanantly.

    Sub Macro4()
    '
    ' Macro4 Macro
    '
    '
        Selection.HomeKey Unit:=wdStory
        Selection.MoveDown Unit:=wdLine, Count:=2
        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Selection.Copy
        ChangeFileOpenDirectory _
            "\\mokansasc2s103\users\stephen.andre\0000AAAAAAARemedy Tickets\"
        ActiveDocument.SaveAs FileName:= _
            "\\mokansasc2s103\users\stephen.andre\0000AAAAAAARemedy Tickets\Joan Jones.docx" _
            , FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
            AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
            EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
            :=False, SaveAsAOCELetter:=False
    End Sub


    Stephen.Andre
    • Edited by StephenAndre Wednesday, January 25, 2012 3:06 PM
    Wednesday, January 25, 2012 3:04 PM
  • Adam, I tried to figure out how to select a whole line using Cntl-F.  I couldn't see anything that works. 

    In this application, I want the Macro to go to the third line of the document and select and copy the whole line.  -Then use that name in the clipboard as the new File name and save it. 

    I also want the Macro to go the 1st line of the document and select and copy the whole line.  -Then use that name in the clipboard as the new File name and save it. 

    I also want the Macro to go the 24th line of the document and select and copy the whole line.  -Then use that name in the clipboard as the new File name and save it. 

     


    Stephen.Andre
    Wednesday, January 25, 2012 3:46 PM
  • Hi StephenAndre,

    Welcome to the MSDN forum.

    This queue is about Visual Basic in Visual Studio. I’m afraid that your topic about Marco is unsuitable here. For better support, I will move this thread to Microsoft Visual Basic for Applications (VBA) Forum.

    Sorry for any inconvenience and have a nice day.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 30, 2012 5:39 AM
    Moderator
  • Thanks for directing me to the right place. 

    What books should I study to figure this out?   For Microsoft Office 2010, WORD & EXCEL both use VBA 7.0  

    Is this the same ?  I am interested in mostly WORD VBA. 


    Stephen.Andre
    Monday, January 30, 2012 1:27 PM
  • Hi Stephen,

    You might try below code, the Folder is a hardcoded string, while the Filename will be the you Selection in the Document.

    Sub test()
    
    Dim strPath As String
    Dim strFileName As String
    
    'set pathname accordingly
    strPath = "\\mokansasc2s103\users\stephen.andre\0000AAAAAAARemedy Tickets\"
    
    'create the Filename with your selection in Document
    strFileName = Trim(Selection.Text) & ".docx"
    
    
    ActiveDocument.SaveAs FileName:= _
            strPath & strFileName _
            , FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
            AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
            EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
            :=False, SaveAsAOCELetter:=False
    
    
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Wednesday, February 15, 2012 12:55 AM
    Moderator
  • I'll try it, thanks, Daniel.  I will get back to you later.

    Stephen.Andre

    Wednesday, February 15, 2012 1:08 PM
  • Yippieieieieeeee!!!!!!!!!!!!!!!

    It finally worked.  This works:    C:\Name\SomeName\etc 

                                Not this:          \\Computername\Name\SomeName\etc

    And

    If InStr(strFileName, vbCr) <> 0 Then
    strFileName = Trim(Left(strFileName, InStr(strFileName, vbCr) - 1))
    End If

    Somebody told me to put this code in to take off the Paragraph Mark at the end

    thanks loads!!!!!!!!!! ---Steve from Kansas


    Stephen.Andre

    Tuesday, February 21, 2012 8:18 PM
  •  Thanks, I have just used this code to save a lot of time. I was wondering if it would be possible to expand the principle so that two areas of text could be highlighted and used in the file name i.e. Letter Reference and the Subject.

    In this case the filename would be;

    Letter Reference & Subject & .doc

    Thanks for your time

    Rodger Talty

    Wednesday, July 18, 2012 1:07 PM