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.
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.