PDA

View Full Version : Add Template to Active Window if document in Active Window is blank.



bstephens
11-30-2009, 02:42 PM
Help with if-then statements.

The code as pasted below always load an instance of the template in a new window.

Does anyone know how I modify the following code to call a template into the "Active Window" if the document already in the "Active Window" is blank (i.e. there is nothing in it), and if it is not blank, than load it into a new window.


Sorry for my horrible English.

Option Explicit
Sub SKLetter()
'
' SKLetter Macro
'
Documents.Add Template:= _
Environ("APPDATA") & "\Microsoft\Templates\SKLetter.dot"

End Sub

fumei
11-30-2009, 02:58 PM
That codes creates a new document with the attached template being SKLetter.dot.

It has absolutely nothing to do with any existing ActiveDocument. There is NO connection.

bstephens
11-30-2009, 03:49 PM
I'm probably using bad terminology.

Currently, when I launch my macro from the button I have assigned it on the ribbon, there will always be the active open document (Document1) and than after the button is pressed, there will be another instance of word (Document2) with the template loaded.

Is there a way to change the macro so that if there is nothing in (Document1) than the template will simply load in the same window that Document1 is in and there will only be one instance of word in the windows task bar?

lucas
11-30-2009, 04:16 PM
The obvious thing would be to just open the template but there must be some reason you are not doing that, right?

TonyJollans
12-01-2009, 02:16 AM
You cannot make this happen automatically with any setting. You must code the checks yourself. Depending on what you want, you might try:

If ActiveDocument.Saved Then ActiveDocument.Close
Documents.Add Template:= _
Environ("APPDATA") & "\Microsoft\Templates\SKLetter.dot"

or, maybe, ..

If ActiveDocument.Content = "" Then ActiveDocument.Close
Documents.Add Template:= _
Environ("APPDATA") & "\Microsoft\Templates\SKLetter.dot"

or, maybe, ...

fumei
12-01-2009, 12:25 PM
You need to define "if there is nothing in (Document1)".

Either of Tony's suggestions will work, but as he mentions, "Depending on what you want"

His first suggestions checks if the current active document is saved, and if it is, close it and create the new document based on SKLetter.dot.

His second suggestions checks to see if there is any text content (ActiveDocument.Content = ""), and if there is NOT, close it and create the new document based on SKLetter.dot. However - sorry Tony - this will always fail. A "blank" document - freshly created with "nothing" in it - never has ActiveDocument.Content = "".

Try it. Create a new document with "nothing" in, and execute:
Sub TestContent()
If ActiveDocument.Content = "" Then
MsgBox "Empty"
End If
End Sub
It will never display the message. ALL documents have at least one paragraph mark. Therefore Content is never "".

If you execute:
Sub TestContent()
If ActiveDocument.Content = "" Then
MsgBox "Empty"
Else
MsgBox ActiveDocument.Content
End If
End Sub
You get a message box (from the Else part) which looks like there is nothing there. However, if you execute:
Sub TestContent()
If ActiveDocument.Content = "" Then
MsgBox "Empty"
Else
MsgBox Asc(ActiveDocument.Content)
End If
End Sub
You get a message box with "13" - the ASCII code for...a paragraph mark.

Therefore, if you use Tony's second method, it should be:
If ActiveDocument.Content = Chr(13) Then
ActiveDocument.Close
End If
Documents.Add Template:= _
Environ("APPDATA") & "\Microsoft\Templates\SKLetter.dot"

TonyJollans
12-01-2009, 12:53 PM
Of course you are right, Gerry :o: - I should think before I post

I did think of checking if anything was undo-able - no check is perfect but that struck me as indicating as well as anything whether there had been activity on the document - but it really does depend what bstephens wants, and in what circumstances.

fumei
12-01-2009, 02:00 PM
"but it really does depend what bstephens wants, and in what circumstances."

Indeed. Or, as I put it: You need to define "if there is nothing in (Document1)".

The question about any existing Undo, or any other changes may - or may not - be an issue. Who knows? Would it be important if something had been put in Document 1, then deleted?

Besides, if the current ActiveDocument is "empty" or "nothing" why bother closing it? Why not just simply attach the desired template?
If ActiveDocument.Content = Chr(13) Then
ActiveDocument.AttachedTemplate =
Environ("APPDATA") & "\Microsoft\Templates\SKLetter.dot"
End If
This does exactly what seems to be asked for.

"if there is nothing in (Document1) than the template will simply load in the same window that Document1 is in and there will only be one instance of word in the windows task bar?"

The issue of "only be one instance of word in the windows task bar" is not (it seems to me) all that relevant. Whether there is one, or many, "instances" of Word in the taskbar is a function of the Taskbar. Don't like individual icons for multiple documents on the taskbar? Then change "Group similar taskbar buttons" in the Taskbar Properties.

lucas
12-01-2009, 02:17 PM
I'm missing something basic on this. If there is nothing in the document why are we using it to call the template? Why not just use the template?

fumei
12-01-2009, 03:11 PM
Indeed. However, the OP seems to want a test to see if the current ActiveDocument is "empty" first, I believe in order to NOT have an "empty" document hanging around. But again, the issue of multiple icons on the taskbar is in my mind not relevant.

"If there is nothing in the document why are we using it to call the template?"

I am not sure "we" are using it - in terms of the code to do anything is in THAT document. Where this code actually resides...I do not know. Considering the normal state of things, probably in Normal.dot.

In any case, for the moment, the answer to the posed question is here. Test the active document, and either attach the desired template to it, OR close it and clone a new document from the template.