PDA

View Full Version : Problem keeping control of a document created in a macro.



cjmitton
03-01-2014, 03:05 AM
Please forgive me if I've used the wrong terminology here! I've never had any proper training in VBA and everything I know is self taught via google / help files and help people in forums (generally this one!)

I'm writing a document creation system, but I have one very annoying issue at current that I can't seem to get my head around.

From a custom form I've created with in Word 2010 (32bit) my form has a series of list boxes / combo boxes / check boxes that depending on the selections will generate a letter.

When I create my base letter for all the rest of the information to be inserted in too I do the following code:

If chkbxSendEmail = True Then
Set StdCtrlDoc = Documents.Add _
(Template:="c:\dfa\Templates\Word\" & Department & "\Blank " & Department & ".dotx", _
NewTemplate:=False, DocumentType:=0)
StdDoc.Bookmarks("Date").Range.InsertDateTime DateTimeFormat:="dd MMMM yyyy", InsertAsField:= _
False, DateLanguage:=wdEnglishUK, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
ActiveWindow.EnvelopeVisible = True
Else
' open standard letter template then insert date
Set StdCtrlDoc = Documents.Add _
(Template:="c:\dfa\Templates\Word\" & Department & "\LH " & Department & ".dotx", _
NewTemplate:=False, DocumentType:=0)
StdCtrlDoc.Bookmarks("Date").Range.InsertDateTime DateTimeFormat:="dd MMMM yyyy", InsertAsField:= _
False, DateLanguage:=wdEnglishUK, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
End If


I have Declared the variable StdCtrlDoc as a document at the very top of the forms code so it available across all the sub routines in the form code.

The form then goes to various macro's with in the form to do stuff like get a address from a DB and insert it in to a particular book mark on the template. add various endings to the letter or insert images.

At the end I call the originally created document 'StdCtrlDoc' to bring it to the front then clear it ready for the next time I run the macro


StdCtrlDoc.Activate
Set StdCtrlDoc = Nothing


When I have the template open and I'm testing the code it runs fine, the document created comes to the front as the active document, but when I add this template to the startup folder in 'C:\program files\Microsoft office\office14\startup' and run the form from a ribbon bar I've set up. (just like I do when I'm testing with the template open) the document I create with my form stays to the back and the document where I clicked from the ribbon on come to the front instead?

I'm confused and have got through the different macro's used with-in the form to ensure I've not used an 'activedocument' type statement or used the incorrect name so I lose 'control' but can't find anything. the documents no matter what combination I use all work correctly and generate the letters I require on to the correct letter (nothing appears on any other documents open).

I'm guessing I've missed something straight forward but I'm now running in circles! any thoughts please.

mrojas
03-01-2014, 04:00 PM
You may want to try keeping track of each opened object by using the ActiveDocument.ActiveWindow.Index collection.
I do something similar in one of my projects where I need to bring to the front the right document, and I do this by storing the document's index number before I make another one the active document.
To return to proper document, use Windows(intWindowIndex).Activate

cjmitton
03-02-2014, 03:55 AM
mrojas...

Thank you very much, it works a treat.

gmaxey
03-02-2014, 07:48 AM
I'd still be interested in why CJ is experiencing the issue. Here at least using the following code oDoc2 is consistently the activedocument after the code is run.



Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc1 As Word.Document, oDoc2 As Word.Document, oDoc3 As Word.Document
Set oDoc1 = Documents.Add
oDoc1.Range.Text = "AAA"
Set oDoc2 = Documents.Add
oDoc2.Range.Text = "BBB"
Set oDoc3 = Documents.Add
oDoc3.Range.Text = "CCC"
oDoc2.Activate
End Sub

cjmitton
03-02-2014, 10:02 AM
I did find another issue, when I ran my original 'stdctrldoc.active' it was followed by the form I used to generate the document being unloaded, this then bought back the original document back to the front. no matter which option I used (I went back and tested both) this happened.

I adjusted the order so the form closed and the document is then called to the front, but still used the 'index' to keep note of the document as I could then run another form / macro to open up some other files (which ran from a different form) but I could always call back the correct document.

So my original way would have worked if not for the unloading of the form. but with index I've been able to improve a couple of areas and make it a bit more user friendly / quicker!

gmaxey
03-02-2014, 10:20 AM
CJ,

Thanks for the update.