Consulting

Results 1 to 11 of 11

Thread: copy from Word to Excel

  1. #1
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location

    copy from Word to Excel

    Hi!
    I need a little help.
    Select Word text (address 5-6 lines), run macro and paste it in myfile.xls file.

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Uh....what have you tried so far?

  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    This may work for you.

    Option Explicit
     
    Sub WordToExcel()
    Dim AppExcel        As Object
    Dim Wkb             As Object
    Dim Path            As String
    'Path to the workbook
        Path = ThisDocument.Path & "\Book1.xls"
    'Create a new instance of Excel
        Set AppExcel = CreateObject("Excel.Application")
    'Open the workbook
        Set Wkb = AppExcel.Workbooks.Open(FileName:=Path)
    'Copy the selected text
        Selection.Copy
    'Paste into range A1 of the workbook on sheet1
        Wkb.Sheets("Sheet1").Select
        Wkb.Sheets("Sheet1").Range("A1").Select
        AppExcel.ActiveSheet.Paste
    'Make the Excel application visible
        AppExcel.Visible = True
    Set Wkb = Nothing
        Set AppExcel = Nothing
    End Sub
    Last edited by Aussiebear; 04-19-2023 at 03:55 PM. Reason: Adjusted the code tags

  4. #4
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location
    Quote Originally Posted by fumei
    Uh....what have you tried so far?
    i cant open Excel and myfile.xls

    Selection and copy/paste is easy

    DRJ:
    I try this code

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Did you try Jake's code?

  6. #6
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location
    Hmm i try but now problem to check in Word, is excel file opened or not.
    If opened then macro open new myfile.xls as read-only

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Huh?
    If opened then macro open new myfile.xls as read-only
    If it is opened....you want to try and open it again, but as read only?

  8. #8
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location
    sry my bad englis but i dont mean open new file. as read-only.
    I try explain step-by-step
    1. Select text (5-6 rows) in Word
    2. run macro ( copy selected text, paste it in myfile.xls)
    3. Make new selection in Word and run macro. (myfile.xls is still opened)
    4. This new selection paste over earlier pasted selection
    5. and so on until workday is over and I close excel file

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    If you want to work with an already open file, then instead of ..

     'Create a new instance of Excel
    Set AppExcel = CreateObject("Excel.Application")
    'Open the workbook
    Set Wkb = AppExcel.Workbooks.Open(FileName:=Path)
    use ..

    Set Wkb = GetObject("Path")
    Set AppExcel = Wkb.Parent

    If the workbook is not opened, this will open it, but you may find you need to add an extra line to make the rest of the code work ..

    Wkb.Windows(1).Visible = True
    Last edited by Aussiebear; 04-19-2023 at 03:56 PM. Reason: Adjusted the code tags
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    VBAX Regular
    Joined
    Aug 2005
    Posts
    77
    Location
    Thanks!

    Final code is now this

    Sub WordToExcel()
        Dim AppExcel  As Object
        Dim Wkb       As Object
        Dim Path      As String
        Dim wbName      As String
    On Error GoTo Copy_Error
    wbName = "Birgit v.2.xls"
        Path = "C:HendrikExcelMarko jama"
    Set Wkb = GetObject(Path & wbName)
        Set AppExcel = Wkb.Parent
    'Copy the selected text
            Selection.Copy
    Wkb.Sheets("Taide").Select
            Wkb.Sheets("Taide").Range("C5:C11").Clear
        'Paste into range C5 of the workbook on Taide
            Wkb.Windows(1).Visible = True
            Wkb.Sheets("Taide").Select
            Wkb.Sheets("Taide").Range("C5").Select
            AppExcel.ActiveSheet.Paste
         'Make the Excel application visible
            AppExcel.Visible = True
    Set Wkb = Nothing
        Set AppExcel = Nothing
        Exit Sub
    Copy_Error:
      MsgBox "Viga! Tekst ei ole valitud."
    End Sub
    Last edited by Aussiebear; 04-19-2023 at 03:57 PM. Reason: Adjusted the code tags

  11. #11
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi and Welcome to VBAX!

    Glad to see you have it working now...don't forget to mark your thread solved if it has been?
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •