Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-11-07
7
Medium Priority
?
184 Views
Last Modified: 2010-04-30
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
0
Comment
Question by:ellsworth2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17894721
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
0
 

Author Comment

by:ellsworth2000
ID: 17902503
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
0
 

Author Comment

by:ellsworth2000
ID: 17902525
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 17902650
It will certainly run differently form a previous macro of that name. It checks to see if there is a document called Document1. If there is, and its .Saved property is true (that is, there are no changes to save) then nothing will happen.

However, I think I misunderstood your requirement. You require Document1 to be a document that would be as if it were created by file/new. That is surprisingly difficult. Why don't you just close the Document1, and create a new document anyway?

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

A macro name cannot contain spaces. Why do you think that you need it to?

0
 

Author Comment

by:ellsworth2000
ID: 17902814
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
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17903005
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.)
0
 

Author Comment

by:ellsworth2000
ID: 17907572
Many Thanks to you GrahmSkan.  I think I'm going to use that second revision being that it works best.

Thanks again,

Ellsworth
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question