Call Word VBA MsgBox function from Excel - vba

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

Related

Simple Outlook macro to convert selected text to 'code'

I'm trying to create a macro in Outlook to allow me to select text and convert that text to 'code' (Courier New, black). I'm using Outlook for Microsoft 365 which (I think) uses the Word editor...
I've copied some code I found in another answer and tweaked it, but no dice, and I fear I'm missing something obvious.
Public Sub FormatSelectedText()
Dim objItem As Object
Dim objInsp As Outlook.Inspector
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
On Error Resume Next
Set objItem = Application.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
Set objInsp = objItem.GetInspector
If objInsp.EditorType = olEditorWord Then
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
With objSel
.Font.Name = "Courier New"
.Font.Color = RGB(0, 0, 0)
End With
End If
End If
End If
End Sub
I'm not a VBA guy, so it's likely something obvious :) I'm not even sure if it's a problem with the code or with the general macro support.
FWIW, in the past I wrote a separate VBA script to throw a warning message when I send an email to multiple people with different email domains (to avoid accidental data cross-sharing) which works fine, so I know a little bit...
To be clearer, I have added this code into a macro within the ThisOutlookSession object
I then open a new email and add some random text, select some of the text and run the macro, but the text in the email body doesn't change.
The code works correctly. You just need to add a COM reference to the Word object model to be able to use the Word object model in Outlook VBA macros.
In VBA editor window, click the “Tools” button in the menu bar.
Then, from the drop down list, select the “References” option.
The “References – Project 1” dialog box will display.
In this dialog box, you can pull the scrolling bar down until you locate what you need, in your case it is “Microsoft Word 16.0 Object Library”.
Mark the checkbox in front of the required entry and click “OK”.
Now you have added the Word object library reference successfully.

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

Override Document_New() in Office 2010 from 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.