PDA

View Full Version : Coordinating macro with external application



MTP
07-18-2013, 09:53 AM
My company uses a program that exports data into a Word template.

I would like to alter the appearance of the Word document with a macro. I wrote a macro in the template and called it AutoNew() so it runs when the external application creates a new file based on the template. I used OnTime to delay the running of the macro since it can't format something the external application hasn't put into the document yet.

This works OK, but somehow turns on ScreenUpdating (so the document takes longer to create), and if I put the delay with OnTime for very long, the external application throws an error that it can't export to Word. (The document gets header information and not the body of the report.) So I can't format anything at the end of the report, since I get an error by trying to wait until the report is done.

I have a couple of questions:
The external application turns off screen updating. Why would running my macro turn it back on? Even if I try to set it back to false within my macro, it doesn't work.

Is there any way besides OnTime to delay running of my macro until after the external application has put all its information into the document?

SamT
07-24-2013, 07:01 AM
I dunno, but try this

Private Sub AutoNew()
Do Events
'Rest of code here
End Sub

MTP
07-24-2013, 07:25 AM
That looks very promising. I had come across it before I posted, and tried putting the "Do Events" both at the beginning and sprinkled throughout my macro. Unfortunately, the reality was not as promising as the looks.

I suspect the issue is that Do Events lets the operating system do stuff that doesn't involve Word. Since the external application is doing things with Word, it's not helped at all by the Do Events statement. I also tried running a loop that checked for number of tables (the external application creates three tables) with a Do Events inside the loop and it locked up Word so the external application couldn't do anything.

SamT
07-29-2013, 10:58 AM
Does the external app save the document when it's done?

If so, use the DocumentBeforeSave Event to call your sub, then in your sub place code similar to the Helps' Timer Example to delay operation for long enough for the doc to be saved.

Otherwise we'll need to work with the last thing the app does that raises and event.

BTW, what is the other application?

MTP
07-29-2013, 11:17 AM
The application does not save the document. As far as I can tell, the last thing it does is insert text into a table.

The application is "Omnimet MHT Sample Viewer". I asked the manufacturer about helping with the customization I'm looking to do; and they replied that the Word form was not intended to be customized and if we wanted a customized report we should use a Microsoft Access report.

They may be right in general; however, since we've used this form for years and it's working for us now I'd rather work with what we have than switch to an entirely different format. It would be nice if I could keep the screen updating off and do a bit of formatting at the end of the report, but we're still happy even without that.

SamT
07-29-2013, 04:35 PM
How about just putting the code in Normal.dot or its newest replacement? It will then be available in any open document. You would still have to run it manually, but...