2017-08-30 41 views
1

リストボックスから項目をプログラムで選択しようとしています。XLwings ListBoxで項目を選択しています

Sheet1.MyListBox.Selected(0) = True 

するか、実行して項目を選択した場合、私は簡単にチェックすることができたXLwingsで

Sheet1.MyListBox.Selected(0) = False 

選択を解除する:VBAで私が行います

>> wb.Sheets(1).api.MyListBox.Selected(0) 
True 

をしかし、私ならば値を割り当てようとするとエラーが発生します:

wb.Sheets(1).api.MyListBox.Selected(0) = True 
    File "<ipython-input-156-9ef416a5660f>", line 1 
    wb.Sheets(1).api.MyListBox.Selected(0) = True 
               ^
SyntaxError: can't assign to function call 

誰かが助けてくれることを願っています。

PS:デザインモード(Excelツールバーで選択した場合)でも、何らかの理由で最初の行がPythonで動作しないことがわかりました。

答えて

0

Comment: What if ... the method was not implemented? ... use ListBox.Selected(0) = True and find it is impossible with xlwings

現在のライブラリにはListBox.Selected(Int32)はありません。 したがって、xlwingsに実装する必要があると仮定します。
.apiインターフェイスを使用すると、ライブラリが提供するものだけを使用できます。


Question: select items programmatically from a listbox

wb.Sheets(1).api.MyListBox.SetSelected(0, True) 

ListBox.SetSelected Method (Int32, Boolean)

Selects or clears the selection for the specified item in a ListBox.
Parameters
index Type: System.Int32
The zero-based index of the item in a ListBox to select or clear the selection for.
value Type: System.Boolean
true to select the specified item; otherwise, false.

+0

本当にありがとうございました!それがトリックでした。何らかの理由でメソッドが実装されていない場合はどうなりますか?私はそれから 'ListBox.Selected(0)= True'を使う必要があり、xlwingsでは不可能であることがわかります... – Yona

+0

'wb.Sheets(1).api.MyListBox.Selected(0)'と 'その項目のステータスに応じて「True」または「False」を選択します。しかし、VBAでは、 'MyListBox.Selected(0)= True'の値を割り当てることができますが、それはxlwingsを使っては不可能です。あなたは別の方法(SetSelected)を使って問題を解決しました。最後のコメントは、wb.Sheets(1).api.MyListBox.Selected(0)= True(SetSelectedがインスタンスに存在しないため)を強制的に使用するようになった場合の対処方法に関する質問でした。 – Yona

+0

@Yona:なぜあなたは_ **強制** _ですか? VBAに '.Selected(0)' ** **のみが存在するという事実は_historical_または_future_かもしれません。なぜなら、MSだけがその理由を知っているからです。 – stovfl

関連する問題