Consulting

Results 1 to 7 of 7

Thread: Modify default text into "normal.dotm" with VBA

  1. #1
    VBAX Newbie
    Joined
    Aug 2018
    Posts
    3
    Location

    Modify default text into "normal.dotm" with VBA

    Hi,

    I'm trying to make a macro to change the default text into "normal.dotm" but everytime i try to write in it, it somehow corrupt the "normal.dotm", as i can see without any modification the file is like 24 ko and when i try to write for exemple "test" into it using vba it end up with a size of like 1ko and when I try to open it says it's corrupted and it regenerate it...

    Is there a special trick for .dotm files ?

    Thanks in advance !

    If you need my code i can copy paste it here but it's a basic write into a file code !

    PS : I'm able to write into .txt file no problem with this code.

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    5,861
    Location
    Moved from the Excel forum
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    The normal template is a system file and should not have default text. If you want to have a template with default text then create a new template and base the documents that require such text on that template.

    However it should be possible to edit the normal template (or any other template) using VBA e.g.

    Sub Macro1()
    Dim oDoc As Document
    Dim oRng As Range
        Set oDoc = Documents.Open(Options.DefaultFilePath(wdUserTemplatesPath) & "\Normal.dotm")
        Set oRng = oDoc.Range
        oRng.Text = "The normal template is not intended to be used in this way!"
        oDoc.Close wdSaveChanges
    lbl_Exit:
        Set oDoc = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Newbie
    Joined
    Aug 2018
    Posts
    3
    Location
    Thanks you gmayor, but when I try to run your code in my Access program (using vba)
    Sub WriteFileTest()
    
    Dim oDoc As Document
    Dim oRng As Range
        Set oDoc = Documents.Open(Options.DefaultFilePath(wdUserTemplatesPath) & "\Normal.dotm")
        Set oRng = oDoc.Range
        oRng.Text = "The normal template is not intended to be used in this way!"
        oDoc.Close wdSaveChanges
    lbl_Exit:
        Set oDoc = Nothing
        Set oRng = Nothing
        Exit Sub
    
    
    End Sub
    I got this error : oDoc.Range <= Erreur de compilation : Membre de méthode ou de données introuvable

    I have this ref in my project :
    Microsoft Word 15.0 Object Library

  5. #5
    If you are running Word code from another VBA enabled Office application, the syntax is different. You need instead the following and the reference to the Word 15.0 object library. Frankly I see even less reason to modify the normal template from Access than from Word. Why are you doing this? Surely it would be better to simply create a new document from Access?

    Sub WriteFileTest()
    Dim wdApp As Object
    Dim oDoc As Object
    Dim oRng As Object
    Dim bStarted As Boolean
    
        On Error Resume Next
        Set wdApp = GetObject(, "Word.Application")
        If Err Then
            Set wdApp = CreateObject("Word.Application")
            bStarted = True
        End If
        On Error GoTo 0
        wdApp.Visible = True
        Set oDoc = wdApp.Documents.Open(wdApp.Options.DefaultFilePath(2) & "\Normal.dotm")
        Set oRng = oDoc.Range
        oRng.Text = "The normal template is not intended to be used in this way"
        oDoc.Close -1
    lbl_Exit:
        If bStarted = True Then wdApp.Quit
        Set wdApp = Nothing
        Set oDoc = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Newbie
    Joined
    Aug 2018
    Posts
    3
    Location
    It works just fine with your code !

    Thanks a lot,

    It's only for educationnal purpose to show my coworkers to not trust any office based application.

    Have a good day !

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,585
    Location
    Quote Originally Posted by Zeroqt View Post
    It's only for educationnal purpose to show my coworkers to not trust any office based application.
    It's not apparent to me how that educates anyone, since most office based applications can be trusted... Besides which, it does nothing to educate them about malicious code that runs automatically when a given file is opened - which how most malicious macro code is designed to run.
    Cheers
    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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