Two documents open when I open one...

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Two documents open when I open one...

Post by ErikJan »

Cross-posting in this new topic.

If I open een existing document, that document opens OK. But also a second empty document opens.
I don't want that: if I open an existing document, all I want to see open is that document and nothing else.

How could that happen?

User avatar
Charles Kenyon
5StarLounger
Posts: 626
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Two documents open when I open one...

Post by Charles Kenyon »

In Word for Windows, I expect.

What is the second document?
Is it an unnamed blank document?

How do you open an existing document?
Does it happen if Word is already open?

I almost always have Word open so don't have this problem. Generally when I start Word, I do it with a pinned shortcut in the TaskBar rather than by double-clicking on documents. I then open them from within Word. I always then, have a blank document open until I close it.

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Two documents open when I open one...

Post by ErikJan »

OK, again (with apologies) I need to fine-tune and clarify my post.

I use Word (MSO365) in Windows 11 (Pro). My Windows OS is English. Windows and MSO are up-to-date (always).

In the default setup (so without files in the startup folder and only Normal.dotm active):
(1) if I click the Word icon I get one empty document called "Document1". That seems OK.
(2) if I double click an existing docx or docm file, the file opens (and nothing else). That seems OK as well.

I had problems that all my new Word documents used the OS language (=English) as proofing language, no matter what I saved into Normal.dotm (see a recent other thread). I want Dutch as my proofing default language in Word (and in other MSO apps, but that's for later maybe).
For Word, I adopted a proposed solution by Charles and copied a dotm file into the Word "Startup" folder. This file runs an AutoExec VBA macro and makes sure the proofing language is correct (Dutch).

If I do that and:

(3) if I click the Word icon, I get one empty document called "Document2", which is a document created from the template in the Word Startup folder. That allows me to have Dutch as my default proofing language. That seems OK.
(4) if I double click an existing docx or docm file, the file opens but I also get the same “Document2” file opened as I got in #3 above. I don’t want that.
(4a) if after #4, I open another existing document, only that document opens (and no further empty documents are opened) – I understand that
(4b) if after #4, I choose “File” and the click “Blank document”, I get a blank document (without the Dutch proofing as default) – I understand that
(4c) if after #4, I click “ctrl-N” to open a new document, I get a document where Dutch is the default proofing language – I understand that (as VBA code in the template is called)

So the question is about #4 (I thought I had seen earlier that this also happened in my case #1 but it doesn’t).

I know, this isn’t a big deal but I'm puzzled.
My guess is that maybe the code in the template in the Startup folder can be changed to prevent this. The code that runs (twice, for reasons I don't understand) there is AutoExec.
I added some debug lines in the Template VBA code to log when the AutoExec code is called and what is happening.
If I first check the #3 case above (click the Word icon), I see this (I slightly edited to remove user info):
AutoExec: 15:59:26 ( 57566.59 )
Before adding the template (C:\Users\<...>\AppData\Roaming\Microsoft\Word\STARTUP\DutchNormal - Debug.dotm)
End of AutoExec (current ActiveDocument: Document1)
AutoExec: 15:59:26 ( 57566.68 )
Document 'Document1' was just closed in the background
Before adding the template (C:\Users\<…>\AppData\Roaming\Microsoft\Word\STARTUP\DutchNormal - Debug.dotm)
End of AutoExec (current ActiveDocument: Document2)
Looks like the first time, there’s no document to close, but it still loads the template and ends with “Documen1”
The second time, it closes “Document1”, calls the template again, and ends (logically) now with “Document2”

So I changed the code to end if the name of the active document is “Document1”. That worked:
AutoExec: 16:38:03 ( 59883.46 )
Before adding the template (C:\Users\<…>\AppData\Roaming\Microsoft\Word\STARTUP\DutchNormal - Debug.dotm)
End of AutoExec (current ActiveDocument: Document1)
AutoExec: 16:38:03 ( 59883.56 )
Document1 is loaded, End AutoExec
Of course, now I needed to test this with scenario #4 where I open an existing document (that's why I did all of this in the first place).
Weirdly enough, I get exactly the same debug output as in the above case #3 (I had added an AutoOpen in the template but that never fired).
Also and regretfully, still two windows open…
It seems that everything that involves an existing document all happens AFTER the whole AutoExec cycle is complete.
That’s bad as I wanted to prevent the template from being loaded (or alternatively close it) if the document that is active when control is returned to the end-user is not called “Document1”.

User avatar
Charles Kenyon
5StarLounger
Posts: 626
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Two documents open when I open one...

Post by Charles Kenyon »

I appreciate it that you would like everything to be perfect.

Use File>Open from within Word.
Or leave Word open in the background. (That is what I do, normally. It is rare that I have my computer running without Word and Outlook running.

When you do not have Word open already, you will run the AutoExec to get a Dutch-language blank document.
I'm afraid I do not think it is worth the effort to try to work around this as well.

You could hold the Ctrl key down when you double-click on it. When you release it, you will be asked if you want to start Word in safe mode. Say yes and you will not get the blank document with the Dutch proofing language.

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Two documents open when I open one...

Post by ErikJan »

Thanks Charles, I understand what you suggest. But I'm stubborn :grin:, so I want to see if I can solve this without changing my normal workflow.

The remaining question is now: How can I run VBA code after a document is opened (and I mean ANY document, I understand that I can do anything if I open a specific DOCM file. But I also want to run the code after a DOCX opens).
From there I think I can work myself towards something in VBA that will work for me.

User avatar
Charles Kenyon
5StarLounger
Posts: 626
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Two documents open when I open one...

Post by Charles Kenyon »

A macro named AutoOpen in your Normal.dotm will run any time a document is opened. It will not run when a new document is created.
A macro named AutoNew in your Normal.dotm will run any time a new document is created.
See Run a macro automatically when a document is created, opened or closed https://wordmvp.com/FAQs/MacrosVBA/DocumentEvents.htm

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Two documents open when I open one...

Post by ErikJan »

OK, I got it all to work. Thanks for all the suggestions. As promised, I'm sharing my code here. This is built based on the code Charles created.
Note that I have a version with debug information embedded which shows exactly which procedure is called, when and what happens (which can be enabled / disabled via a Boolean constant). The code below is are versions without that debug info (I hope I didn't mess up removing the extra debug lines...).
I realize that this might only work on English language Office versions as I test for new documents that have names like "Document1". Guess this can be adapted easily.

In summary:
  • If I open Word, one empty new document is shown with the proofing language in Dutch (following the method from Charles)
  • Typing "Ctrl-N" creates a new document, also with proofing language in Dutch
  • Using the mouse to go to File and the select the NEW template does NOT create a new document with the proofing language in Dutch (but that's a matter of placing the right template there or maybe I can add code to fix that as well later)
  • With Word closed, if i double click an existing Word-document, that simply opens as expected, no other blank documents remain in the background
In Normal.dotm:

Code: Select all

Sub AutoOpen()
    Application.ScreenUpdating = False
    '
    If Documents.Count = 2 Then
        If Documents(2).Name = "Document1" And Documents(2).Saved = True Then Documents(2).Close
    End If
End Sub
Next the AutoExec sub which is located in the "Different language template" dotm-file in the "STARTUP" folder:

Code: Select all

Sub AutoExec()
    'Charles Kenyon
    ' On Word start, creates a new document based on this template rather than on Normal.dotm
    '
    'Changes: EJvdH (April 2024)
    '
    Dim DocName As String
    On Error Resume Next
    DocName = ActiveDocument.Name
    If DocName <> "Document1" Then
        On Error GoTo NoOpenDoc
        ActiveDocument.Close SaveChanges:=False
NoOpenDoc:
        Documents.Add Template:=ThisDocument.FullName, DocumentType:=wdDocument, Visible:=True
        'Print View
        If ActiveWindow.View.SplitSpecial = wdPaneNone Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        Else
            ActiveWindow.View.Type = wdPrintView
        End If
        ActiveDocument.Saved = True
    End If
End Sub

User avatar
Charles Kenyon
5StarLounger
Posts: 626
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Two documents open when I open one...

Post by Charles Kenyon »

:clapping: Looks like some good coding!
Congratulations!

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Two documents open when I open one...

Post by ErikJan »

Update: I also added some code to Normal.dotm to make sure that when I use File and then select the New template, that new file is closed and instead the special template with the proofing language in Dutch is loaded.

My Word language problem is now fully solved.

I wonder if and how a similar approach could work for emails (I use the Outlook Desktop App - NOT the "New" one). In the Templates folder where the Normal.dotm file is found, I also see a NormalEmail.dotm. But where I find a StartUp folder for Word, I don't (yet) find one for Outlook.

(The additional problem is that even if I change the proofing language manually in an email, it seems impossible to change that for the email subject)

User avatar
Charles Kenyon
5StarLounger
Posts: 626
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Two documents open when I open one...

Post by Charles Kenyon »

You may want to post that question in an Outlook forum. I do nothing with coding Outlook. I just use it.

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Two documents open when I open one...

Post by ErikJan »

OK, I understand and it's logical, yes. However, I might leave that one alone for now: if I raise this elsewhere, I would have to start to explain how this is all set up based on your work. I was hoping that I could simply build this (minor?) extension on top of this thread somehow.
Maybe in a few months ;-)

snb
4StarLounger
Posts: 586
Joined: 14 Nov 2012, 16:06

Re: Two documents open when I open one...

Post by snb »

If you create a new document, type 6 words + Enter and have language detection on 'automatically' Word will check your text in the desired language.

User avatar
Charles Kenyon
5StarLounger
Posts: 626
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Two documents open when I open one...

Post by Charles Kenyon »

@snb
This should be posted in the previous thread about language to be most useful.
https://www.eileenslounge.com/viewtopic ... 26&t=40483

Generally, the detect language automatically seems to cause more problems than it solves.

snb
4StarLounger
Posts: 586
Joined: 14 Nov 2012, 16:06

Re: Two documents open when I open one...

Post by snb »

@CK

Don't think so: this cumbersome 'workaround' is only meant to accomplish this.
Removing the language option in the 'normal' style could be sufficient too.

User avatar
ErikJan
BronzeLounger
Posts: 1255
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Two documents open when I open one...

Post by ErikJan »

snb wrote:
03 May 2024, 13:17
If you create a new document, type 6 words + Enter and have language detection on 'automatically' Word will check your text in the desired language.
If that would have always worked for me, I wouldn't have gone through all the trouble finding this (solid and reliable) workaround... believe me.

User avatar
Charles Kenyon
5StarLounger
Posts: 626
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Two documents open when I open one...

Post by Charles Kenyon »

snb wrote:
03 May 2024, 15:55
@CK

Don't think so: this cumbersome 'workaround' is only meant to accomplish this.
Removing the language option in the 'normal' style could be sufficient too.
1) Please address that in the thread about the problem and the workaround rather than in one about problems arising from the workaround. You participated in that discussion and did not bring this idea up at that time. Putting it there will give future forum users who have that problem the best chance of finding the solution. Just sayin'. :cheers:
2) You cannot remove the language option in a style once it is there. You can change it, but not remove it.
See https://feedbackportal.microsoft.com/fe ... 0d3ae46fcb and https://answers.microsoft.com/en-us/mso ... 6a71ceec0f