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
Related
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)
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.
I want to:
Select all emails from a certain sender
Copy the body of the email to a new Word document
Save the word document to a specific directory
Clear the clipboard
I'd like to know what else I need to do, especially in the the FindSetAside() and SaveToDicrectory() functions.
Sub FindSetAside() 'find all set-aside emails
End Sub
Sub PasteToWord()
Dim Word As Word.Application
Dim Doc As Word.Document
Dim activeMessage As Outlook.MailItem 'the email to copy
Dim activeBody As String
If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then
'get the active email
Set activeMessage = ActiveExplorer.Selection.Item(1)
'setup Word
Set Word = CreateObject("Word.Application")
WordApp.Visible = True
setDoc = Word.Documents.Add
'Copy selection to document
activeMessage.GetInspector().WordEditor.Range.FormattedText.Copy
Doc.Range.Paste
Call ClearClipboard
End If
End Sub
Sub SaveToDirectory() 'Save the Word Document to the correct directory
End Sub
Public Sub ClearClipboard()
Dim Data As New DataObject
Data.SetText Tex:=Empty
Data.PutInClipboard
End Sub
I am using Outlook 2010. I am also considering adding some code to send the Word document as an attachment to specific emails, but perhaps that is irrelevant to this question.
Using Explorer.Selection won't help if you need to execute a search for certain emails (unless you use Explorer.Search, which executes a search in the UI). Take a look at this to help determine the search method you need to use:
https://msdn.microsoft.com/EN-US/library/ff869846.aspx
Then it is just a matter of traversing the returned collection and accessing the MailItem objects within.
To save the Word document, use Document.SaveAs2.
Is there any way to create a VBA code to read a word document via audio? as if the computer was reading the whatever is on the document. (of course all text).
I found this searching on google. http://vbadud.blogspot.com/2010/06/vba-how-to-convert-text-file-to-speech.html
Sub Speech_FromFile_Example()
Dim oVoice As SpVoice
Voice Object Dim oVoiceFile As SpFileStream
File Stream Object Dim sFile As String
File Name Set oVoice = New SpVoice
Set oVoiceFile = New SpFileStream
oVoice.Speak "This is an example for reading out a file"
sFile = "C:\ShasurData\ForBlogger\SpeechSample.txt"
oVoiceFile.Open sFile
oVoice.SpeakStream oVoiceFile
End Sub
The code requires Microsoft Speech Object Library
VBA macro for Word:
Dim speech as SpVoice
Sub SpeakText()
On Error Resume Next
Set speech = New SpVoice
If Len(Selection.Text) > 1 Then 'speak selection
speech.Speak Selection.Text
SVSFlagsAsync + SVSFPurgeBeforeSpeak
Else 'speak whole document
speech.Speak ActiveDocument.Range(0, ActiveDocument.Characters.Count).Text
SVSFlagsAsync + SVSFPurgeBeforeSpeak
End If
Do
DoEvents
Loop Until speech.WaitUntilDone(10)
Set speech = Nothing
End Sub
Sub StopSpeaking()
'Based on a macro by Mathew Heikkila
'used to interrupt any running speech to text
On Error Resume Next
speech.Speak vbNullString, SVSFPurgeBeforeSpeak
Set speech = Nothing
End Sub
Be careful if you are using Windows 10 - I had two libraries with the same name "Microsoft Speech Object Library" - make sure you select the one which points to Windows/system32/Speech/Common/sapi.dll
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