从Word-UserForm

时间:2016-01-12 15:50:06

标签: vba excel-vba-mac excel-2011 word-vba-mac

我有一个基于发票模板的Word文档和一个包含两个表的数据库:公司和个人。

我想将一些字符串放入Word中userform的文本框中,然后在Excel中搜索。 Excel应将值返回到位于另一个UserForm中的MultiColumn-Listbox(此userform仅显示搜索到的字符串的结果是否超过1)。

这是我在Word中运行宏的代码,它实际上已经开始了:

CSearchText = UFCompanySearch.tbSearchCompany.Value 'Textbox -> Search-String

xlWB.Application.Run("SearchCompany")

仅当SearchCompany是sub或没有其他规范的函数时才有效,所以

Function SearchCompany(SearchText As String)

无法运行,因为我无法运行宏,如下所示:

xlWB.Application.Run("SearchCompany("SomeCompany")") 'NOTE!

注意:这不起作用!!

要在UserForm中填充列表框,我认为有可能用Excel表填充它,所以这应该以某种方式解决。

这就是问题:

我不能将位于单词Document中的Userform中的Search-TextBox称为" Doc!"也不是" Doc。"作品。像这样,我无法在单元格中搜索字符串。这是我必须找到包含字符串的单元格的代码:

IF (InStr(xlComp.Cells(Row, 1), CSearchText) > 0) Or _
    (InStr(xlComp.Cells(Row, 2), CSearchText) > 0) Or _
    (InStr(xlComp.Cells(Row, 3), CSearchText) > 0) Then

这将在列A-C中搜索输入的字符串。 (我在某个地方找到了代码......我一直在搜索,知道从哪里来。^。^)

有没有办法在Word中解决UserForm或解决方法来获取" SearchText"从用户窗体到Excel?

我在VBA中很新,所以答案越详细,我就越有可能理解它。

1 个答案:

答案 0 :(得分:0)

由于我没有找到直接做到这一点的方法,我在尝试时得到了解决方法:

Word中的代码:

Private Sub cbFirmaSearch_Click()

    ActiveDocument.FormFields("FSearchText").Result = UFFirmaSearch.txtFirmaSuchen.Value

    xlWB.Application.Run "SearchFirma"

    ActiveDocument.FormFields("FSearchText").Delete

    Dim DFLastRow  As Integer
    DFLastRow = xlWB.Sheets("DataFirma").Cells(xlWB.Sheets("DataFirma").Rows.Count, "a").End(xlUp).Row

    Dim lbFirmTar As ListBox
    Set lbFirmTar = UFFirmaSearchList.lbFirmaSearchList

    Dim Row As Integer
    For Row = 2 To DFLastRow
        With lbFirmTar
            Dim ListIndex As Integer
            ListIndex = UFFirmaSearchList.lbFirmaSearchList.ListCount
            .AddItem xlWB.Sheets("DataFirma").Cells(Row, 1).Value, ListIndex
            .List(ListIndex, 1) = xlWB.Sheets("DataFirma").Cells(Row, 2).Value
            .List(ListIndex, 2) = xlWB.Sheets("DataFirma").Cells(Row, 3).Value
            .List(ListIndex, 3) = xlWB.Sheets("DataFirma").Cells(Row, 4).Value
            .List(ListIndex, 4) = xlWB.Sheets("DataFirma").Cells(Row, 5).Value
            .List(ListIndex, 5) = xlWB.Sheets("DataFirma").Cells(Row, 6).Value
            .List(ListIndex, 6) = xlWB.Sheets("DataFirma").Cells(Row, 7).Value
        End With
    Next Row

    With UFFirmaSearchList
        If (.lbFirmaSearchList.ListCount > 1) Then
            UFFirmaSearch.Hide
            .Show
        ElseIf (.lbFirmaSearchList.ListCount = 1) Then
            FirmaID = .lbFirmaSearchList.List(0, 0)
            FirmaZusatz = .lbFirmaSearchList.List(0, 1)
            FirmaName = .lbFirmaSearchList.List(0, 2)
            FirmaAbteilung = .lbFirmaSearchList.List(0, 3)
            FirmaAdresse = .lbFirmaSearchList.List(0, 4)
            FirmaPLZ = .lbFirmaSearchList.List(0, 5)
            FirmaOrt = .lbFirmaSearchList.List(0, 6)
            UFFirmaSearch.lblfrFirmenangaben = "Firma ID : " & FirmaID & _
                                                "Firmenzusatz : " & FirmaZusatz & _
                                                "Name : " & FirmaName & _
                                                "Firmenabteilung : " & FirmaAbteilung & _
                                                "Adresse : " & FirmaAdresse & _
                                                "PLZ / Ort : " & FirmaPLZ & " " & FirmaOrt
        Else
            MsgBox "No Entry found.", vbOKOnly
        End If
    End With
    UFFirmaSearch.txtFirmaSuchen.SetFocus
End Sub

Excel中的代码:

Sub SearchFirma()

    Dim Doc As Word.Document
    Set Doc = ActiveDocument

    Dim xlFirm As Worksheet
    Set xlFirm = ActiveWorkbook.Sheets("Firma")

    Dim LastRow As Integer 'Last row on sheet "Firma" containing values
    LastRow = xlFirm.Cells(xlFirm.Rows.Count, "a").End(xlUp).Row

    Dim xlDatFirm As Worksheet
    Set xlDatFirm = ActiveWorkbook.Sheets("DataFirma")

    Dim FSearchText As String
    FSearchText = Doc.FormFields("FSearchText").Result

    For Row = 2 To LastRow
        Dim DFNewRow As Integer  'Next free line on sheet "DataFirma"
        DFNewRow = xlDatFirm.Cells(xlDatFirm.Rows.Count, "A").End(xlUp).Row + 1
        If (InStr(1, xlFirm.Cells(Row, 1), FSearchText, vbTextCompare) > 0) Or (InStr(1, xlFirm.Cells(Row, 2), FSearchText, vbTextCompare) > 0) Or (InStr(1, xlFirm.Cells(Row, 3).Value, FSearchText, vbTextCompare) > 0) Or (InStr(1, xlFirm.Cells(Row, 4).Value, FSearchText, vbTextCompare) > 0) Then
            xlDatFirm.Range("A" & DFNewRow).Value = xlFirm.Cells(Row, 1).Value
            xlDatFirm.Range("B" & DFNewRow).Value = xlFirm.Cells(Row, 2).Value
            xlDatFirm.Range("C" & DFNewRow).Value = xlFirm.Cells(Row, 3).Value
            xlDatFirm.Range("D" & DFNewRow).Value = xlFirm.Cells(Row, 4).Value
            xlDatFirm.Range("E" & DFNewRow).Value = xlFirm.Cells(Row, 5).Value
            xlDatFirm.Range("F" & DFNewRow).Value = xlFirm.Cells(Row, 6).Value
            xlDatFirm.Range("G" & DFNewRow).Value = xlFirm.Cells(Row, 7).Value
        End If
    Next Row
End Sub

不知何故,这有效。当我第一次尝试" Dim xlWB As Excel.Workbook"在Word中我总是会遇到运行时错误。当我尝试" Dim Doc As Word.Document"在Excel中虽然我从来没有出现过错误......非常奇怪,但仍然以某种方式设法完成它。

如果您对此有任何疑问,我将很乐意尝试提供帮助,如果有更好的方法可以改写,请不要犹豫,发表评论。

感谢您的支持:)