Jump to content

VBA to AUTOIT (counting MSWord fields)


Recommended Posts

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

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.au3

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.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
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

5 hours ago, OmegaGeneral said:

Hi and thanks,

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

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:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

How should the new Beta solve this problem?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.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
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...