|
-
Oct 27th, 2005, 01:16 PM
#1
Thread Starter
Frenzied Member
[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 ) 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?
Beantown Boy
Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
-
Oct 27th, 2005, 01:48 PM
#2
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
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Oct 27th, 2005, 01:54 PM
#3
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 27th, 2005, 06:04 PM
#4
Thread Starter
Frenzied Member
Re: [RESOLVED] Paste Text Directly Into New Word Document
Thank you both. 
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.
Beantown Boy
Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
-
Oct 27th, 2005, 06:11 PM
#5
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 28th, 2005, 08:38 AM
#6
Thread Starter
Frenzied Member
Re: [RESOLVED] Paste Text Directly Into New Word Document
Beantown Boy
Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
-
Oct 28th, 2005, 08:58 AM
#7
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 29th, 2005, 03:34 PM
#8
Addicted Member
Re: [RESOLVED] Paste Text Directly Into New Word Document
RobDog, you said:
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.
-
Oct 29th, 2005, 08:43 PM
#9
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"
'...
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 30th, 2005, 02:51 PM
#10
Addicted Member
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.
-
Oct 30th, 2005, 02:59 PM
#11
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 30th, 2005, 03:11 PM
#12
Addicted Member
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"
-
Oct 30th, 2005, 03:15 PM
#13
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"
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 30th, 2005, 03:22 PM
#14
Addicted Member
Re: [RESOLVED] Paste Text Directly Into New Word Document
It woiked. Muchos Gracias!
-
Oct 30th, 2005, 03:24 PM
#15
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|