Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

need code to copy paste extended

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Co - 18 Oct 2007 14:38 GMT
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
Steve Yandl - 18 Oct 2007 22:07 GMT
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

> Hi All,
>
[quoted text clipped - 9 lines]
>
> Marco
Co - 18 Oct 2007 22:51 GMT
> Marco,
>
[quoted text clipped - 30 lines]
>
> > Marco

Steve,

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

Marco
Steve Yandl - 18 Oct 2007 23:32 GMT
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

>> Marco,
>>
[quoted text clipped - 39 lines]
>
> Marco
Co - 19 Oct 2007 07:03 GMT
> 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
[quoted text clipped - 55 lines]
>
> > Marco

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
Steve Yandl - 19 Oct 2007 15:40 GMT
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

>> It copies the text that was selected in your document appended with the
>> name
[quoted text clipped - 72 lines]
>
> Marco
Co - 19 Oct 2007 17:04 GMT
> 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
[quoted text clipped - 104 lines]
>
> > Marco

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
Jean-Guy Marcil - 19 Oct 2007 17:25 GMT
Co was telling us:
Co nous racontait que :

>> The subroutine below assumes that the selected text plus the name of
>> the document (and a space between the two strings) will be appended
[quoted text clipped - 21 lines]
>>
>> _____________________________________

<snip>

> Looks great Steve,
>
[quoted text clipped - 3 lines]
>
> 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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Russ - 19 Oct 2007 18:54 GMT
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

>> 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
[quoted text clipped - 112 lines]
>
> Marco

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 19 Oct 2007 19:06 GMT
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

> Marco,
> This code will add text to the first document it finds not saved or if
[quoted text clipped - 135 lines]
>>
>> Marco

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Co - 19 Oct 2007 20:01 GMT
> Marco,
> I added a vbCr at the start of the string in case you are going to use this
[quoted text clipped - 167 lines]
>
>  drsmN0SPAMikleAThotmailD0Tcom.INVALID

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
Steve Yandl - 19 Oct 2007 20:47 GMT
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

> Russ,
>
[quoted text clipped - 8 lines]
>
> Marco
Co - 19 Oct 2007 22:09 GMT
> Marco,
> I added a vbCr at the start of the string in case you are going to use this
[quoted text clipped - 167 lines]
>
>  drsmN0SPAMikleAThotmailD0Tcom.INVALID

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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.