ThatNewGuy
Technical User
I'm currently working on code so the end user will have an open form in Excel then upon execution of a command button the user will be able to browse their pc for Word files needed to update the current Excel document/form. Once the Word document is opened I want to automate the process of selecting a range and pasting it into Excel. I know this is a pain going back and forth between the two formats of Word and Excel, but the story is too long to get into here. One last thing, all the vba is currently in Excel only.
Everything works until I try selecting a range within a Word table. The starting point of my range is selected in the Word table, but the ending point of the range is never selected. My code only selects one word. I've roamed the FAQ and tried some variations but any help would be appreciated because I'm stuck.
I'm also having trouble working out how to copy/paste from Word to Excel when all my code is in Excel. I can't run a Word.Range without getting errors bcuz of the Excel format.
Thanks,
Everything works until I try selecting a range within a Word table. The starting point of my range is selected in the Word table, but the ending point of the range is never selected. My code only selects one word. I've roamed the FAQ and tried some variations but any help would be appreciated because I'm stuck.
I'm also having trouble working out how to copy/paste from Word to Excel when all my code is in Excel. I can't run a Word.Range without getting errors bcuz of the Excel format.
Thanks,
Code:
Sub CommandButton1_Click()
Dim Filter As String, Title As String
Dim FilterIndex As Integer
Dim Filename As Variant
'File filters
Filter = "Word Documents (*.doc),*.doc," & "Text Files (*.txt),*.txt,"
' Default Filter to *.*
FilterIndex = 1
' Set Dialog Caption
Title = "Select LBL File to Open"
' Select Start Drive and Path
ChDrive ("C")
ChDir ("C:\Documents and Settings\")
With Application
' Set File Name to selected File
Filename = .GetOpenFilename(Filter, FilterIndex, Title)
' Reset Start Drive Path
ChDrive (Left(.DefaultFilePath, 1))
ChDir (.DefaultFilePath)
End With
' Exit on Cancel
If Filename = False Then
MsgBox "No file was selected."
Exit Sub
End If
' Open File
On Error Resume Next
' Open Word Doc
Set WrdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then ' Word is not already running
Set WrdApp = CreateObject("Word.Application")
End If
'Open The Document
Set wdDoc = WrdApp.Documents.Open(Filename)
With WrdApp
'Show Word
.Visible = True
With .Selection.Find
.ClearFormatting
.Text = "specifications:"
.Execute Forward:=True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
point1 = Selection.Range.Start
With .Selection.Find <--------Troubles at this point down
.ClearFormatting
.Text = "author"
.Execute Forward:=True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
point2 = Selection.Range.End
ActiveDocument.Range(Start:=point1, End:=point2).Copy
End With
End Sub