Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于MS word VBA宏从excel工作表中读取特定列_Excel_Vba_Ms Word - Fatal编程技术网

基于MS word VBA宏从excel工作表中读取特定列

基于MS word VBA宏从excel工作表中读取特定列,excel,vba,ms-word,Excel,Vba,Ms Word,我想突出显示word文档中excel工作表特定列中给出的特定单词 我有一个有效的解决方案(见下文),可以从word文件中读取单词,但我无法使用excel文件中的特定列运行它来执行相同的操作。基本上,我想做以下python代码所做的事情(但对于VBA): 。。。下面的代码中应该使用docRef。我就是不能让它运行 Sub highlightWords() Dim sCheckDoc As String Dim docRef As Document Dim docCurrent As Docume

我想突出显示word文档中excel工作表特定列中给出的特定单词

我有一个有效的解决方案(见下文),可以从word文件中读取单词,但我无法使用excel文件中的特定列运行它来执行相同的操作。基本上,我想做以下python代码所做的事情(但对于VBA):

。。。下面的代码中应该使用docRef。我就是不能让它运行

Sub highlightWords()

Dim sCheckDoc As String
Dim docRef As Document
Dim docCurrent As Document
Dim wrdRef As Object

sCheckDoc = "list.docx"
Set docCurrent = Selection.Document
Set docRef = Documents.Open(sCheckDoc)
docCurrent.Activate

Options.DefaultHighlightColorIndex = wdRed
With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Highlight = True
    .Replacement.Text = "^&"
    .Forward = True
    .Format = True
    .MatchWholeWord = True
    .MatchCase = False
    .MatchWildcards = False
End With

For Each wrdRef In docRef.Words
    If Asc(Left(wrdRef, 1)) > 32 Then
        With Selection.Find
            .Wrap = wdFindContinue
            .Text = wrdRef
            .Execute Replace:=wdReplaceAll
        End With
    End If
Next wrdRef

docRef.Close
docCurrent.Activate
End Sub

修改自的代码。

要从Word中控制Excel,应在Word VBA编辑器中设置对Excel库的引用:工具,引用,向下滚动到Microsoft Excel并勾选它

然后,您需要打开Excel并加载工作簿

   Dim XL as new Excel.Application
   Dim wb as Excel.Workbook
   Set wb = xl.Workbooks.open("path and name of file list.xlsx")
   For Each wordref in wb.Sheets(1).Range("a1:A" & wb.Sheets(1).usedrange.rows.count)

您拥有的代码对Word文件执行搜索(如您所述)。您能提供您尝试用于Excel文件的代码吗?我们可以帮助您解决您遇到的任何问题。我已经尝试了很多方法——在最简单的版本中,只是将“list.docx”替换为我的“list.xlsx”文件(假设excel文件也是一个文档,并且所有读卡器都继承自同一类)。然后将文档替换为工作簿(在google上找到),但它表示未定义类型、需要对象、404等。原则上,我只需要在VBA中运行3行pyhton代码(不幸的是,我对语法不是很熟悉)
   Dim XL as new Excel.Application
   Dim wb as Excel.Workbook
   Set wb = xl.Workbooks.open("path and name of file list.xlsx")
   For Each wordref in wb.Sheets(1).Range("a1:A" & wb.Sheets(1).usedrange.rows.count)