need code to copy paste extended

C

Co

Hi All,

I need some VBA code that will Copy a highlighted part of a document
to the clipboard including
the name of my document (ActiveDocument.Name).

So when I paste it in a new blanc document I will have the highlighted
text and at the end of it my filename, like:

"This is my highlighted text which was copied from another document".
MySourceDoc.20071018.doc

Marco
 
S

Steve Yandl

Marco,

You need a reference to the Microsoft Forms object library. Just insert a
UserForm that won't get used and then go back to a module to create the
subroutine. The sub between the lines should then do what I think you're
asking to do.

______________________________

Sub CopyPlusDocName()
Dim myDataObject As DataObject
Dim strPlus As String
strPlus = Selection.Text & ActiveDocument.Name
Set myDataObject = New DataObject
myDataObject.SetText strPlus
myDataObject.PutInClipboard
End Sub
______________________________

Steve
 
C

Co

Marco,

You need a reference to the Microsoft Forms object library. Just insert a
UserForm that won't get used and then go back to a module to create the
subroutine. The sub between the lines should then do what I think you're
asking to do.

______________________________

Sub CopyPlusDocName()
Dim myDataObject As DataObject
Dim strPlus As String
strPlus = Selection.Text & ActiveDocument.Name
Set myDataObject = New DataObject
myDataObject.SetText strPlus
myDataObject.PutInClipboard
End Sub
______________________________

Steve

Steve,

I don't quite understand.
I inserted a UserForm1 and then ran your code but it doesn't Paste
anywhere.

Marco
 
S

Steve Yandl

It copies the text that was selected in your document appended with the name
of the active document. You didn't specify a target where you wanted it
pasted so I assumed you would be pressing Ctrl plus v or using some other
method to paste the contents into some other Word document or other
application.

If your intent was to send the text to a different word document or some
text log file, there are better ways to go about it than involving the
Windows clipboard. What is the ultimate goal?

Steve
 
C

Co

It copies the text that was selected in your document appended with the name
of the active document. You didn't specify a target where you wanted it
pasted so I assumed you would be pressing Ctrl plus v or using some other
method to paste the contents into some other Word document or other
application.

If your intent was to send the text to a different word document or some
text log file, there are better ways to go about it than involving the
Windows clipboard. What is the ultimate goal?

Steve

The goal is to manually select a part of text in my word document.
Then copy that text together with the name of the document into
another (new)
word document, like:

"This is the text that I am going to copy into a new
Document including the name of the source document"
THE.NAME.OF.MY.SOURCE.DOC.20071019.doc

Marco
 
S

Steve Yandl

The subroutine below assumes that the selected text plus the name of the
document (and a space between the two strings) will be appended to a Word
document named "C:\Test\myLog.doc" and that myLog.doc is not already open.
The clipboard isn't being utilized so there is no need for the forms object
library to be available.

_____________________________________

Sub LotTxtSelection()
Dim strTrans As String
Dim objDoc As Word.Document
strTrans = Selection.Text & " " & ActiveDocument.Name
Set objDoc = Documents.Open("C:\Test\myLog.doc")
objDoc.Activate
With Selection
.WholeStory
.MoveRight
.TypeParagraph
.TypeText strTrans
End With
objDoc.Save
objDoc.Close
End Sub

_____________________________________

Steve
 
C

Co

The subroutine below assumes that the selected text plus the name of the
document (and a space between the two strings) will be appended to a Word
document named "C:\Test\myLog.doc" and that myLog.doc is not already open.
The clipboard isn't being utilized so there is no need for the forms object
library to be available.

_____________________________________

Sub LotTxtSelection()
Dim strTrans As String
Dim objDoc As Word.Document
strTrans = Selection.Text & " " & ActiveDocument.Name
Set objDoc = Documents.Open("C:\Test\myLog.doc")
objDoc.Activate
With Selection
.WholeStory
.MoveRight
.TypeParagraph
.TypeText strTrans
End With
objDoc.Save
objDoc.Close
End Sub

_____________________________________

Steve

Looks great Steve,

But could this also be done with a new document which is already
opened
and of which we do not know the name?

Marco
 
J

Jean-Guy Marcil

Co was telling us:
Co nous racontait que :

Looks great Steve,

But could this also be done with a new document which is already
opened
and of which we do not know the name?

Marco

You mean transferring the name of "Document A" and some selected text in
"Document A" into a already opened document?

You need code to determine which other opened document to target. What if
there are more than two opened documents, which one will the code select?
What would be the logic?

By using
ActiveDocument.ActiveWindow.Index
you can get the ActiveDocument window index number.
Then, using some logic you can get the name of the document in another
window.

For example:
'_______________________________________
Dim lngDocIndex As Long
Dim i As Long

lngDocIndex = ActiveDocument.ActiveWindow.Index

For i = 1 To Application.Windows.Count
If i <> lngDocIndex Then
MsgBox Application.Windows(i).Document.Name
Exit For
End If
Next
'_______________________________________

But if you have more than two documents, you need some logic to determine if
you have got the right one.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Russ

Marco,
This code will add text to the first document it finds not saved or if
everything is saved, it will open a new blank document and add the text.

Dim blnFound As Boolean
Dim aDoc As Word.Document
Dim myText As String

blnFound = False
myText = Selection.Range.Text & vbCr & ActiveDocument.Name
'or use .FullName
For Each aDoc In Documents
If aDoc.Path = "" Then
aDoc.Content.InsertAfter myText
blnFound = True
Exit For
End If
If Not blnFound Then
Documents.Add
ActiveDocument.Content.InsertAfter myText
End If
Next aDoc
 
R

Russ

Marco,
I added a vbCr at the start of the string in case you are going to use this
code repeatedly.

Dim blnFound As Boolean
Dim aDoc As Word.Document
Dim myText As String

blnFound = False
myText = vbCr & Selection.Range.Text & vbCr & ActiveDocument.Name
'or use .FullName
For Each aDoc In Documents
If aDoc.Path = "" Then
aDoc.Content.InsertAfter myText
blnFound = True
Exit For
End If
If Not blnFound Then
Documents.Add
ActiveDocument.Content.InsertAfter myText
End If
Next aDoc
 
C

Co

Marco,
I added a vbCr at the start of the string in case you are going to use this
code repeatedly.

Dim blnFound As Boolean
Dim aDoc As Word.Document
Dim myText As String

blnFound = False
myText = vbCr & Selection.Range.Text & vbCr & ActiveDocument.Name
'or use .FullName
For Each aDoc In Documents
If aDoc.Path = "" Then
aDoc.Content.InsertAfter myText
blnFound = True
Exit For
End If
If Not blnFound Then
Documents.Add
ActiveDocument.Content.InsertAfter myText
End If
Next aDoc

Russ,

It copies the text and doc name into the same document, namely the
source document.

What I actually wanted is to select a text by hand, then run the macro
which will take this text
and add a vbNewLine and then the name of the document.
That way I can then with Ctrl-V paste it into another document I have
opened.

Marco
 
S

Steve Yandl

Marco,

That is what I provided in my first response but then you said you expected
it to automatically paste as well. If you want a new line started in the
document where you're pasting, you could try changing the line:
strPlus = Selection.Text & ActiveDocument.Name
to
strPlus = vbCrLf & Selection.Text & ActiveDocument.Name


Steve
 
C

Co

Marco,
I added a vbCr at the start of the string in case you are going to use this
code repeatedly.

Dim blnFound As Boolean
Dim aDoc As Word.Document
Dim myText As String

blnFound = False
myText = vbCr & Selection.Range.Text & vbCr & ActiveDocument.Name
'or use .FullName
For Each aDoc In Documents
If aDoc.Path = "" Then
aDoc.Content.InsertAfter myText
blnFound = True
Exit For
End If
If Not blnFound Then
Documents.Add
ActiveDocument.Content.InsertAfter myText
End If
Next aDoc

Russ,

It copies the text and doc name into the same document, namely the
source document.

What I actually wanted is to select a text by hand, then run the macro
which will take this text
and add a vbNewLine and then the name of the document.
That way I can then with Ctrl-V paste it into another document I have
opened.

Marco
 

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