Consulting

Results 1 to 6 of 6

Thread: How to run a procedure in a Word Document as soon as it is newly created

  1. #1
    VBAX Regular
    Joined
    Mar 2009
    Location
    Stowmarket
    Posts
    62
    Location

    How to run a procedure in a Word Document as soon as it is newly created

    I have a VS2010 Vb.net program that creates a Word 2007 file.

    My Normal.dot file is customised to give me a new Tab with Buttons that do specific things via VBA in the Normal.dot program when those Buttons are pressed.

    This all works fine, however, I now want to add some functionality whereas as soon as the new Word document is created, it edits a Task in Outlook via the VBA code (I have this code ready to apply, this is not the problem, I need to make it execute, that is the problem !).

    I have edited the 2 "This Document" Procedures and you can see my Normal.Dot file in the attached Screenshot ... except you can't, why can't I load a screenshot from my PC ?!?!?

    OK, under Project (NL Skeleton) which is the Word Document I use to update my Normal.dot template (although I now realise that it doesn't really work like that !) I have a basic MSGBOX command saying ...

    [VBA]MsgBox ("NL Skeleton)"[/VBA]

    ... and under Project (Normal) I have a basic MSGBOX command saying ...

    [VBA]MsgBox ("Normal)"[/VBA]

    However, when I run my VB.Net program that creates a brand new Word 2007 document, the VBA code in the Normal.dot template does NOT stop on either of the message boxes, it just continues and opens the Word document exactly as before, my VB.net code is below, what am I doing wrong ?!?

    [VBA]
    'Open or Create Word document for Editing

    myNewsLetter = myFolder + myLeague + "News" + mySession + ".doc"

    If File.Exists(myNewsLetter) Then
    'do nothing
    Else
    myTemplate = myTempFolder + "NL Skeleton.doc"
    File.Copy(myTemplate, myNewsLetter)
    Create_Blank_Newsletter()
    End If

    'Open Word Newsletter, or switch to it if it's already open

    Dim myFileOpen As Boolean

    myFileOpen = IsFileOpen(myNewsLetter)

    If myFileOpen = False Then
    MSDoc = MSWord.Documents.Open(myNewsLetter)
    End If

    MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
    MSWord.Visible = True
    MSWord.ActiveDocument.Bookmarks("\StartOfDoc").Select()
    [/VBA]

  2. #2
    VBAX Contributor
    Joined
    Oct 2012
    Location
    Brisbane, Queensland, Australia
    Posts
    163
    Location
    For code to run when a document is created from a template, it needs to be in a routine named Document_New in the ThisDocument object or AutoNew in a standard Module. And I assume that is where you have the MsgBox command in the normal.dot template.

    While the subject of your post says "newly created", your code is actually opening a document "myNewsLetter"

    For a code to run when a document is opened, it needs to be in a routine Document_Open in the ThisDocument object or AutoOpen in a standard module.

    Unless you want this routine to run every time you open any document, I would not have it in the Normal.dot template.

    If you really are wanting to create a new document, it would be better to save the file that is opened as "myNewsLetter" as a template and have the code that you want to run in a Document_New or AutoNew routine in that template. If that template was named "myNewsLetter", then in your VB.Net code, you would use:

    [vba]MSDoc = MSWord.Documents.Add("[templates path]\myNewsLetter")[/vba]

    in place of:

    [vba]MSDoc = MSWord.Documents.Open(myNewsLetter)[/vba]

    You need to substitute the drive:\path to the template for the [Template Path] in the above code.

    It's probably better however to have the code in the VB.Net routine.

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "However, when I run my VB.Net program that creates a brand new Word 2007 document, the VBA code in the Normal.dot template does NOT stop on either of the message boxes,"

    Could please explain what is supposed to happen wtih these messageboxes. Also, what procedure is executing in the Project (NL Skeleton), why would you think that creating a new document would make the messageboxes stop. And what do you mean by stop.

    Be that as it may, Doug is correct. If you want action to happen when a document is created, use Document_New(). For it happen every time, use it in Normal; otherwise put it in a different template.

  4. #4
    VBAX Regular
    Joined
    Mar 2009
    Location
    Stowmarket
    Posts
    62
    Location
    OK, thanks for your replies ... firstly, the fact that I couldn't upload my screenshot means that you couldn't see my procedures, and in my haste to get the question posted before I went to bed, I didn't put enough detail in it, sorry !!!

    So, in my Normal.dot Template by pressing alt-F11 to get to the Project, I have a Top-level Name of "Project (NL Skeleton)" with a Folder, "Microsoft Word Objects" which includes "This Document" ... if I open this, I have a top line of "Option Explicit" & this has "General" & "Declarations" as its heading. Then I have my code, which is just a MsgBox at the moment, and this has "Document" & "New" as its headings. This, I think, is correct and is what you mean. But in the Project explorer on the left of the screen there is now another Top-level Name of "Project (Normal)" and this has an identical structure as described above.

    The reason it is just a MsgBox is because I don't want to enter code until I have got my logic correct.

    HOWEVER ... as you have pointed out, when I open the document, it's not New, it's already been created by my Copy statement, so I need to rethink that approach, thank you !!! I'll take a look at your idea, I'm not really that sure how this Normal.dot template works, I use a lot of trial and error to get things working, because I am a COBOL programmer & understand the logic & the basic ideas, but there is so much other stuff that I barely understand ... thankfully we have Forums like this and people like you two !!!

    EDIT ... and as a test I just opened Word and created a new document, the "Normal" MsgBox opens perfectly, so I see now where I went wrong there, thanks again ...

    ANOTHER EDIT ... I have changed the 2 Procedures to be "Document_Open" & BOTH MsgBoxes are displayed ... which one should I be using ?!?
    Last edited by vodkasoda; 03-09-2013 at 02:53 AM.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    yikes, that is just so confusing I do not know where to start.

    Please post your code. State what module the procedure(s) are in.

    "have changed the 2 Procedures to be "Document_Open" & BOTH MsgBoxes are displayed ... which one should I be using ?!?"

    "the" 2 procedures. Except you do not state what those are. So you changed (whatever they were) to be BOTH Document_Open. One in Normal, and one in the project of the document.

    Which one...how am i supposed to know which one you want to use.

    If there IS Document_Open in Normal, and there IS one in a document - so two of them - then the one in the document supercedes the one in Normal.

    If there is NOT one in Normal, and there is one in the document, the one in the document fires.

    If there is one in Normal, and NOT one in the document, the one in Normal fires.

    In the VBE (Visual Basic Editor) thee is ALWAYS a Normal project. And unless you have closed all documents, there is always a document project as well.

    We need more information, and you are NOT being clear, and you are tossing out things lke we can read your mind.

    "it's already been created by my Copy statement"

    Oh really. That is nice. WHAT Copy statement, and where is that statement.

    Please post code.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    yikes, that is just so confusing I do not know where to start.

    Please post your code. State what module the procedure(s) are in.

    "have changed the 2 Procedures to be "Document_Open" & BOTH MsgBoxes are displayed ... which one should I be using ?!?"

    "the" 2 procedures. Except you do not state what those are. So you changed (whatever they were) to be BOTH Document_Open. One in Normal, and one in the project of the document.

    Which one...how am i supposed to know which one you want to use.

    If there IS Document_Open in Normal, and there IS one in a document - so two of them - then the one in the document supercedes the one in Normal.

    If there is NOT one in Normal, and there is one in the document, the one in the document fires.

    If there is one in Normal, and NOT one in the document, the one in Normal fires.

    In the VBE (Visual Basic Editor) thee is ALWAYS a Normal project. And unless you have closed all documents, there is always a document project as well.

    We need more information, and you are NOT being clear, and you are tossing out things lke we can read your mind.

    "it's already been created by my Copy statement"

    Oh really. That is nice. WHAT Copy statement, and where is that statement.

    Please post code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •