[RESOLVED] Paste Text Directly Into New Word Document
I have this code that I got from these forums (can't remember who posted it...sorry :blush: ) that pastes text directly into Notepad.
VB Code:
Dim lnghWnd1 As Long
Dim lnghWnd2 As Long
Dim sText As String
Clipboard.Clear
Clipboard.SetText Text1.Text
Shell "notepad", vbNormalFocus
lnghWnd1 = FindWindow(vbNullString, "Untitled - Notepad")
lnghWnd2 = GetWindow(lnghWnd1, GW_CHILD)
sText = Clipboard.GetText
SendMessage lnghWnd2, WM_SETTEXT, Len(sText), sText
This works like a champ. What I need to do is modify this to paste the text into a blank word document, so I changed it to this
VB Code:
Dim lnghWnd1 As Long
Dim lnghWnd2 As Long
Dim sText As String
Dim objWord As Word.a
Clipboard.Clear
Clipboard.SetText Text1.Text
Shell "c:\program files\microsoft office\office11\winword.exe", vbNormalFocus
lnghWnd1 = FindWindow(vbNullString, "Document1 - Microsoft Word")
lnghWnd2 = GetWindow(lnghWnd1, GW_CHILD)
sText = Clipboard.GetText
SendMessage lnghWnd2, WM_SETTEXT, Len(sText), sText
However, it does not paste the text into the document. The text is on the clipboard. When the word document opens, if I do a Ctrl-V or an Edit/Paste, the text is copied to the document, but I need it to be there without having to paste it myself.
I don't want the text pasted into an existing document, I need the text pasted into a new, blank, document1, Word document.
How would I do that?
Re: Paste Text Directly Into New Word Document
add a reference to the MS Word Object
VB Code:
Dim wrd As Word.Application
Set wrd = New Word.Application
wrd.Visible = True
wrd.Documents.Add
Clipboard.Clear
Clipboard.SetText "Hello There"
wrd.Documents(1).Range.Paste
Re: Paste Text Directly Into New Word Document
It doesnt have to be pasted. You can write directly into a new word document using the Word Object Model.
I also see that your code is missing the pplication part of the word definition.
VB Code:
Dim objWord As Word.Application
To automate Word...
VB Code:
Option Explicit
'Add a reference to MS Word xx.0 Object Library
Private Sub Command1_Click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Set oApp = New Word.Application
Set oDoc = oApp.Documents.Add
oDoc.Range.Paste 'Assumes that your clipboard alread conains the data
End Sub
Moved from Classic VB forum.
Re: [RESOLVED] Paste Text Directly Into New Word Document
Thank you both. :thumb:
I have a curousity question. I originally posted this question in the VB section because I'm using VB in my program. I am not using VBA.
I was always under the impression that if you were using VB in your program your question belonged in the VB section, and if you were using VBA your question belonged in the VBA section.
My question was successfully anwsered, so I am not questioning the move on that score, but I am interested in what kind of criteria the moderators use in moving questions between the VB section and the VBA section.
Thanks and reps to both RobDog888 and A51g]Static.
Re: [RESOLVED] Paste Text Directly Into New Word Document
Recently I had the VBA forum renamed to Office Development as this was more appropriate. If your doing anything with an office app it usualy would now fall into the OD forum criteria. There are exceptions to the rule but traffic in the OD forum has been on the rise for about a year now. Going from 25-35 posts per month to now over 230+ posts per month. :D
Re: [RESOLVED] Paste Text Directly Into New Word Document
Quote:
Originally Posted by RobDog888
Recently I had the VBA forum renamed to Office Development as this was more appropriate. If your doing anything with an office app it usualy would now fall into the OD forum criteria. There are exceptions to the rule but traffic in the OD forum has been on the rise for about a year now. Going from 25-35 posts per month to now over 230+ posts per month. :D
Ok. If that is the rule, I have no problem.
I can certainly see why a question dealing with Office Development should go here as opposed to ClassicVb. Answers in ClassicVb would be in VB code, which might not be supported in VBA. But it just seems odd that if I'm not doing any development in an Office product, but merely using an Office product as sort of an ancillary part of my VB program, that the question should go here. In all likelyhood, I would be getting answers in VBA which might not be supported in VB.
Whatever....I have no problems playing by the rules as long as I know what the rules are. :)
Thanks RobDog888. :thumb:
Re: [RESOLVED] Paste Text Directly Into New Word Document
:)
As in the description of the OD forum, it describes "automation" also. So even if your not using any VBA at all, the automation factor is what sets it as an OD thread basically.
Now, your original goal was all VB and no automation. But the solution as an automation one. Kind of a grey area but I see your point. :) At least its solved. :D
Re: [RESOLVED] Paste Text Directly Into New Word Document
RobDog, you said:
Quote:
It doesnt have to be pasted. You can write directly into a new word document using the Word Object Model.
But how? The code you provided just seems to insert the selection from the clipboard. Perhaps I'm just not seeing it?
Thanks.
Re: [RESOLVED] Paste Text Directly Into New Word Document
Thats how the poster wanted ot. To do as I stated you just need to use the Word Object Model methods, functions, and properties to write/read to a document.
VB Code:
oDoc.Activate
oDoc.Select
Selection.TypeText Text:="test"
'...
Re: [RESOLVED] Paste Text Directly Into New Word Document
Thanks for the help, RD. I realize that what I'm after is different than the original poster's query, but I have searched for my answers regarding writing to a new doc (other than from the clipboard) and this is the closest I have found.
I get an error "Object variable or With block variable not set." when the 'Selection' method/property is executed.
I am using the Microsoft Word 8.0 Object Library. What am I missing now? oDoc.Selection is an invalid property... (I tried everything I could think of.)
Thanks again. :)
Re: [RESOLVED] Paste Text Directly Into New Word Document
Thats because you probably didnt see the restof the code in the previous post.
VB Code:
Option Explicit
'Add a reference to MS Word xx.0 Object Library
Private Sub Command1_Click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Set oApp = New Word.Application
Set oDoc = oApp.Documents.Add
oDoc.Activate
oDoc.Select
Selection.TypeText Text:="test"
End Sub
Re: [RESOLVED] Paste Text Directly Into New Word Document
Saw it. (I just omitted it for brevity.) Here's my code:
VB Code:
Dim oApp As Word.Application
Dim oDoc As Word.Document
Set oApp = New Word.Application
Set oDoc = oApp.Documents.Add 'Adds a new blank doc
oDoc.Activate
oDoc.Select
Selection.TypeText Text:="test"
Re: [RESOLVED] Paste Text Directly Into New Word Document
Does Word at at lest appear? If not try adding oApp.Visible = True. Also, try adding oApp.Selection.TypeText Text:="test"
Re: [RESOLVED] Paste Text Directly Into New Word Document
It woiked. Muchos Gracias! :D
Re: [RESOLVED] Paste Text Directly Into New Word Document
Oh ok, wasnt too sure as your last post didnt say if you got it working.
Glad to have helped. :)