2014-04-12 29 views
0

我一直在創造的縮寫發現宏,將坐在字的自定義工具欄上。運行時,它會搜索文檔中的縮略詞並將它們放在一個表中。我想包括一些用戶的形式,這樣,當宏查找的首字母縮寫,用戶可以選擇預定義的定義(從Excel文件了),或者輸入自己的新的(我知道多首字母縮寫詞的含義是不可取的,但它發生)。從取決於按鈕VBA用戶形式返回不同的值按

反正我堅持。我用三個按鈕創建了一個用戶表單。文本輸入和標籤。現在,我已經成功地設定被發現,但是我似乎無法得到按鈕來改變一個變量,userChoice,如果適用保存新輸入定義的縮寫標籤文本。

下面

是測試宏我一直在嘗試這一點上

Sub userFormTest() 

Dim objExcel As Object 
Dim objWbk As Object 
Dim rngSearch As Object 
Dim rngFound As Object 
Dim targetCellValue As String 
Dim userChoice As Integer 

Set objDoc = ActiveDocument 
Set objExcel = CreateObject("Excel.Application") 
Set objWbk = objExcel.Workbooks.Open("C:\Users\Dave\Documents\Test_Definitions.xlsx") 
objExcel.Visible = True 
objWbk.Activate 

With objWbk.Sheets("Sheet1") 

Set rngSearch = .Range(.Range("A1"), .Range("A" & .Rows.Count).End(-4162)) 

Set rngFound = rngSearch.Find(What:="AA", After:=.Range("A1"), LookAt:=1) 



If rngFound Is Nothing Then 

    UserForm1.Label1.Caption = "Acronym: AA" & vbCr & _ 
           "Definition: Not found, please enter a definition below" & vbCr & _ 
           "   or choose to ignore this acronym" 
    UserForm1.Show 

'an if statement here so that if the add button was pressed it adds to doc etc 

Else 

    targetCellValue = .Cells(rngFound.Row, 2).Value 

    UserForm2.Label1.Caption = "Acronym: AA" & vbCr & _ 
           "Definition: " & targetCellValue 
    UserForm2.Show 

'an if statement here so that if the add button was pressed it adds to doc etc 

End If 

End With 

objWbk.Close Saved = True 

Set rngFound = Nothing 
Set rngSearch = Nothing 
Set objWbk = Nothing 
objExcel.Visible = True 
Set objExcel = Nothing 
Set objDoc = Nothing 

End Sub 

我也知道這可能在button_click()來完成潛艇,但是我已經把所有的文件中的其他宏開等。或者是否有可能鏈接到那些已經打開的文檔?說實話,無論哪種方式,我寧願回到主宏,只是使用的形式向用戶輸入。

回答

相關問題