Forum Discussion

eykxas's avatar
eykxas
Regular Contributor
2 years ago

Setting value in word doc

Hi everyone,

 

I'm working on some Word automation, and I have this kind of field in my doc :

 

I can read the value of these fields, but how can I change their values ? After getting the UIAObject, I don't know what to do to set a new value.

Thx

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I'm not sure how to get UIAObjects to appear in Word.

     

    You might be able to use Sys.OleObject("Word.Application") and modify the field values

    • eykxas's avatar
      eykxas
      Regular Contributor

      Unfortunately I can't get Sys.OleObject to work properly. I get this error on line 2 :


      1. var objWordApp = Sys.OleObject("Word.Application");
      2. var wordDoc = objWordApp.ActiveDocument;

      "Command not available: no document is open."

       

       

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    You will have to open the document like,

     

    function main()
    {
        var MsWord = Sys.OleObject("Word.Application");
        MsWord.Visible = true;
        // https://learn.microsoft.com/en-us/office/vba/api/word.documents.open
        MsWord.Documents.Open("C:\\Temp\\Word.docx");
    }

     

    Refer to https://learn.microsoft.com/en-us/office/vba/api/overview/word/object-model to try and gain access to the objects you need. Hopefully, there should be examples on the internet, most likely written in VB Script, but it should give you a rough idea.

  • eykxas's avatar
    eykxas
    Regular Contributor

    I cannot do this. The docx file is not located on the local pc, but on a distant server loaded by a custom plugin. (there is no tmp file, everything is done in ram). Si I can't call "Documents.Open"

     

    in the meanwhile, I work with LLP but I'm not happy with this.