Jump to content

Recommended Posts

Posted

Hi, I'm in the process of trying to take some VBA code and converting it into an AUTOIT EXE, as to move away from needing EXCEL or WORD in the background to run the code.

The first hurdle I have hit is accessing/ counting the fields within a word document

Dim wrdapp As Object
    Set wrdapp = GetObject(, "Word.Application")

Dim fc As Integer
    fc = wrdapp.ActiveDocument.Fields.Count
'
    If fc < 1 Then
        Set wrdapp = Nothing
        Exit Sub
    End If

Any help would be appreciated.

Thanks

 

Posted

objcreate is your answer

https://www.autoitscript.com/autoit3/docs/functions/ObjCreate.htm

but probably easier is in your VBA macro to add wrdApp.visible = false then you won't see excel or word in the background.

or save your vba code as a vbscript file then you are 99.99% compatible in an easy way to convert

Anyway if you make it an autoit exe you still have word/excel in memory running in the background.

 

Posted

Using AutoIt instead of VBA will allow you to automate multiple applications within a single script.  That is the main advantage of going with AutoIt.  As per your request, it is very simple :

Local $oWord = ObjGet("", "Word.Application") ; get current instance of Word
Local $fc = $oWord.ActiveDocument.Fields.Count
ConsoleWrite($fc & @CRLF)
Local $wc = $oWord.ActiveDocument.Words.Count
ConsoleWrite($wc & @CRLF)

You can also use the Word UDF to simplify your life...

Posted

AutoIt has nothing to do with your problem.  AutoIt does not count fields, it is calling a method or property of a COM object.  You will need to post the full script that produce the error so we can see what is the issue.  I tested my script, of course, and on my hand it is working perfectly...

Posted

Thanks again,

 

Aside from the existing script, if I just create a fresh autoIT script and copy/paste your above code, the same error is produced.

See the script attached.

Now I know your code must be sound as all my research has lead to the same sort of code but it never runs.

I always get the error surrounding 'ActiveDocument.Fields'

Perhaps you can share the test script you made for me to try? Maybe it is the same as mine?

fields test.au3Fetching info...

Posted

If I have no current document open, I get the same error as you.  You should check for errors.  (tested both Win7 and Win10)

Local $oWord = ObjGet("", "Word.Application")
If Not IsObj($oWord) Then Exit MsgBox (0,"Error", "You need to have an opened word document")
Local $fc = $oWord.ActiveDocument.Fields.Count
ConsoleWrite($fc & @CRLF)

ps. use tags when you post code, as described in the link

 

Posted

Please use the Word UDF that comes with AutoIt. _Word_Create connects to a running instance or - if none exists - it starts up Word.
The functions of the Word UDF do all error checking and set @error and @extended in case of a problem.
The objects returned by the functions can be used in your script in case hte Word UDF does not provide the needed function (count fields etc.).

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 2/28/2022 at 4:50 PM, OmegaGeneral said:

Hi and thanks,

1791109571_activedocumenterror.PNG.f67e957ba55a26f98217a78dc50e0528.PNGautoit appears to fail at accessing the fields, see the screenshot

Expand  

Try to use current AutoIt 3.3.15.5 beta

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

How should the new Beta solve this problem?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Understand that when you run from VBA you already have

  • application object
  • activedocument and all other kinds of active.... something variables are available as most likely you have a document open where your VBA lives in.
  • Above 2 things you first have to arrange when running from AutoIt

As given above maybe use the word udf.

Excellent examples over here https://www.autoitscript.com/autoit3/docs/libfunctions/_Word_DocOpen.htm

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.
×
×
  • Create New...