I got a VBA project from colleague who is retired.
I have to change a function:
Documents.Add Template:= _
"c:\word\Link.dot", _
NewTemplate:=False, DocumentType:=0
Here a new document (another Word file) is created with a template.
This template also relates data from another project "Common".
Basically, Documents.Add Template:= _"c:\word\Link.dot"
in Link.dot the Document_Open() is executed and the Common project is initialized.
Private Sub Document_Open()
Common.Initialize
End Sub
I don't want a second document opened by Documents.add, it should use the already active document.
I tried these two variants:
#1
Dim oDoc As Document
Set oDoc = ActiveDocument
oDoc.AttachedTemplate = "c:\word\Link.dot"
#2
ActiveDocument.AttachedTemplate = "c:\word\Link.dot"
Nothing happens to both of them, even no Runtime Error. I think it's because Document_Open() wasn't executed.
Document_Open() responds to the Open event. As you are not opening a document when attaching the template it will not execute.
You can execute the code directly though.
ActiveDocument.AttachedTemplate = "c:\word\Link.dot"
Common.Initialize
Related
Is it possible to build something like AutoOpen but less generic? So I mean a macro, which executes when you open file xyz.docx.
Please, copy the next code in "Normal.dotm" "ThisDocument" code module:
Option Explicit
Const docName = "xyz.docx" 'use here the document name you need
Private Sub Document_Open()
If ActiveDocument.Name = docName Then
MsgBox ActiveDocument.Name & " has been opened..."
End If
End Sub
The Open event is triggered for any document being open.
Here's a sample macro that automatically runs when the document opens. This sample checks whether the user is trying to open a template for editing, then it creates a new document based on the template instead. (Bypass the macro by holding down Shift while you open the file).
This sample only makes sense when placed in a macro-enabled template, but you could also add something like this to a macro-enabled document. The document location would also have to be made a trusted location in Windows.
Sub AutoOpen()
Dim PathTemp$, NameTemp$
If ActiveDocument.Type = wdTypeTemplate Then
NameTemp$ = ActiveDocument.Name
PathTemp$ = ActiveDocument.Path
Documents.Add Template:=PathTemp$ & Application.PathSeparator & NameTemp$
For Each fWindow In Application.Windows
If fWindow.Caption = NameTemp$ Then
fWindow.Close SaveChanges:=wdDoNotSaveChanges
End If
Next fWindow
End If
End Sub
I have written a macro in Word and it works every time I open ANY of Word documents. I don't know what is the reason for such an action and I'd like to ask you why?
Private Sub Document_Open()
Dim fso As Object
Dim f As Object
Dim plik As Object
Dim sciezka As String
ActiveDocument.Content.Select
Selection.Delete
ActiveDocument.Select
sciezka = Application.ActiveDocument.Path & "\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(Application.ActiveDocument.Path)
For Each plik In f.Files
If Not Left(Right(plik.Name, 8), 4) = "rozp" And Right(plik.Name, 4) = ".pdf" Then
Selection.TypeText plik.Name
Selection.TypeParagraph
End If
Next plik
End Sub
You probably stored it in normal.dotm document This code will apply to all documents. You should move it and store it in module that is in the document your are dealing with.
Edit 1: You created a .docm document but are you sure you stored your code in the docm document and not in the normal.dotm document?
Edit 2: I created a word test.docm document and put your code in a new module (under project(Test) -> Modules -> Module 1). Then I started the code and it create 1 paragraph per files in the same folder (giving they respect the check in your IF condition). Then, I transfered that code in Project(Test) -> Microsoft Word Objects -> ThisDocument; so it works at opening. It does exactly the same as intended at the opening.
Finally, I tested opening other word documents (docx and docm) in the same folder and the code did not run.
I cannot reproduce your problem running the exact same code. This reinforce the idea that you wrote your code under Normal -> Microsoft Word Object -> ThisDocument while in the VBA frame.
Have a good day,
Jonathan.
(Using Windows 10 and MS Word 2016. Global templates are: Normal.dotx and Autoload.dotm. Attached template to some docs is: Reference.dotx)
Hello everyone,
I'm having problems in VBA getting the attached template of a document.
I have a global template that loads when I load MS Word, called Autoload.dotm. But, for some specific documents, they use an attached template, which is not the global template (Autload.dotm) or the regular template (Normal.dotx). This attached template is called Reference.dotx.
So I use ActiveDocument.AttachedTemplate. But this returns Autoload.dotm, not Reference.dotx. I need to find out if the attached template defined in Developer->Document Template->Templates tab->Document Template is Reference.dotx. (Don't think it makes a difference, but the "Automatically update document styles" checkbox is checked.) Does anyone know how I can find if a document uses Reference.dotx? I don't need any of the global templates returned.
The code I'm using to try to get the attached template is simple:
If (ActiveDocument.AttachedTemplate = "Reference.dotx") Then
PrepareDocument_enabled = True
End If
Maybe this will help you? It will show the template used.
Sub Macro1()
Dim strPath As String
strPath = Dialogs(wdDialogToolsTemplates).Template
MsgBox strPath
End Sub
Otherwise, you can use this to change the template
Sub ChangeAttachedTemplate()
Dim oDoc As Document
Dim oTemplate As Template
Dim strTemplatePath As String
Set oDoc = ActiveDocument
If oDoc.Type = wdTypeTemplate Then Exit Sub
Set oTemplate = oDoc.AttachedTemplate
Debug.Print oTemplate.FullName
' Path is probably: C:\Users\USERNAME\AppData\Roaming\Microsoft\Templates\
If InStr(UCase(oTemplate.FullName), UCase("Path of the template")) > 0 Then
oDoc.AttachedTemplate = "PATH TO TEMPLATE" & "TEMPLATE NAME.dotm"
End If
End Sub
I have a simple macro that opens a Word Document using Excel. I made sure the Word Object Library is properly referenced but when running this macro it freezes after Documents.Open is called (based on me seeing where it fails in the debugger). I don't know if it is a OLE Automation Error but the macro freezes and I have to force close Excel.
Public Const Dir = "C:/Temp/"
Public Const File = "temp.docx"
Public Sub OpenFile()
Dim f As String: f = Dir & File
Dim oWord As Object, oDoc As Object
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Open(f)
oDoc.Visible = True
End Sub
I get this message as well: (even though there is no other application open)
Is there an alternative to opening a file with Excel and how I rewrite my program?
As requested - this is a common problem
You should change your Dir variable - that's a reserved name - and you're probably getting a Word error you can't see when you try to open that "file".
You should also change your File variable name too - that can be a reserved word too depending on references you've set
Added Comment:
With regard to it freezing - you can remove the oDoc.Visible = True statement and replace it with oWord.Visible = True BEFORE the problem statement Set oDoc = oWord.Documents.Open(f). That would popup the error indicating you had a problem with your filename
I'm writing some VBA code (in Excel) to automatically produce reports at work. These reports need to have a specific format, which is easily available through our Company templates. It's all in MS Office 2010.
However, all of these templates have Document_New() subs, which sparks some VBA-code that I don't want initiated, and sadly don't have direct access to (company rules, hell yeah!).
Is there a way to override the Document_New() macro? Right now, my VBA code for opening a document, which sparks the Document_New() macro, is as follows:
Sub GenerateReport()
Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
WordApp.Documents.Add Template:="Letter.dot" ' the "error" is produced at this lie
Dim SaveStr As String
SaveStr = "KommRapp-" & Date & ".docx"
WordApp.ActiveDocument.SaveAs SaveStr
End Sub
Try Something like:
Sub OpenWordDocAndBlockMacros()
Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
'Just to see whats happening
WordApp.Visible = True
Dim InitialSecurityAutoSetting As MsoAutomationSecurity
'Save Automations Security Setting
SecurityAutoSetting = WordApp.Application.AutomationSecurity
WordApp.Application.AutomationSecurity = msoAutomationSecurityForceDisable
WordApp.Documents.Add Template:=" Place Template file path here"
'Do some stuff with the template
End Sub
I have tested it, using Excel VBA to open a Word.dotm which has a Sub called Document_New in the ThisDocument module and it worked.