Jump to content
Sign in to follow this  
water

Set property of an object passed as variable

Recommended Posts

water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - 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
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
stormbreaker

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
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - 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
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
stormbreaker

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
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - 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
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
stormbreaker

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
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - 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
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
stormbreaker

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
hannes08

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
trancexx

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
water

... 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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - 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
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - 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
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
trancexx

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
water

Thanks, function will be removed.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - 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
 
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  

  • 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/AutoItObject-Internal/tree/master/Examples
      Version: 2.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.
    • xiantez
      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
    • tuffgong
      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!
    • marcoauto
      By marcoauto
      Ciao
      I would like to control an ATEM Video Mixer from autoit. I downloaded his SDK which is written in c ++ and I found the sequences to interface, but I was not able to convert the script to self.
      The instructions say to follow this sequence:
      and to connectTo with C++ is:
      string address = "192.168.1.240"; _BMDSwitcherConnectToFailure failureReason = 0; IBMDSwitcher switcher = null; var discovery = new CBMDSwitcherDiscovery(); discovery.ConnectTo(address, out switcher, out failureReason); From Blackmagic SDK:
      IBMDSwitcherDiscovery::ConnectTo method
      The ConnectTo method connects to the specified switcher and returns an IBMDSwitcher object interface for the switcher.
      Syntax HRESULT ConnectTo (string deviceAddress, IBMDSwitcher** switcherDevice, BMDSwitcherConnectToFailure* failReason); Parameters: deviceAddress in Network hostname or IP address of switcher to connect to. switcherDevice out IBMDSwitcher object interface for the connected switcher. failReason out Reason for connection failure as a BMDSwitcherConnectToFailure value. So, I have I tried these solutions but with non success:
      $DllName =@ScriptDir&"\BMDSwitcherAPI.dll" $result = DllCall($DllName, "none", "IBMDSwitcherDiscovery::ConnectTo" & @CRLF) ConsoleWrite("DLLCall Result: " & $result & @CRLF) and I have tried also create an Object (That I think is the best way solution):
      #include <MsgBoxConstants.au3> $oSwitcher=ObjCreate("IBMDSwitcher") If IsObj($oSwitcher) Then MsgBox(64, "", "Object $oSwitcher created successfully") EndIf $oAtem=ObjCreate("IBMDSwitcherDiscovery") If IsObj($oAtem) Then MsgBox(64, "", "Object $oAtem created successfully") EndIf $failureReason =ObjCreate("_BMDSwitcherConnectToFailure") If IsObj($failureReason) Then MsgBox(64, "", "Object $failureReason created successfully") EndIf $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Install a custom error handler Global $errore,$oSwitcher1 $oAtem.ConnectTo("192.168.1.36",$oSwitcher,$failureReason); ; This is the custom error handler Func MyErrFunc() $HexNumber = Hex($oMyError.number, 8) MsgBox(0, "", "We intercepted a COM Error !" & @CRLF & _ "Number is: " & $HexNumber & @CRLF & _ "Windescription is: " & $oMyError.windescription & @CRLF & _ "Source is: " & $oMyError.source & @CRLF & _ "Description is: " & $oMyError.description & @CRLF & _ "Helpfile is: " & $oMyError.helpfile & @CRLF & _ "Helpcontext is: " & $oMyError.helpcontext & @CRLF & _ "Lastdllerror is: " & $oMyError.lastdllerror & @CRLF & _ "Scriptline is: " & $oMyError.scriptline) EndFunc ;==>MyErrFunc But the result is:
      We intercepted a COM Error !
      Number is: 000000A9
      Windescription is: Variable must be of type 'Object'.
      Source is: 
      Description is: 
      Helpfile is: 
      Helpcontext is: 
      Lastdllerror is: 0
      Scriptline is: 17
      The BMDSwitcherAPI.dll is registered in system. Can someone help me?
      Grazie
      Marco
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I'm working on a little project of mines, and I was trying to use WMI Object.
      The question which I don't find an answer is: 
      Once I do the query with WMI Object, something like "SELECT * FROM Win32_LogonSession", instead of specify the field of the collection returned, ( i.e. $colItems.Caption ), can I loop though each property and each value of the property, writing so one row of code only?
      Hope my question was clear enough.
      Thanks in advance.

      Best Regards.
×