water Posted June 4, 2012 Posted June 4, 2012 How can I set the value of a property by passing the property name as variable? Like: $sPropertyName = "Username" $oWord_Appl.Username = "Test" ; Works $iResult = Assign("oWord_Appl.Username", "Test", 4) ; Returns 0: unable to create/assign the variable $iResult = Assign("oWord_Appl." & sPropertyName, "Test", 4) ; Returns 0: unable to create/assign the variableIs it possible at all? If yes, how? Thanks in advance! My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
stormbreaker Posted June 4, 2012 Posted June 4, 2012 Water, I think you should declare $OWord_Appl first, like: Global $OWord_Appl I read through Assign example and the variables to be used were declared first. Good luck... ---------------------------------------- :bye: Hey there, was I helpful? ---------------------------------------- My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1
water Posted June 4, 2012 Author Posted June 4, 2012 Variable $oWord_Appl is already declared and is set to the Word application object. I would like to set a property of this object by passing the name to a function. This way I can keep the function flexible and don't have to list all valid properties in the function. If a new version of Microsoft Word adds new properties my function needs not to be changed. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
stormbreaker Posted June 4, 2012 Posted June 4, 2012 Assign function requires the variable to be declared(do a forum search and you will get it, its an old thread of around 2008), so, if its a property value, may be you could try something like: Global $myvar Local $myvar.property ;your assign function goes now ---------------------------------------- :bye: Hey there, was I helpful? ---------------------------------------- My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1
water Posted June 4, 2012 Author Posted June 4, 2012 (edited) MKISH, my problem is not with the $myvar part but with "property". Example: Global $oWord_Appl Set_Property($oWord_Appl, "Username", "Test") Func Set_Property($oObject, $property, $value) $oObject.$property = $value ; <== How to write this line? EndFunc Edited June 4, 2012 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
stormbreaker Posted June 5, 2012 Posted June 5, 2012 Water, taking my example, I would write the assign function as: Assign('myvar.property', 'value', 4) ---------------------------------------- :bye: Hey there, was I helpful? ---------------------------------------- My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1
water Posted June 5, 2012 Author Posted June 5, 2012 MKISH, If you have Microsoft Word installed then here is a small reproducer script to show you what I need. The problem is that the name of the property I need to change is passed as parameter to the function. Global $oWord_Appl = ObjGet("", "Word.Application") If @error Then $oWord_Appl = ObjCreate("Word.Application") If @error Then Exit MsgBox(16, "Word property", "Error starting Word!") EndIf _ChangeUsername($oWord_Appl, "Username", "Test") $oWord_Appl.Quit() Exit Func _ChangeUsername($oAppl, $sProperty, $sValue) MsgBox(64, "Word property", 'Current value of Word application property "Username": ' & $oWord_Appl.Username) Local $iResult = Assign($oAppl.$sProperty, $sValue, 4) ; <==== This statement needs to be changed to make it work! If $iResult = 1 Then MsgBox(64, "Word property", 'New value of Word application property "Username": ' & $oWord_Appl.Username) Else MsgBox(16, "Word property", "Could not set $oAppl.UserName!") EndIf Return EndFunc My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
stormbreaker Posted June 5, 2012 Posted June 5, 2012 What I get now is that you are trying to somehow convert '$object' to 'object' so that you can use it with assign function. Right? I think you could try using assign function without the flag value 4. Flag 4 requires that your variable is declared (using local or global statements) func assigntoword($property, $value) Assign('oAppl.' & $property, '' & $value) endfunc ;don't use flag here and try if it works, sorry i am currently travelling ---------------------------------------- :bye: Hey there, was I helpful? ---------------------------------------- My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1
hannes08 Posted June 5, 2012 Posted June 5, 2012 (edited) I tried what both of you wrote and I even fail with only getting properties: #include <word.au3> Global $oWord_Appl $oWord_Appl = _WordCreate() ConsoleWrite(Get_Property($oWord_Appl, "Username") & @CRLF) Func Get_Property($oObject, $property) Local $str = "oObject." & $property Local $result = Execute($str) Local $err = @error ConsoleWrite($str & @CRLF) ConsoleWrite($result & @TAB & $err & @CRLF) Return $result EndFunc So I'm not sure that you can get it to work anyhow, but maybe if you findout how to read the properties you can also find out how to write the properties. @MKISH, I don't think the issue is with declarations of variables here... Edited June 5, 2012 by hannes08 Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
trancexx Posted June 5, 2012 Posted June 5, 2012 MKISH, If you have Microsoft Word installed then here is a small reproducer script to show you what I need. The problem is that the name of the property I need to change is passed as parameter to the function. Global $oWord_Appl = ObjGet("", "Word.Application") If @error Then $oWord_Appl = ObjCreate("Word.Application") If @error Then Exit MsgBox(16, "Word property", "Error starting Word!") EndIf _ChangeUsername($oWord_Appl, "Username", "Test") $oWord_Appl.Quit() Exit Func _ChangeUsername($oAppl, $sProperty, $sValue) MsgBox(64, "Word property", 'Current value of Word application property "Username": ' & $oWord_Appl.Username) Local $iResult = Assign($oAppl.$sProperty, $sValue, 4) ; <==== This statement needs to be changed to make it work! If $iResult = 1 Then MsgBox(64, "Word property", 'New value of Word application property "Username": ' & $oWord_Appl.Username) Else MsgBox(16, "Word property", "Could not set $oAppl.UserName!") EndIf Return EndFunc water that code is lame. Could you show what exactly you want to do? Why would you need such function? ♡♡♡ . eMyvnE
water Posted June 5, 2012 Author Posted June 5, 2012 ... but maybe if you find out how to read the properties ...Reading the properties is "easy" $sProperty = Execute("$oWord_Appl." & $sProperty)You missed the $ sign. Parsix 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
water Posted June 5, 2012 Author Posted June 5, 2012 trancexx, thanks for replying. I'm about to rewrite function _WordPropertySet. You can pass a single property name as parameter. The Word application object has no collection for all properties so you have to access them by name. The function compares the parameter with all the properties it can set. If a match is found the appropriate property is set. This leads to a lot and unflexible code. You can not process all possible properties now - only those defined in the function. Something like: Switch $s_Property Case "activeprinter" $o_object.Application.ActivePrinter = $v_newvalue Case "screenupdating" $o_object.Application.ScreenUpdating = $v_newvalue Imagine a new Word version offers new properties. The function has to be changed to process them. If the name of the property could be replaced by the parameter the function could be reduced to a few lines and would be flexible enough for future changes of Word. I hope I could make myself clear My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
trancexx Posted June 5, 2012 Posted June 5, 2012 trancexx, thanks for replying. I'm about to rewrite function _WordPropertySet. You can pass a single property name as parameter. The Word application object has no collection for all properties so you have to access them by name. The function compares the parameter with all the properties it can set. If a match is found the appropriate property is set. This leads to a lot and unflexible code. You can not process all possible properties now - only those defined in the function. Something like: Switch $s_Property Case "activeprinter" $o_object.Application.ActivePrinter = $v_newvalue Case "screenupdating" $o_object.Application.ScreenUpdating = $v_newvalue Imagine a new Word version offers new properties. The function has to be changed to process them. If the name of the property could be replaced by the parameter the function could be reduced to a few lines and would be flexible enough for future changes of Word. I hope I could make myself clear Paradigms in collision. _WordPropertySet shouldn't exist at all. Mindset needs changed. Objects should be used as intended. Really, I have changed AutoIt internally a whole lot for it to give the user a proper interface for object manipulation. Use it. ♡♡♡ . eMyvnE
water Posted June 5, 2012 Author Posted June 5, 2012 Thanks, function will be removed. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now