Sign in to follow this  
Followers 0
water

Set property of an object passed as variable

14 posts in this topic

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by hannes08

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Thanks, function will be removed.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • genius257
      By genius257
      I've made a library, based on AutoItObject UDF with the goal of implementing getter and setter functionality and make it possible to define new object properties in as few steps as possible.
      Thank you to @trancexx for getting me on the right track, and all users in Hooking into the IDispatch interface for the code to get me going.
      If I've forgotten to add credit, please let me know
      Example:
      #include "AutoItObject_Internal.au3" $myCar = IDispatch() $myCar.make = 'Ford' $myCar.model = 'Mustang' $myCar.year = 1969 $myCar.__defineGetter('DisplayCar', DisplayCar) Func DisplayCar($oThis) Return 'A Beautiful ' & $oThis.parent.year & ' ' & $oThis.parent.make & ' ' & $oThis.parent.model EndFunc MsgBox(0, "", $myCar.DisplayCar) More examples: https://github.com/genius257/AutoIt-projects/tree/master/AutoItObject Internal/Examples
      Version: 1.0.3
      AutoItObject_Internal.au3
      Documentation:
       

      Edit2 (19th March 2017):
      First of all, sorry about the lack of updates on this project. I always start too many projects and end up ignoring old projects, if I run into problems ^^'.
      So I've started moving my AutoIt scripts to GitHub. I will still post the most recent script version here.
    • ur
      By ur
      The below functions are to write data to a configuration or property file and also read them back.
      BuildLocation:C:\Build BuildExe : setup.exe Release:r9.7 Silent:No InstallPath:default Compare :No MartUpgrade :Yes Bit:64 ERwinUpgrade:No License_File:150416-1952 Navigator (ca.com).lic You can change the character from ":" to "=" or whatever based on your requirement and update the code also accordingly.
      Property.au3
    • ur
      By ur
      Hi,
       
      I have a property file format configuration file for our project.
      The sample file is as below.
      BuildLocation:C:\Build BuildExe:erwin Data Modeler r9.7 (64-bit)_2378.exe Release:r9.64.02 Silent:No InstallPath:default Compare :No MartUpgrade :Yes Bit:64 ERwinUpgrade:No License_File:150416-1952 Navigator (ca.com).lic To read this file, I am using below code.
      Func readConfig($sFilePath,$intStartCode) ;Usage: MsgBox(0,"Silent",readConfig(@ScriptDir&"\Config.txt","Silbent")) ;$sReplaceText = "Mani Prakash" ;$sFilePath = "C:\Users\KIRUD01\Desktop\Config.txt" ;$intStartCode = "BuildExe" $arrRetArray = "" $s = _FileReadToArray($sFilePath, $arrRetArray);Reading text file and saving it to array $s will show status of reading file.. For $i = 1 To UBound($arrRetArray)-1 $line = $arrRetArray[$i];retrieves taskengine text line by line If StringInStr($line, $intStartCode) Then ConsoleWrite ("Starting point "& $line & @CRLF) return StringStripWS(StringSplit($line,":")[2],$STR_STRIPLEADING + $STR_STRIPTRAILING ) EndIf if $i = UBound($arrRetArray)-1 then return "Not Found" Next EndFunc The above code is working to read the particular key value.
      But problem is , if I try to read the key "Bit" it is giving the value of key "BuildExe" as the line contains the word "bit"..
      Can you suggest how to do this. 
       
      If possible I need to fix writeConfig also.
       
      Func writeConfig($sFilePath,$intStartCode,$sReplaceText) ;$sReplaceText = "Mani Prakash" ;$sFilePath = "C:\Users\KIRUD01\Desktop\Config.txt" ;$intStartCode = "BuildExe" $arrRetArray = "" $s = _FileReadToArray($sFilePath, $arrRetArray);Reading text file and saving it to array $s will show status of reading file.. $intStartingPointFound = 0 For $i = 1 To UBound($arrRetArray)-1 $line = $arrRetArray[$i];retrieves taskengine text line by line If StringInStr($line, $intStartCode) Then $intStartingPointFound = 1;if found the starting point of the module to copy then set this variable to 1 ConsoleWrite ("Starting point " & @CRLF) $arrRetArray[$i] = $intStartCode & ": " & $sReplaceText ExitLoop EndIf if $i = UBound($arrRetArray)-1 then ConsoleWrite("Not Found" & @CRLF) Next _FileWriteFromArray ($sFilePath, $arrRetArray,1) EndFunc  
    • WoodGrain
      By WoodGrain
      Hi All,
      I've coded the small script below, but it can't seem to get the instance of Windows Media player as it keeps going to @error, I've not used com objects before so any assistance would be appreciate. I already have WMP open and minimised. I retrieved "WMPlayerApp" from the AutoIT info tool, I've included a copy below.
      I'm using these sources:
      https://msdn.microsoft.com/en-us/library/dd564085.aspx
      https://msdn.microsoft.com/en-us/library/dd564018.aspx
      $oWMP = ObjGet("", "WMPlayerApp") If @error Then MsgBox(0, "Can't get WMP", "Couldn't connect to the WMP instance") Exit EndIf $wmpPlayState = $oWMP.playState MsgBox(0, "Play State", $wmpPlayState) $wmpSongName = $oWMP.currentMedia.name MsgBox(0, "Play State", $wmpSongName) I've also seen references to the below, but I want to get an existing open WMP:
      ObjCreate("wmplayer.OCX") and have looked at the WMP.udf but can't see how it will do either of the functions I've coded above.
       
    • harvester2001
      By harvester2001
      Hi
      I need help, I found script to check computer model. I want use it in loop to chceck multiple pc`s (targets.txt with computer names).
      But when i try use inside loop i get error: Variable must be of type "Object" and I dont know how to fix this
      Plz help
       
      Local $targets = @ScriptDir& "\targets.txt" Local $log_name = "\scan.log" $msgBox = MsgBox(4, "Scan", "need tergets.txt") Example() Func Example() If $msgBox = 7 Then exit EndIf If Not FileExists($targets) Then MsgBox($MB_SYSTEMMODAL, "", "File: targets.txt - no exist !") Exit EndIf FileOpen($targets, 0) Global $arr[1000] ReDim $arr[_FileCountLines($targets)+1] For $i = 1 to _FileCountLines($targets) $line = FileReadLine($targets, $i) $arr[$i] = $line ; chceck if pc is online Local $iPing = Ping($arr[$i], 250) If $iPing Then ; ONLINE Local $strComputer = $arr[$i] $colItems = "" $Output="" $Output = $Output & "Computer: " & $strComputer & @CRLF $Output = $Output & "==========================================" & @CRLF $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _ "0x10" + "0x20") If IsObj($colItems) then For $objItem In $colItems $Output = $Output & "Vendor: " & $objItem.Vendor & @CRLF $Output = $Output & "SN: " & $objItem.IdentifyingNumber & @CRLF $Output = $Output & "Name: " & $objItem.Name & @CRLF $Output = $Output & "UUID: " & $objItem.UUID & @CRLF if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop $Output="" Next Else Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_ComputerSystemProduct" ) Endif Else ;OFFLINE _FileWriteLog(@ScriptDir & $log_name, $arr[$i]&" OFFLINE") EndIf Next EndFunc