Hi!
I need a little help.
Select Word text (address 5-6 lines), run macro and paste it in myfile.xls file.
Hi!
I need a little help.
Select Word text (address 5-6 lines), run macro and paste it in myfile.xls file.
Uh....what have you tried so far?
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
i cant open Excel and myfile.xlsOriginally Posted by fumei
Selection and copy/paste is easy
DRJ:
I try this code
Did you try Jake's code?
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
Huh?
If it is opened....you want to try and open it again, but as read only?If opened then macro open new myfile.xls as read-only
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
If you want to work with an already open file, then instead of ..
use ..'Create a new instance of Excel Set AppExcel = CreateObject("Excel.Application") 'Open the workbook Set Wkb = AppExcel.Workbooks.Open(FileName:=Path)
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
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
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)