PDA

View Full Version : Modify default text into "normal.dotm" with VBA



Zeroqt
08-29-2018, 02:06 PM
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.

Paul_Hossler
08-29-2018, 04:22 PM
Moved from the Excel forum

gmayor
08-29-2018, 09:16 PM
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

Zeroqt
08-30-2018, 02:13 AM
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

gmayor
08-30-2018, 05:28 AM
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

Zeroqt
08-30-2018, 07:08 AM
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 !

macropod
08-30-2018, 04:59 PM
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.