can't copy from Word document VBA excel form


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: can't copy from Word document VBA excel form

  1. #1
    Join Date
    Mar 2009
    Posts
    6

    can't copy from Word document VBA excel form

    Hi,

    this is my first post and i am new to VB.

    I am using a VBA form through Excel.

    I am trying to open a word document and copy the entire contents to a text box on my form.

    here is my code:

    [CODE]

    Dim wrdApp As Object
    Dim wrdDoc As Object

    Set wrdApp = CreateObject("Word.Application")

    wrdApp.Visible = True

    Set wrdDoc = wrdApp.Documents.Open(PathToDocument)
    'PathToDocument uses variables that are defined earlier. it works...

    wrdDoc.Edit.SelectAll. 'here is my problem
    TxtSummary.SetFocus
    TxtSummary.SelStart = 0
    TxtSummary.Paste

    [END CODE]

    @ wrdDoc.Edit.SelectAll. I get an error "Object doesn't support this property or method."

    How do I select and copy the text in my word document?

    Thanks a bunch...

    thegeeber

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    Try changing this:
    Code:
      wrdDoc.Edit.SelectAll
    to this:
    Code:
      wrdDoc.Selection.WholeStory
      wrdDoc.Selection.Copy

  3. #3
    Join Date
    Mar 2009
    Posts
    6
    Thanks,

    still having problems,

    I think I am not Declaring WrdDoc properly or something. It still won't support the Method.

    Should I declare WrdDoc as Object or as Word.Document or something like that?

    Maybe Word.Application.Document?

    thegeeber

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Do you have a reference set to Word from Excel?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  5. #5
    Join Date
    Mar 2009
    Posts
    6
    Do you mean:

    Tools

    References

    Microsoft Word 11.0 Object Library

    I have this selected.

  6. #6
    Join Date
    Mar 2009
    Posts
    6
    Strangely enough, after

    Set WrdDoc = WdApp.Documents.Open (PathToDocument)

    I CAN do:

    WdDoc.Close

    Works fine with no problems, I just can't copy the darn thing!!

    thanks,

    thegeeber

  7. #7
    Join Date
    Mar 2009
    Posts
    6
    Am I using the right code to copy? Do I need to do range or selection?

  8. #8
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    Here is a little test workbook. Open the visual basic editor and double click on the user form. Run the form, select a word doc file and press the GetWord button. The text from the Word Doc will go into the large multiline textbox.
    Have Fun...
    Attached Files Attached Files

  9. #9
    Join Date
    Mar 2009
    Posts
    6
    Thanks!!!

    Looks like my whole problem was that when I was trying to use Selection I should have been using it with WordApp not WordDoc!

    Don't quite get it, I thought that the Document object would be what I wanted to reference, not the Application.

    In any case it works great! Much obliged!

    thegeeber
    Last edited by thegeeber; 04-03-2009 at 10:34 AM. Reason: switched words

  10. #10
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Quote Originally Posted by thegeeber View Post
    Don't quite get it, I thought that the Document object would be what I wanted to reference, not the Application.
    I think this is something that you just learn as time goes on.

    I was a VB6 programmer for over 13 years when I got tossed into the middle of a bunch of VBA apps about a year ago.

    I still get tripped up over this particular aspect of using VBA, but it has nailed me so many times, that I now just automatically try the "other" thing, if the first one doesn't work.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  11. #11
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    Quote Originally Posted by thegeeber View Post
    Thanks!!!

    Looks like my whole problem was that when I was trying to use Selection I should have been using it with WordApp not WordDoc!

    Don't quite get it, I thought that the Document object would be what I wanted to reference, not the Application.

    In any case it works great! Much obliged!

    thegeeber
    Actually you mean just the opposite. Selection is part of the Application object not the Document object. You were using the Document object when you should have been using the Application object.

  12. #12
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,261
    Quote Originally Posted by Hack View Post
    I think this is something that you just learn as time goes on.

    I was a VB6 programmer for over 13 years when I got tossed into the middle of a bunch of VBA apps about a year ago.

    I still get tripped up over this particular aspect of using VBA, but it has nailed me so many times, that I now just automatically try the "other" thing, if the first one doesn't work.
    VBA is basically the same as VB6. The thing that seems to hang most people up is the differences between the applications that implement VBA. Each application has it's own set of objects. These objects from the Application object on down are part of the application in which VBA is implemented.

    Because of this they have similar but different properties and methods; which can be very confusing when you switch between applications, like Excel, Word, or MS Access.

    You must always keep in mind that the objects are different. When you develop in VBA you should always define your object variables as the type of object you are accessing. Example of this is MS Words document object. If you declare your variable as type Object then VBA cannot help you with the available properties and methods; but if you define it as a Word.Document type, then VBA can help you with the properties and methods.

    Intellisence can give you a list when you access the objects while you are coding, plus you can select the specific property or method and hit F1 to get detailed help on it.

    Once you have debugged your application then you can switch the Object variable definitions to type Object. Doing this allows you to develope code that is version independent. An app written in Version 9 will be able to run in version 10. This is mostly important when you access external applications like when Excel accesses Word objects.

    Durning development of your Excel app you set the Word 9 object references; but once the app is debugged you should remove the word 9 objects references and change the variable definitions to type Object. This prevents object reference version errors. If you have the Word 9 object references set and your user only has version 10 or 11 you will get errors.

Similar Threads

  1. Replies: 0
    Last Post: 10-15-2008, 11:54 AM
  2. Replies: 4
    Last Post: 06-04-2008, 02:28 PM
  3. Replies: 0
    Last Post: 04-22-2008, 04:09 AM
  4. Replies: 4
    Last Post: 08-09-2006, 05:07 PM
  5. Search for MS Word (Parents and child window)
    By Sasha Polinskaya in forum VB Classic
    Replies: 21
    Last Post: 02-15-2002, 07:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center