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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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

Link to post
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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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

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

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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

Link to post
Share on other sites

Thanks, function will be removed.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • 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/AutoItObject-Internal/tree/master/Examples
      Version: 3.0.0
      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.
    • By nacerbaaziz
      hello autoit team
      please
      i tried to create a SPVoice object 
      when i create the object and tried to use this function SetAlertBoundary
      it gave me an error
      please where is the problem?
      here is the code
      local $o_Sapi_OBJ = ObjCreate("SAPI.SpVoice") if IsObj($o_Sapi_OBJ) then $o_Sapi_OBJ.SetAlertBoundary("SPEI_PHONEME") endIf note i got the function name from microsoft web site
       
      https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms719784(v=vs.85)
      i have windows10
    • By Colduction
      Hi AutoIt Programmers!

      I've seen a code that gives Name of Group by writing it's group's SID, but this one response very slower than Windows Command-Line WMIC
       
      Func _GetLocalGroupName($sSID = 'S-1-5-18') $objWMIService = ObjGet ("winmgmts:\\" & @ComputerName & "\root\cimv2") $colItems = $objWMIService.ExecQuery('SELECT Name FROM Win32_Group where SID="' & $sSID & '"') For $GroupNames in $colItems MsgBox (0,"",$GroupNames.Name) ExitLoop Next EndFunc I don't want call and use any third-party programs even CMD, i just want use from Windows API, netapi32.dll or AutoIt Functions (Standalone).
      Do you have any idea to improve speed/performance of this code? I'll happy of your comments
       
       
       
      ====================== SOLOUTION by @Subz ======================
       
    • By xiantez
      This script used to work on an older version of AutoIT. Currently I am running AutoIT v3.3.14.5 and it's failing.
      Func PublicIP() ;Post public facing IP address Local $url = 'https://www.google.com/search?client=opera&q=what+is+my+ip&sourceid=opera&ie=UTF-8&oe=UTF-8' Local $getIPaddress = BinaryToString(InetRead($url)) Local $sStart = 'clamp:2">' Local $sEnd = '</div>' Local $ipaddress = _StringBetween($getIPaddress, $sStart, $sEnd For $i In $ipaddress MsgBox(0, 'External IP', "Your public IP address is " & $i) Next EndFunc ;==>PublicIP The console output shows:
      "C:\Users\user\Documents\AutoIT\Scripts\WSI Tools.au3" (197) : ==> Variable must be of type "Object".: For $i In $ipaddress For $i In $ipaddress^ ERROR ->14:12:16 AutoIt3.exe ended.rc:1 +>14:12:16 AutoIt3Wrapper Finished. >Exit code: 1 Time: 9.811
    • By tuffgong
      Good morning. I have a system I am trying to automate that works like this: user fills a column in an Excel spreadsheet with values they would like printed and saves it to a folder on their desktop, they start the script and it formats their data into a text file (adding a prefix) and sends the text file as a .bch file where it needs to go. This is working:
      #include <Array.au3> #include <Excel.au3> #include <File.au3> #include <MsgBoxConstants.au3> Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) barcodePrint() Func barcodePrint() $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) FileOpen($sFilename, $FO_OVERWRITE) ;Global $oWorkbook = _Excel_BookAttach($oExcel) Global $sSTCArray = _Excel_RangeRead($Read) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, "!StaticShelving1x3_ZPL," & $sSTCArray[$i]) Next _Excel_Close($oExcel) FileMove($sFilename, "***file path***\print.bch") EndFunc However, it only works if the user first formats the spreadsheet to text. I want to automate that. From what I have read it appears AutoIt does not like formatting cells that already have values. True? Can I pull the values from an un-formatted (default GENERAL format) spreadsheet and go straight to my text file? I have also considered opening a second spreadsheet, formatting it, and copying the values over. Like this:
        
      Global $sPrefix Global $oPath Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) Global $aArray Global $bExcel _Excel_BookNew($bExcel) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 $aArray = IniReadSection("***File path***\barcode.ini", "stc/rvt/mgm") $sPrefix = $aArray[1][1] $oPath = $aArray[2][1] barcodePrint() EndSwitch WEnd Func barcodePrint() $oWorkbook = _Excel_BookOpen($bExcel, @DesktopDir & "\Labels\print.xlsx") $oWorkbook.ActiveSheet.Columns("A").NumberFormat = "@" Local $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) $oCopy = _Excel_RangeRead($sWorkbook) _Excel_RangeWrite($oWorkbook, Default, $oCopy) FileOpen($sFilename, $FO_OVERWRITE) $sSTCArray = _Excel_RangeRead($Read,"Default","Default",3) ;_ArrayDisplay($sSTCArray) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, $sPrefix & $sSTCArray[$i]) Next _Excel_Close($oExcel) ;FileMove($sFilename, $oPath) Exit EndFunc This does not like the formatting of $oWorkbook: "Variable must be of type 'Object'".  Do I need this second sheet? If so, how can I format it? Is there a better way to get the Excel values into a .txt file? Any ideas would be appreciated. Thanks!
×
×
  • Create New...