Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi guys,

    i need a macro to copy the current location of a file being worked on in word to the clipboard. e.g. "c:\test.doc".

    I have done basic macros in the past but nothing, dare i say 'this advanced'

    Help would be greatly appreciated.

    Cheers in advance,
    BtizeF200.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Welcome to the Lounge!

    You can do it as follows:
    • Activate the Visual Basic Editor (Alt+F11).
    • In the Project Explorer on the left hand side, click anywhere in Normal.dot.
    • Select Insert | UserForm.
    • Select File | Remove UserForm1.
    • Click No when asked wether you want to export the userform before removing it.
    • This may seem weird, but it's used to make some code available to your macros.
    • Select Insert | Module.
    • Paste the following code into the module window:

      Sub Doc2Clip()
      Dim MyData As New MSForms.DataObject
      MyData.SetText ActiveDocument.FullName
      MyData.PutInClipboard
      Set MyData = Nothing
      End Sub
    • Switch back to Word.
    • If you wish, you can assign the macro to a custom toolbar button and/or a keyboard shortcut.

  3. #3
    New Lounger
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Works a treat,

    gave it a quick test and it pastes to the clipboard as expected:

    "C:\Documents and Settings\user1\Desktop\Doc5.doc"

    many thanks! looks like i found the right place to ask

  4. #4
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='766811' date='23-Mar-2009 23:31'][*]Select File | Remove UserForm1.[*]This may seem weird, but it's used to make some code available to your macros.[/quote]

    Yes, it does. What exactly does the removal of the (default?) UserForm do?

    Alan

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    The code uses the DataObject from the Microsoft Forms 2.0 Object Library, so it needs a reference to this library.
    The easiest way to create that reference is by inserting a userform into the VBE project. But since we don't need the userform itself, we can delete it immediately after creating it. This won't remove the reference.

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Got it! Thanks, neat "shortcut". I'd have gone the long way and manually created the reference.

    Alan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •