How to resolve the algorithm OLE automation step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm OLE automation step by step in the Go programming language

Table of Contents

Problem Statement

OLE Automation   is an inter-process communication mechanism based on   Component Object Model   (COM) on Microsoft Windows.

Provide an automation server implementing objects that can be accessed by a client running in a separate process.
The client gets a proxy-object that can call methods on the object. The communication should be able to handle conversions of variants to and from the native value types.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm OLE automation step by step in the Go programming language

This Go program uses the github.com/go-ole/go-ole library to create and manipulate a Microsoft Word document. It first initializes the COM library, then creates an instance of the Word application and sets its visibility to true. It then adds a new document, paragraph, and range to the document and sets the text of the range to "This is a Rosetta Code test document." After waiting for 10 seconds, it saves the document, closes it, quits Word, and uninitializes the COM library.

Here is a detailed breakdown of the code:

  • ole.CoInitialize(0): Initializes the COM library.
  • unknown, _ := oleutil.CreateObject("Word.Application"): Creates an instance of the Word application. The unknown variable is of type ole.IDispatch, which is the base interface for all COM objects. The second return value, _, is a placeholder for the error variable, which we are not using in this case.
  • word, _ := unknown.QueryInterface(ole.IID_IDispatch): Queries the unknown object for the IDispatch interface. The word variable is now a pointer to an IDispatch interface.
  • oleutil.PutProperty(word, "Visible", true): Sets the Visible property of the Word application to true, making it visible.
  • documents := oleutil.MustGetProperty(word, "Documents").ToIDispatch(): Gets the Documents property of the Word application and converts it to an IDispatch interface.
  • document := oleutil.MustCallMethod(documents, "Add").ToIDispatch(): Adds a new document to the Word application and converts it to an IDispatch interface.
  • content := oleutil.MustGetProperty(document, "Content").ToIDispatch(): Gets the Content property of the document and converts it to an IDispatch interface.
  • paragraphs := oleutil.MustGetProperty(content, "Paragraphs").ToIDispatch(): Gets the Paragraphs property of the content and converts it to an IDispatch interface.
  • paragraph := oleutil.MustCallMethod(paragraphs, "Add").ToIDispatch(): Adds a new paragraph to the document and converts it to an IDispatch interface.
  • rnge := oleutil.MustGetProperty(paragraph, "Range").ToIDispatch(): Gets the Range property of the paragraph and converts it to an IDispatch interface.
  • oleutil.PutProperty(rnge, "Text", "This is a Rosetta Code test document."): Sets the Text property of the range to "This is a Rosetta Code test document."
  • time.Sleep(10 * time.Second): Sleeps for 10 seconds.
  • oleutil.PutProperty(document, "Saved", true): Sets the Saved property of the document to true. This saves the document.
  • oleutil.CallMethod(document, "Close", false): Closes the document. The second argument, false, indicates that the document should not be saved again.
  • oleutil.CallMethod(word, "Quit"): Quits the Word application.
  • word.Release(): Releases the word object.
  • ole.CoUninitialize(): Uninitializes the COM library.

Source code in the go programming language

package main

import (
    "time"
    ole "github.com/go-ole/go-ole"
    "github.com/go-ole/go-ole/oleutil"
)

func main() {
    ole.CoInitialize(0)
    unknown, _ := oleutil.CreateObject("Word.Application")
    word, _ := unknown.QueryInterface(ole.IID_IDispatch)
    oleutil.PutProperty(word, "Visible", true)
    documents := oleutil.MustGetProperty(word, "Documents").ToIDispatch()
    document := oleutil.MustCallMethod(documents, "Add").ToIDispatch()
    content := oleutil.MustGetProperty(document, "Content").ToIDispatch()
    paragraphs := oleutil.MustGetProperty(content, "Paragraphs").ToIDispatch()
    paragraph := oleutil.MustCallMethod(paragraphs, "Add").ToIDispatch()
    rnge := oleutil.MustGetProperty(paragraph, "Range").ToIDispatch()
    oleutil.PutProperty(rnge, "Text", "This is a Rosetta Code test document.")

    time.Sleep(10 * time.Second)

    oleutil.PutProperty(document, "Saved", true)
    oleutil.CallMethod(document, "Close", false)
    oleutil.CallMethod(word, "Quit")
    word.Release()

    ole.CoUninitialize()
}


  

You may also check:How to resolve the algorithm DNS query step by step in the Scheme programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Shen programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Ceylon programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the Racket programming language
You may also check:How to resolve the algorithm Simple database step by step in the Racket programming language