2016-01-12 78 views
0

我有一個Word文檔,它基於Excel中包含兩個表格:Excel和Person的發票和數據庫模板。從Word中的文本框中獲取數據UserForm

我想將一些字符串放入Word中的用戶表單中的文本框中,然後在Excel中搜索該字符串。 Excel應將值返回到位於另一個用戶窗體中的MultiColumn-Listbox(此用戶窗體僅在搜索到的字符串的結果超過1時纔會顯示)。

這是我在Word中的代碼來運行宏,它實際上得到開始:

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

xlWB.Application.Run("SearchCompany") 

僅當SearchCompany是子或沒有進一步的規範功能的作品,所以

Function SearchCompany(SearchText As String) 

不工作,我不能運行宏如下:

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

注意:這將 不行!!

要填充用戶窗體中的列表框我認爲有可能用Excel表格填充它,所以這應該以某種方式解決。

這就是問題所在:「督」

我不是指在用戶窗體搜索,文本框位於在Word文檔中既不也不是「文件」。作品。像這樣,我無法搜索字符串的單元格。這是代碼我必須找到包含字符串的單元格:

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中的用戶窗體或變通方法來從用戶窗體獲取「SearchText」到Excel ?

我在VBA中頗爲新穎,所以您的答案越詳細,越有可能我會理解它。

+0

使用跑好文章:http://www.rondebruin.nl/win/s9/win001.htm –

+0

xlWB.Application.Run( 「SearchCompany(」 SomeCompany」 )「)應該是xlWB.Application。運行(「SearchCompany」,「SomeCompany」) –

+0

Tim&Nathan:也不管用...... :(我馬上得到一個預期爲「=」的編譯錯誤(xlWB.Application.Run(「'」&xlWB& 「'!SearchFirma」,FSearchText)/ xlWB.Application.Run(「SearchFirma」,FSearchText))。在Mac上看起來真的不一樣了...... - .-其他想法? – Kathara

回答

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 

不知怎的,這作品。當我第一次在Word中嘗試「Dim xlWB As Excel.Workbook」時,我總會遇到運行時錯誤。當我在Excel中嘗試「Dim Doc As Word.Document」時,雖然我從來沒有得到一個錯誤......非常奇怪,但仍然設法讓它結束。

如果您對此有任何疑問,我將很樂意爲您提供幫助,如果有些事情我可以以更好的方式重寫,請不要猶豫,以發表評論。

感謝您的支持:)

相關問題