Copy the File name of active document into the clipboard

A

andreas

Dear Experts:

Is it possible to copy the file name of the active document into the
clipboard via VBA? Below macro performs this task BUT I would like the
macro to copy the file name into the clipboard WITHOUT first creating
a new document and copy the string from there.

Hope this is feasible. Help is much appreciated. Thank you very much
in advance. Regards, Andreas

Sub CopyActiveDocumentNameToClipboard()

Dim strName As String
strName = ActiveDocument.Name
Dim newDoc As Document


MsgBox ("this document's name is " & strName)

Set newDoc = Documents.Add

newDoc.Range.Text = strName

Selection.WholeStory
Selection.Copy
Selection.Collapse direction:=wdCollapseStart
 
J

Jay Freedman

Yes, the technique for assigning a string to the clipboard is shown in
http://www.word.mvps.org/FAQs/MacrosVBA/ManipulateClipboard.htm.

Pay special attention to the instruction at the to set a reference to the Forms
library before you start entering code in your macro, so you don't get a compile
error on the "Dim ... As DataObject" statement.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
P

Pesach Shelnitz

Hi Andreas,

If you don't want to deal with the requirements at the of the link provided
by Jay, you could use the following macro.

Sub CopyActiveDocumentNameToClipboard()
Dim myRange As Range

Set myRange = ActiveDocument.Range
myRange.Collapse Direction:=wdCollapseEnd
myRange.InsertAfter ActiveDocument.name
MsgBox "This document's name is " & myRange.Text
myRange.Copy
ActiveDocument.Undo 1
End Sub
 
A

andreas

Hi Andreas,

If you don't want to deal with the requirements at the of the link provided
by Jay, you could use the following macro.

Sub CopyActiveDocumentNameToClipboard()
    Dim myRange As Range

    Set myRange = ActiveDocument.Range
    myRange.Collapse Direction:=wdCollapseEnd
    myRange.InsertAfter ActiveDocument.name
    MsgBox "This document's name is " & myRange.Text
    myRange.Copy
    ActiveDocument.Undo 1
End Sub

Hi Pesach,

astonishing coding. Thank you very much for your terrific help. It
works. Regards, Andreas
 
A

andreas

Yes, the technique for assigning a string to the clipboard is shown inhttp://www.word.mvps.org/FAQs/MacrosVBA/ManipulateClipboard.htm.

Pay special attention to the instruction at the to set a reference to theForms
library before you start entering code in your macro, so you don't get a compile
error on the "Dim ... As DataObject" statement.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all
may benefit.













- Show quoted text -

Hi Jay,

It is working as desired. Thank you very much for your terrific help.
Regards, Andreas
 
M

Musawwir Spiegel

I am running MS Office 2007 Word (Home and Student) on a computer
running Windows 7 Professional. I read the MVP article to which you
(Jay) cited. Part of the article said that in order to use the code
it provided, one must do one of two things:

1. Have at least one UserForm in your project, or
2. In the VBA editor, go to Tools, References, and set a reference to
the "Microsoft Forms 2.0 Object Library"

I tried #2 first. However, when I went to the list of references,
although the list of available references was very long, there was no
reference to the "Microsoft Forms 2.0 Object Library". Was that
something that only was only present in XP, Vista, or an earlier
version of Word?

I then tried #1 after creating only a blank untitled user form. This
time the code worked.

Thank you, Jay, for providing this important service to us.

For years I continued to use Word 95 because I had mastered WordBasic
and didn't want to have to learn VBA. Now that I am learning VBA, I
have found a paucity of good textual materials to assist me.

I find the Object Browser difficult to use and poorly documented when
I find the object I am looking for. Calling up the help file produces
an enormous number of entries, seldom on point.

The only good text I have found is Mastering VBA for Microsoft Office
2007 Mastering VBA for Microsoft Office 2007 by Richard Mansfield
(Sybex). I would appreciate any other suggestions that you might
have.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top