我正在尝试从Word中的表中获取信息并将其拉入Excel,因为表的数量是可变的,我没有其他方式来引用表我想在其前面的文本之后直接找到表在文件中。我之前使用过.Find方法,但出于某种原因我这次无法让它工作,而且从我以前读过和使用的所有内容开始,这应该可行:
Sub Process_NIM()
Dim WordPullFile As Object
Dim PullFolder As String
Dim PullDate As Date
Dim AppWord As Word.Application
Dim wd As Word.Document
Dim Table1 As Word.Table
Dim wb As Workbook
Dim ws1, ws2 As Worksheet
Dim Text1 As Word.Range
PullDate = DateAdd("d", 1, Now())
PullFolder = "M:\Production Case Files\" & Format(PullDate, "YYYY") & _
" Production Case Files\" & UCase(Format(PullDate, "MMM")) & _
"\" & UCase(Format(PullDate, "MMM DD")) & "\"
On Error GoTo OpenFileError
Set WordPullFile = Application.FileDialog(msoFileDialogOpen)
With WordPullFile
.AllowMultiSelect = False
.Filters.clear
.Filters.Add "DOC Files (*.doc)", "*.doc"
.InitialFileName = PullFolder
.Show
End With
On Error GoTo 0
If WordPullFile.SelectedItems.Count > 0 Then
PullFolder = WordPullFile.SelectedItems(1)
End If
Set AppWord = CreateObject("Word.Application")
Set wd = AppWord.Documents.Open(PullFolder)
wd.Application.Visible = True
Set wb = ThisWorkbook
Set ws1 = wb.Worksheets(1)
Set ws2 = wb.Worksheets(2)
If Text1.wd.Selection.Find.Execute(findtext:="Generator Outages for Today - Greater than 25MW") = True Then
'do stuff
End If
OpenFileError:
MsgBox ("There was an error opening the word file. Try closing any other instances of word and re-run.")
Exit Sub
End Sub
我目前在Word中使用它,但我无法从Excel中使用它:
If Text1.Find.Execute(findtext:="RC South Regional Review for") Then
Text1.InsertAfter (" " & Format(FileDate, "MMMM DD, YYYY"))
End If
答案 0 :(得分:0)
Sub Process_NIM()
Dim WordPullFile As Object
Dim PullFolder As String
Dim PullDate As Date
Dim AppWord As Word.Application
Dim wd As Word.Document
Dim Table1 As Word.Table
Dim wb As Workbook
Dim ws1, ws2 As Worksheet
Dim Text1 As Word.Range
PullDate = DateAdd("d", 1, Now())
PullFolder = "D:\Users\Mark\Documents\"
On Error GoTo OpenFileError
Set WordPullFile = Application.FileDialog(msoFileDialogOpen)
With WordPullFile
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "DOC Files (*.doc)", "*.doc"
.InitialFileName = PullFolder
.Show
End With
On Error GoTo 0
If WordPullFile.SelectedItems.Count > 0 Then
PullFolder = WordPullFile.SelectedItems(1)
End If
Set AppWord = CreateObject("Word.Application")
Set wd = AppWord.Documents.Open(PullFolder)
wd.Application.Visible = True
Set wb = ThisWorkbook
Set ws1 = wb.Worksheets(1)
Set ws2 = wb.Worksheets(2)
Set Text1 = wd.Range
If Text1.Find.Execute(findtext:="RC South Regional Review for") Then
Text1.InsertAfter (" " & Format(Date, "MMMM DD, YYYY"))
Else
MsgBox "could not find text"
End If
wd.Save
wd.Close
AppWord.Quit
Set AppWord = Nothing
Exit Sub
OpenFileError:
MsgBox ("There was an error opening the word file. Try closing any other instances of word and re-run.")
Exit Sub
End Sub