Jump to content

Set property of an object passed as variable


Recommended Posts

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 variable
Is it possible at all? If yes, how?

Thanks in advance!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by hannes08
Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

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

Link to comment
Share on other sites

... 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.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Thanks, function will be removed.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...