Override Document_New() in Office 2010 from VBA - vba

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.

Related

Apply code to active document not document opened with Documents.Add

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

Ignore an "external" error when opening a Word document

I'm attempting to copy the contents of a word document, but the document has embedded vba which causes a compile error anytime I open it.
The error reads
"The code in this project must be updated for use on 64-bit systems."
I expect this error to occur, so how do I tell my vba code in Access to ignore the errors without changing the word doc?
My vba code so far - the DisplayAlerts = wdAlertsNone does not work, neither does setting it to False:
Private Sub cmdQuickLtr_Click()
Dim wApp As Word.Application
Set wApp = CreateObject("Word.Application")
wApp.DisplayAlerts = wdAlertsNone
Dim doc As Object
Set doc = wApp.Documents.Open(Me.tbLetterPath.Value)
doc.Content.Copy
doc.Close
'do something with the copied content
wApp.DisplayAlerts = wdAlertsAll
Set doc = Nothing
Set wApp = Nothing
End Sub
Thank you all in advance.

Selection.Goto from Excel to Word

I am using a spreadsheet to capture test cases, and in the process of automating the generation of a Word document for presentation to the business. I can't get the GoTo function to work across the files, however.
All of the subs are written in the Excel VBA instance.
Here are the two pertinent subs:
Sub 1
Sub CreateTestDocument()
Set wordapp = CreateObject("word.Application")
Set Wordfile = wordapp.Documents.Open("S:\myPath\myFilename.dotm")
wordapp.Visible = True
AddNextCase ("FeatureCases")
End Sub
'Sub 2 (called from Sub 1)
Sub AddNextCase(Bmark As String)
Wordfile.Activate
Wordfile.SelectAllEditableRanges
ActiveWindow.Selection.Goto What:=wdGoToBookmark, Name:=Bmark 'ERROR HERE
Selection.TypeText "TEST1"
End Sub
on the Selection.Goto line, I get the error below:
Run-time error 438:
Object doesn't support this property or method
I've tried various different approaches to this, but i always hit a blocker on setting my entry point to start putting this text block in, what's the obvious issue i'm missing?
You need to make sure that a couple things are addressed:
Make sure that the wordapp and wordfile objects are defined at the module level. I have shown this below using object type references from the Microsoft Word XX.X Object Library. This will also make sure that any word constants you use are defined.
Then you need to use the wordapp context for the activewindow call. This is done with wordapp.activewindow
Private wordapp As Word.Application, _
wordfile As Word.Document
Sub CreateTestDocument()
Set wordapp = CreateObject("word.Application")
Set wordfile = wordapp.Documents.Open("FILE NAME")
wordapp.Visible = True
AddNextCase ("FeatureCases")
End Sub
Sub AddNextCase(Bmark As String)
wordfile.Activate
wordfile.SelectAllEditableRanges
wordapp.ActiveWindow.Selection.Goto What:=wdGoToBookmark, Name:=Bmark
wordapp.Selection.TypeText "TEST1"
End Sub
As per MDN (paraphrased):
.ActiveWindow is an Application property that returns the
active window (Window Object).
You can't however apply a method to a property, you need to apply it to a Window Object instead. Easiest way of achieving this is to store the window object into a variable.
Additionally, your variables such as wordapp or Wordfile are presumably objects, since you have Set them, but there are nowhere declared in your code.
So your code should look something like this:
Private Wordfile as Word.Document
Set Wordfile = CreateObject("word.Application")
Private word as Word.Application
Set word = wordapp.Documents.Open("S:\myPath\myFilename.dotm")
' ... rest of the code
wordapp.ActiveWindow.Selection.GoTo '... etc
In general I would recommend doing a bit of studying into objects, because it looks like you have some learning gaps (with all due to respect)

Is there any method equivalent for "FollowHyperlink" in Outlook VBA?

Is there any method in Outlook VBA equivalent to Excel's FollowHyperlink?
I made an Excel VBA macro that posts the text in the clipboard to a specific site using FollowHyperlink method.
I want to do the same thing with Outlook VBA but I couldn't find the method.
Any method that does the same thing as simply as FollowHyperlink is quite fine.
Here is the Excel VBA version of a function that post the content of the clipboard to Google translation. I want to make an Outlook version of this one.
Public Sub GoogleTranslate_2EN()
Dim clipBoard As New DataObject
With clipBoard
On Error Resume Next
.GetFromClipboard
Dim targetText As String
targetText = .GetText
On Error GoTo 0
End With
Dim URL As String
URL = "https://translate.google.co.jp/?hl=en&tab=wT#auto/en/" & targetText
ThisWorkbook.FollowHyperlink Address:=URL, newWindow:=False, AddHistory:=True
End Sub
I've decided to use Word.Application object and use "FollowHyperlink" from a Word object. It worked!
Now I can Google translate emails in Germany to English as soon as I receive them. :-)
Public Sub ClipboardGoogleTranslate_2EN()
Dim wdApp As Word.Application
Set wdApp = CreateObject("Word.Application")
wdApp.Run "ClipboardGoogleTranslate_2EN"
wdApp.Quit
Set wdApp = Nothing
End Sub

Call Word VBA MsgBox function from Excel

Is it possible to call Word's MsgBox function from Excel VBA? I am having trouble doing this as MsgBox is not a method of Word.Application.
I have tried this:
Dim objWord As Word.Application
Set objWord = New Word.Application
Dim objDoc As Word.Document
Set objDoc = objWord.Documents.Add
objWord.Visible = True
objWord.Activate
Call objWord.Run("MsgBox", "Hello World") ' Or:
Call objDoc.Run("MsgBox", "Hello World")
But I get the error "Object doesn't support this property or method", no matter which 'Call' line I include.
I am trying to do this so that I can display a message box in front of the open Word document saying that the document rendering has completed. If I just call Excel's MsgBox then I have to click on the flashing button in the Windows Taskbar before I can see the message box.
The word document is generated entirely by my Excel macro.
Is this possible?
I have some functions that export modules and re-import them. It is configured now for PPT but was hacked together from Excel functions that I found. There is also a way to use VBA to write VBA. Both are fairly advanced though. Probably both sets of functions are available here:
http://www.cpearson.com/excel/vbe.aspx
However, that's probably overkill. This is probably your best bet, to use a popup instead of a VBA msgBox.
Sub Test()
Dim wdApp As Object
Dim shl As Object
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
wdApp.Activate
Set shl = CreateObject("wscript.shell")
shl.Popup ("Hello, world!")
Set shl = Nothing
Set wdApp = Nothing
End Sub
See also here for more detail about how to control it's timeout/etc.
Whats the best way to display a message box with a timeout value from VBA?
It is! The problem with the way that you're doing it is that the run function of the word application looks for macros with that name and runs them. The way that I was able to accomplish this is by making a subroutine in the word doc that creates the message box.
Public Sub Testing()
Dim objWord As Word.Application
Set objWord = New Word.Application
Dim objDoc As Word.Document
Set objDoc = objWord.Documents.Open("C:\whatnot\Stuff.docm")
objWord.Visible = True
objWord.Activate
objWord.Run ("Testing")
End Sub
Then in the Word document:
Public Sub Testing()
MsgBox ("Hello World")
End Sub