Consulting

Results 1 to 8 of 8

Thread: Solved: Maintaining macro references for multiple instances

  1. #1
    Knowledge Base Approver
    Space Cadet
    VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location

    Solved: Maintaining macro references for multiple instances

    Hi all. I have suddenly ventured into the complicated side of macro writing and I've come undone.

    Here's the story.

    we have a back office system that does the following. It allows you to create headers (templates) and regular documents (.doc's). You can attach a header to a document or not. But bascially you take these two documents with merge fields that insert database data.

    The process is:
    1. Create new document from header
    2. Insert other document (Insert->File)
    3. Mail Merge this combination into a new document
    4. Run macros from a global template on the new document to do word processing requirements.

    These macros are basically;
    1. Create toolbar to access macros for document.(thanks again to everyone at VBAX who showed me how)
    2. Automatically run the starting macro after toolbar has been created to complete the reqired part of the document. (inserting address and other details)
    3. Run a print macro when the toolbar button is clicked (each document is printed differently hence the need)

    My problems are this:
    1. I can work on multiple different documents using te same global template but only after each document has run through its specific macro Eg a letter, a memo and a fax cover sheet. Each ahs there own macro to run post merge. They all work fine but each macro has to be completed before the next doc can be created. Is there a way around this? or is it that each macro has to finish and unload its references to the global template before the next one runs?

    2. It seems that everyother document post merge sequence does not load the global template and its macros. How do I ensure that the document has these references regardless of the number of times the global template is invoked?

    3. A side issue I have tried to deal with is getting focus to the document post merge sequence. sometimes it happens sometimes it doesn't. this is what I have tried for this
    [vba]
    Sub NewBlankDocument()
    CreateToolbar ("New Blank Document") 'This creates a toolbar specific to the doc
    ActiveDocument.ActiveWindow.WindowState = wdWindowStateMaximize
    ActiveDocument.ActiveWindow.SetFocus
    StartBlankDocMacro 'this runs the macro associated with the document
    End Sub
    [/vba]

    apologies - as usual i forget to put in what matters even more than the problem. My entire firm operates on Office 2003 SP1 and windows xp prof SP1

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Hi there,
    I'm not entirely clear on how your code is structured but I think I get the idea, so here's some answers:
    1. With VBA, a routine has to complete before you can do anything else in the app and there isn't really a way around this
    2. If you have a global template that you want available all the time in Word, you should place it in the Word startup folder (as defined in Tools|Options|File Locations|Startup). That is where you would have your toolbar for word processing requirements and invoking the new files from individual templates that have their own specific code
    3. I think the only reason this code would not appear to fire is if the ActiveDocument isn't the one you're expecting it to be. When you create a new document, you should set a reference to it, then use that reference to control it:
    [VBA]Dim myNewDoc As Document
    Set myNewDoc = Application.Documents.Add
    With myNewDoc
    .Activate
    .ActiveWindow.WindowState = wdWindowStateMaximize
    End With[/VBA]

    Hope this helps...
    K :-)

  3. #3
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location

    That does help

    Thanks killian, that does help. You've definitely given me a little insight I didn't have before.

    1. That explains alot.
    2. The template only needs to be avaible when they are working through the vendors system (its AIM Evolution with a Sculptor front end ). That and trying to maintain it over the network - gives me a headache just thinking about it lol.
    3. Trying it - will let you know if it works.
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  4. #4
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location

    2 out of 3 so far

    Just an update. The first two problems are no longer too important, however - getting the focus on the word document is a problem and I can't seem to solve it. This is what I tried with the code you showed me. I didn't need to create a document - the front end program does that for me. So I use active document. Probably don't even need to do that but I really need to have Word become the focus object so I'm trying anything. Is there maybe a windows API call I could do? BTW the front end program (Sculptor) is written in Visual Basic (6 i think) - could this possibly be contributing to my problem?

    Thanks again
    Andrew;?

    [vba]
    Private Sub BringDocForward()
    Dim myNewDoc As Document
    Set myNewDoc = ActiveDocument
    With myNewDoc
    .Activate
    .ActiveWindow.WindowState = wdWindowStateMaximize
    .ActiveWindow.SetFocus
    End With
    WordBasic.ViewChanges 'for some reason it keeps putting a comment in <- this takes it away
    End Sub
    [/vba]

    something more to add to perhaps mae things clearer is that I basically want to make Word the dominant application - essentially bring its focus to the fore - is there a way to do this in VBA? I've heard of an html code that does this for web pages (those annoying popup add ring a bell?). Anyway any more help/information would be greatly appreciated.
    Last edited by sandam; 03-03-2005 at 08:48 AM. Reason: reread previous post
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  5. #5
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    OK, well I think this may be a problem then...
    You can use an API call to get a handle to the Word App window but if the word app is being called from Sculptor it won't make any difference because once your Word code has finished running (even if it ends activating itself) the Scupltor code that calls it will then continue, regaining the focus in Windows
    It appears that's what's happening from here anyway...
    K :-)

  6. #6
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    Okay, cool. Now my next question Whats the API call. And how exactly would I work that into the code? I figure nothing ventured, nothing gained.

    Thanks again
    Andrew

    Thought I might add that its not totally neccessary to hold focus on word after the macro has completed. The user just needs to complete the doc and once its finished they return to sculptor and begin working on another matter - essentially another document will be started but again through sculptor.
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  7. #7
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location

    API calls

    You'll need to declare the API functions you want to use at the top of the module with a Long variable to use as the handle reference
    [VBA]Declare Function GetActiveWindow Lib "user32" () As Long
    Declare Function SetActiveWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
    Dim h As Long[/VBA]
    Then use GetActiveWindow to return a long which will be the Windows handle
    [VBA]h = GetActiveWindow[/VBA]
    and SetActiveWindow when you want to get it back to the front
    [VBA]SetActiveWindow (h)[/VBA]
    I think you'll get the same result though...
    K :-)

  8. #8
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    thanks again for your help killian. You were right and it still made no difference, except that word is now the application underneath the sculptor form so the user can the just click next to the user and word comes into focus. I'll just have to sell it to the users that way when I finish the project off and start training the firm to use it.
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •