Hi,
Im new and learning VBA, and need help with a macro.
I want to copy the entire lines 2,3 and 5 from all of the word 2010 documents in one directory, and paste them in to columns A, C and B respectively in one worksheet of Excel 2010. This can be in the same directory.
I have tried writing it myself by adapting a recorded macro, but am getting lost with it. Can someone please help me?
Option Explicit'***************
Public SendDataToExcel
'variable declaration
Dim rngSel As Word.Range
Dim origSetup As Word.PageSetup
Dim wb As Excel.Workbook
'Assign the selection to its variable
Set rngSel = Selection.Range
Set origSetup = rngSel.Sections (3) . PageSetup
'Create a new workbook from the current document
Set wb + Workbook.Add(ActiveDocument.Fullname)
wb.Range.FormattedText = rngSel. FormattedText
With wb.Sections(!) . Page
Sub DirLoop()
Dim MyFile As String, Sep As String
' Sets up the variable "MyFile" to be each file in the directory
' This example looks for all the files that have an .docx extension.
' This can be changed to whatever extension is needed. Also, this
' macro searches the current directory. This can be changed to any
' directory.
' Test for Windows or Macintosh platform. Make the directory request.
Sep = Application.PathSeparator
If Sep = "\" Then
' Windows platform search syntax.
MyFile = Dir(CurDir() & Sep & "*.docx")
Else
' Macintosh platform search syntax.
MyFile = Dir("", MacID("XLS5"))
End If
' Starts the loop, which will continue until there are no more files
' found.
Do While MyFile <> ""
' Displays a message box with the name of the file. This can be
' changed to any procedure that would be needed to run on every
' file in the directory such as opening each file.
MsgBox CurDir() & Sep & MyFile
MyFile = Dir()
Loop
End Sub
Sub Copy2()
'
' Copy2 Macro
'
'
Selection.Copy
Set oRngDCopy = ActiveDocument.Bookmarks("\Line1").Range
Selection.PasteAndFormat (wdFormatOriginalFormatting)
End Sub
Sub Copy1()
'
' Copy1 Macro
'
'
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1
Set oRngDCopy = ActiveDocument.Bookmarks("\Line2").Range
Selection.Copy
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.MoveUp Unit:=wdLine, Count:=2
End Sub
Sub Copytest()
'
' Copytest Macro
'
'
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=6
Selection.MoveRight Unit:=wdCharacter, Count:=6, Extend:=wdExtend
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.TypeParagraph
Windows("Document1").Activate
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=7, Extend:=wdExtend
Selection.Copy
Windows("Document2").Activate
Windows("Document1").Activate
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdCharacter, Count:=7
Selection.MoveRight Unit:=wdCharacter, Count:=11, Extend:=wdExtend
Selection.Copy
Windows("Document2").Activate
Selection.TypeParagraph
Windows("Document1").Activate
Selection.PasteAndFormat (wdFormatOriginalFormatting)
End Sub
Sub Testforxl()
'
' Testforxl Macro
'
'
Selection.MoveLeft Unit:=wdCharacter, Count:=5
Selection.MoveUp Unit:=wdLine, Count:=5
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=6, Extend:=wdExtend
Selection.Copy
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=6
Selection.MoveRight Unit:=wdCharacter, Count:=7, Extend:=wdExtend
Selection.Copy
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.MoveLeft Unit:=wdCharacter, Count:=7
Selection.MoveRight Unit:=wdCharacter, Count:=11, Extend:=wdExtend
Selection.Copy
End Sub