PDA

View Full Version : VBA Event to stop the server on a Word page, process and resume



amitplease
10-13-2013, 10:28 PM
Hi Folks,

The situation is -
When we start the server, bmp images and text is downloaded dynamically(run time) to a folder, and are pasted to microsoft word. This is a continuous proces, which means the bmps and text gets downloaded from another server and is continuously pasted in the Word. This goes on till the time the size of the document reaches above 512 MB. By default Word can handle 512 MB, anything above it closes with an error.
Now the idea is to convert the bmp into jpeg(I have the code for this) and after any of the page pastes the bmp, the execution need to stop(I need help here.. what event should i fire to stop it on a page) and my code needs to convert the inline bmp into jpeg(i have the code for this) and then the processing needs to resume.

According to the MSDN help - VBA for word has 27 events - I tried using "DocumentBeforeClose Event" but the problem is before this event is called Word reaches above 512 MB and crashes on it own. Then I thought of "WindowActivate Event" to stop the execution at an open window(page) and then my code converts the inline bmp image to Jpeg. But this did'nt help
The server side is inaccessible, hence cannot readily modify the server side code.
appreciate any inputs !!

macropod
10-14-2013, 03:34 AM
I doubt you would be able to have an event handler intercept anything your server is doing. Even if that were possible, the only event you're likely be able to use to any effect is WindowSelectionChange - and then only if the code your server uses is poorly written.

amitplease
10-14-2013, 05:05 AM
I doubt you would be able to have an event handler intercept anything your server is doing. Even if that were possible, the only event you're likely be able to use to any effect is WindowSelectionChange - and then only if the code your server uses is poorly written.

Thanks for the reply. I did try the WindowSelectionChange event, but it does not suffice my requirements.
Moreover the server side code is inaccessible to us and the only way I was thinking of halting the process, was to fire an event. Any other idea / approach to solve this , would be highly appreciated.

gmaxey
10-14-2013, 06:24 AM
This is just a stab, but if you put the image in Picture Content controls then you might be able to use the ContentControl_OnExit event.

amitplease
10-14-2013, 03:45 PM
Thanks Greg for the reply. Document_ContentControlOnExit is a useful function, but I cannot put the image in Picture content controls as I donot have access to the server side code. I can at most inject some external code to convert the bmp picture to jpeg. But for that I need to stop the server on a page which has bmp image, then inject my code to convert it to jpeg and resume the process. Alas, this control event will not work in my case.

macropod
10-14-2013, 04:07 PM
I donot have access to the server side code. I can at most inject some external code to convert the bmp picture to jpeg. But for that I need to stop the server on a page which has bmp image, then inject my code to convert it to jpeg and resume the process.
Without wanting to place too fine a point on it, you would do far better to convert the bmp files to jpg, at an appropriate scale, resolution and compression, before having your server app attempt to insert them into the document. FWIW, Word doesn't care whether a jpg file has a bmp extension, so you could do the conversion and replace your bmp files with jpg files (with the bmp extension) and never even have to change the server code.

Ultimately, though, you're still going to encounter the same issue - at most the above will delay the point at which Word falls over.

amitplease
10-14-2013, 04:44 PM
Thanks Paul, That would definitely have been a good solution, but the issue is my server picks up these BMP's from a file server which again I do not have access to, so there is no way I can modify the BMP's to JPEG.

fumei
10-14-2013, 11:41 PM
This is a continuous proces, which means the bmps and text gets downloaded from another server and is continuously pasted in the Word.

This seems very very strange to me. Can you explain why this is done? But if the situation is as you describe (the server just blindly, stupidly keeps cramming images into Word until it chokes), and you have no access to the server code, no access to the images it is shoving into Word; you may be SOL.

amitplease
10-15-2013, 05:32 AM
Well, the server intelligently picks up the different BMP's and other texts from the file server and place it in the MS word in a predefined , correct manner. Yes , I neither have access to the server side code, nor do I have access to the images and that's the precise reason why I wanted to halt the process on a Word Page, then I inject the code to convert that BMP which is in that page to JPEG and the process resumes. It goes to the other page and if it is again pasting a new BMP, I would like to halt the process and again convert the image and go on.

fumei
10-15-2013, 01:40 PM
This may not be possible. "if it is again pasting a new BMP" - you are asking for some sort of knowledge of what it is going to do, before it does it.

macropod
10-15-2013, 10:24 PM
Cross-posted at: http://social.msdn.microsoft.com/Forums/office/en-US/8a69039b-5341-4876-a75d-bcf2f7f3ca27/vba-event-to-stop-the-execution-on-a-word-page-process-and-resume?forum=worddev
where there is a parallel multi-post discussion.
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

fumei
10-16-2013, 03:13 PM
I think you can probably drop this as there is simply is no way to do what you want, with the restrictions stated. There is no magic available for you. There is no way to interrupt the server side code from Word.

amitplease
10-16-2013, 03:27 PM
I do have the code to convert all the inline images to inline JPEG images, and I can fire this code by using DocumentBefore Save Event or DocumentBefore Close Event . But the issue is before the document closes , it has already reached the maximum size(512 mb) and it crashes, before I can fire any of these events

amitplease
10-16-2013, 03:30 PM
I think you can probably drop this as there is simply is no way to do what you want, with the restrictions stated. There is no magic available for you. There is no way to interrupt the server side code from Word.
Yes I feel that, but I was trying my best If I could do something to get a solution. But Thanks anyways.

fumei
10-16-2013, 05:06 PM
Oh hey, it was sure worth a shot. But there is simply no way without access to the server code.

Frosty
11-01-2013, 02:26 PM
Well, here's a bit of a brain teaser... but you *could* (theoretically, and I don't really want to try and write the code since I don't have an easy way of replicating the process in order to test) which would utilize the built-in document open event to
1. Start an asynchronous process using Application.OnTime
2. Have that process start a timer, where it checks the document every X number of seconds
3. And runs your code periodically.

Not sure what would happen in a multi-access environment like you're describing, but conceptually you can create your own "monitor" and on a set interval run a process. You can't halt the server processing, but you could potentially do your own processing. You just have to make sure your code isn't going to try to convert an area it's already converted (so whether you put a bookmark in the document at the spot at which you last processed too, or just continuously iterate the .shapes collection and only deal with certain formats - no idea).

But it seems like it might be theoretically possible. And terribly problematic. 512 mb word files is crazy.

sts023
11-11-2013, 08:04 AM
Hi amitplease....
Paul (macropod) referred me to this issue for interest.
Just a thought - colud you use a timer event within the receiving document to check its size (using FileLen), and if it hits a predetermined limit, copy it to a new document with an incremental numeric suffix, then delete the current content and repeat the sequence until the transmission stops?
On the bad side, that would possibly take a fair while to process - I suppose it would depend on what buffering is allocated to the process....

macropod
11-11-2013, 02:23 PM
colud you use a timer event within the receiving document to check its size (using FileLen), and if it hits a predetermined limit, copy it to a new document with an incremental numeric suffix, then delete the current content and repeat the sequence until the transmission stops?
Simple answer: no. That's because any code in your document won't start running until the server side process releases the document. In any event, FileLen will return 0 until the file has been saved, which there is no evidence of from anything posted so far.

amitplease
11-11-2013, 03:33 PM
As suggested by Frosty, I created an asynchronous process, started a timer and after say 1 min interval my code runs and converts all the inline BMP images in the document to JPEG.
This is working fine in a standalone process (by which I mean there is no server side process running in parallel).
You are very right Paul, When I deploy it in the actual environment, i.e the server side process starts - my code does not run, because the document is being locked by the server until it releases the lock(by which anyway, the document gets over sized and crashes).
This is an extract of my program which works fine in standalone process -


Const interval As Double = 1 ' minutes
Dim selStart As Long
Dim selEnd As Long


Sub AutoOpen()
MsgBox "Document open"
selStart = 0
selEnd = 0
MsgBox "Calling the Alarm - set for 1 min"
Application.OnTime When:=DateAdd("n", interval, Now), Name:="CheckStatus", Tolerance:=120
End Sub


Sub CheckStatus()
Dim shp As InlineShape
For Each shp In ActiveDocument.InlineShapes
MsgBox "Converting to JPEG"
shp.Range.Copy
shp.Range.PasteSpecial Link:=False, DataType:=15, Placement:=wdFloatOverText, _
DisplayAsIcon:=False
shp.Delete
Next
End Sub

I was thinking if I can still achieve this process by using DocumentBeforePrint() from the client side, might be the server side process relinquishes the lock and runs my code.

fumei
11-11-2013, 08:00 PM
I am not sure why this is still going on. It has been stated right from the get go that without access to the server side execution, there is nothing you can do with Word VBA to change anything.