Link to home
Start Free TrialLog in
Avatar of ellsworth2000
ellsworth2000

asked on

What is the code for making a macro open a template as Document 1 but only when document 1 has not been modified?

Good Day Experts,

I have placed a button/macro in our Normal.dot template that will open another shared template that is for a letterhead.  When the letterhead button/macro is clicked, a whole new Word Document is opened(document 2).  I'd like that to happen ONLY when Document 1 has already been modified, if document 1 has not been modified then I'd like document 1 to be the letter head template that is opened.  this is what is used for the letterhead button/macro:

Sub Letterhead()
    Application.Documents.Add "s:\word templates\ltrhead.dot"
End Sub

Thanks,

Ellsworth
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Best to check if it's there as well:

Sub Letterhead()
    Dim doc As Document
    For Each doc In Application.Documents
        If doc.Name = "Document1" Then
            If doc.Saved Then
                Exit Sub
            End If
        End If
    Next doc
    Application.Documents.Add "s:\word templates\ltrhead.dot"
End Sub
Avatar of ellsworth2000
ellsworth2000

ASKER

Hi GrahamSkan,

I pasted that code into my Normal.dot file and it  disabled my letterhead button.  I click the button and nothing happens.  Do you have any Ideas What is wrong?

Thanks,

Ellsworth
GrahamSkan,

You don't know of a way to make the name of the macro have a space between Letter and Head do you?

Thanks,

Ells
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh I don't think we need to have spaces in the name of the macro though, it would be more pleasing to the eye if there were spaces between words when the cursor is hovering over the macro button and the Name is displayed.

Lol...(I hope you don't hate me for being picky)The recent code changes opens the Letterhead template in a single document but it changes to Document 2.  Is it possible to make the code open the letterhead template in document 1 but still have only one document open if no changes have been made to document 1?

Many Thanks GrahamSkan,

Ells
You can use an under score.
What sort of macrobutton? I just tried hovering over a MacroButton field and nothing popped up. With other button types, you can usually control the tooltip text independently.

I did say that it was difficult to retain the Document1 name. Perhaps I should have said that there are several methods, none entirely satisfactory. That's the reason I asked why you wanted to do it

The only way to change a document name in Word is to do a Save As. Obviously a second occurence of the operation would find a pre-existing file of that name. Should that be overwritten? Also the name DocumentN is a good visible indication to the user that  document has never been saved.

This code involves attaching the template, then opening it as a document, copying the text from it to the blank document, then closing the template.

Sub Letterhead()
    Dim Doc As Document
    Dim TemplateDoc As Document
    Const strTemplateName = "s:\word templates\ltrhead.dot"
    For Each Doc In Application.Documents
        If Doc.Name = "Document1" Then
            If Doc.Saved Then
                Doc.AttachedTemplate = strTemplateName
                Set TemplateDoc = Documents.Open(strTemplateName)
                TemplateDoc.Content.Copy
                Doc.Content.Paste
                TemplateDoc.Close False
                Exit Sub
            End If
        End If
    Next Doc
    Application.Documents.Add strTemplateName
End Sub


( I'm off to bed. It's past midnight here.)
Many Thanks to you GrahmSkan.  I think I'm going to use that second revision being that it works best.

Thanks again,

Ellsworth