Sign in to follow this  
Followers 0
Doc Poster

Accessing Word VBA variables

4 posts in this topic

Searched a ton of posts about Word automation, but can't find one on this: I can't seem to access public variables declared in VBA modules.

I am calling an AutoIt script from VBA code inside of Word 2000. Once the script completes, I have it notify Word by calling a VBA macro. I have to do this because Word doesn't wait for an executable to complete before moving on. Here is my simplified code in VBA:

CODE
Public strAutoItResult As String

'----------------------------------------------

Private Sub CallAutoIt()

Dim x As Long

Dim blnWait As Boolean

Dim lngResult As Long

'Call AutoIt script

lngResult = Shell("c:\AutoItScript.exe")

blnWait = True

strAutoItResult = ""

'Keep checking strAutoItResult until it changes or we timeout.

'AutoIt will call a macro that changes strAutoItResult.

Do While blnWait

If strAutoItResult <> "" Then

blnWait = False

Else

x = x + 1

Pause

If x = 60 Then

'Timeout after 60 seconds

blnWait = False

MsgBox "AutoIt script never finished."

End If

End If

Loop

End Sub

'----------------------------------------------

Public Sub OjinSuccess()

strAutoItResult = "Succeeded"

End Sub

'----------------------------------------------

Public Sub OjinFailure()

strAutoItResult = "Failed"

End Sub

'----------------------------------------------

Private Sub Pause(Optional ByVal intDelay As Integer = 1)

'Do nothing for a default of one second or for how many seconds passed in.

Dim t As Long

t = Timer

Do While Timer < intDelay + t

DoEvents

Loop

End Sub

Here is the companion AutoIt code:

$oWord = ObjGet("","Word.Application")
If @error = 1 or $oWord = 0 Then
    MsgBox(0,"","Failed to access Word")
Else
    ;After performing desired code, I set $blnSuccess to indicate success or failure.
    ;Then I call the appropriate macro in Word.
    If $blnSuccess Then
        $sMacro = "OjinSuccess"
    Else
        $sMacro = "OjinFailure"
    EndIf
    $oWord.Application.Run($sMacro)
EndIf

Is it simply impossible to do this? If so, I'll have to resort to clunky workarounds, such as having AutoIt save desired data in a file and have Word read it. Big_daddy might be the most likely one to know a solution.

Share this post


Link to post
Share on other sites



You can't access VBA variables directly from AutoIt. Fortunately, yours is a common problem and there is a suitable solution:

http://www.mvps.org/access/api/api0004.htm

-S


(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]

Share this post


Link to post
Share on other sites

Locodarwin, you rock! Unfortunately, it appears I can't have my cake and eat it too. That code is great if I simply need to wait for a process to finish. But if I need the process to return a value to my code as well, I get a stalemate. Here's what I did in VBA for Word (along with including the code from the link you gave).

CODE
Private strAutoItReturnVal As String

'--------------------------------------

Public Sub Main()

strAutoItReturnVal = ""

ShellWait "C:\Documents and Settings\User\My Documents\Programming\AutoItTest.exe"

MsgBox strAutoItReturnVal

End Sub

'--------------------------------------

Public Sub AutoItDone(strVal As String)

strAutoItReturnVal = strVal

End Sub

Then in AutoIt, I used your beautiful solution from your answer to my other post.

$oWord = ObjGet("","Word.Application")
If @error = 1 or $oWord = 0 Then
    MsgBox(0,"","Failed to access Word")
Else
    $sMacro = "Interact.AutoItDone"
    $oWord.Application.Run($sMacro, "Success")
EndIf

What happens is that when AutoIt tries connecting with Word, it gets stuck forever because Word is waiting for AutoIt to finish! That's OK, because I can use my prior code since I can pass values to Word from AutoIt. Thanks so much for your help (although if you can figure out a solution to this, you will totally rock!).

Share this post


Link to post
Share on other sites

Locodarwin, you rock! Unfortunately, it appears I can't have my cake and eat it too. That code is great if I simply need to wait for a process to finish. But if I need the process to return a value to my code as well, I get a stalemate. Here's what I did in VBA for Word (along with including the code from the link you gave).

CODE
Private strAutoItReturnVal As String

'--------------------------------------

Public Sub Main()

strAutoItReturnVal = ""

ShellWait "C:\Documents and Settings\User\My Documents\Programming\AutoItTest.exe"

MsgBox strAutoItReturnVal

End Sub

'--------------------------------------

Public Sub AutoItDone(strVal As String)

strAutoItReturnVal = strVal

End Sub

Then in AutoIt, I used your beautiful solution from your answer to my other post.

$oWord = ObjGet("","Word.Application")
If @error = 1 or $oWord = 0 Then
    MsgBox(0,"","Failed to access Word")
Else
    $sMacro = "Interact.AutoItDone"
    $oWord.Application.Run($sMacro, "Success")
EndIf

What happens is that when AutoIt tries connecting with Word, it gets stuck forever because Word is waiting for AutoIt to finish! That's OK, because I can use my prior code since I can pass values to Word from AutoIt. Thanks so much for your help (although if you can figure out a solution to this, you will totally rock!).

the easiest way to coordinate would be with an external file. the existance of which can easily be checked for by either language, and the contents can be read in vba or autoit. It is an easy way to control timing, and pass values

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

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  
Followers 0