Results 1 to 15 of 15

Thread: [RESOLVED] Paste Text Directly Into New Word Document

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Resolved [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:
    1. Dim lnghWnd1 As Long
    2. Dim lnghWnd2 As Long
    3. Dim sText As String
    4.  
    5.     Clipboard.Clear
    6.     Clipboard.SetText Text1.Text
    7.    
    8.     Shell "notepad", vbNormalFocus
    9.     lnghWnd1 = FindWindow(vbNullString, "Untitled - Notepad")
    10.     lnghWnd2 = GetWindow(lnghWnd1, GW_CHILD)
    11.    
    12.     sText = Clipboard.GetText
    13.     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:
    1. Dim lnghWnd1 As Long
    2. Dim lnghWnd2 As Long
    3. Dim sText As String
    4. Dim objWord As Word.a
    5.     Clipboard.Clear
    6.     Clipboard.SetText Text1.Text
    7.    
    8.     Shell "c:\program files\microsoft office\office11\winword.exe", vbNormalFocus
    9.     lnghWnd1 = FindWindow(vbNullString, "Document1 - Microsoft Word")
    10.     lnghWnd2 = GetWindow(lnghWnd1, GW_CHILD)
    11.    
    12.     sText = Clipboard.GetText
    13.     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.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Paste Text Directly Into New Word Document

    add a reference to the MS Word Object

    VB Code:
    1. Dim wrd As Word.Application
    2. Set wrd = New Word.Application
    3. wrd.Visible = True
    4. wrd.Documents.Add
    5.  
    6. Clipboard.Clear
    7. Clipboard.SetText "Hello There"
    8.  
    9. 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"

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Dim objWord As Word.Application
    To automate Word...
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Word xx.0 Object Library
    3. Private Sub Command1_Click()
    4.     Dim oApp As Word.Application
    5.     Dim oDoc As Word.Document
    6.     Set oApp = New Word.Application
    7.     Set oDoc = oApp.Documents.Add
    8.     oDoc.Range.Paste 'Assumes that your clipboard alread conains the data
    9. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    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.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    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.
    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.
    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.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  8. #8
    Addicted Member
    Join Date
    Oct 2005
    Posts
    149

    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.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. oDoc.Activate
    2. oDoc.Select
    3. Selection.TypeText Text:="test"
    4. '...
    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  10. #10
    Addicted Member
    Join Date
    Oct 2005
    Posts
    149

    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.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Option Explicit
    2. 'Add a reference to MS Word xx.0 Object Library
    3. Private Sub Command1_Click()
    4.     Dim oApp As Word.Application
    5.     Dim oDoc As Word.Document
    6.     Set oApp = New Word.Application
    7.     Set oDoc = oApp.Documents.Add
    8.     oDoc.Activate
    9.     oDoc.Select
    10.     Selection.TypeText Text:="test"
    11. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  12. #12
    Addicted Member
    Join Date
    Oct 2005
    Posts
    149

    Re: [RESOLVED] Paste Text Directly Into New Word Document

    Saw it. (I just omitted it for brevity.) Here's my code:

    VB Code:
    1. Dim oApp As Word.Application
    2.       Dim oDoc As Word.Document
    3.       Set oApp = New Word.Application
    4.       Set oDoc = oApp.Documents.Add 'Adds a new blank doc
    5.       oDoc.Activate
    6.       oDoc.Select
    7.  
    8.       Selection.TypeText Text:="test"

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  14. #14
    Addicted Member
    Join Date
    Oct 2005
    Posts
    149

    Re: [RESOLVED] Paste Text Directly Into New Word Document

    It woiked. Muchos Gracias!

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width