Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-23-2015, 08:58 AM
gedinfo gedinfo is offline How do I set selection to the active document? Windows 7 64bit How do I set selection to the active document? Office 2010 32bit
Novice
How do I set selection to the active document?
 
Join Date: Nov 2015
Posts: 2
gedinfo is on a distinguished road
Smile How do I set selection to the active document?

Hi,

I want to be able to run a macro on a Word 2010 document that i currently have open.
The code below shows the working macro with an open call, what I want to do is run the macro on any opened document, without having to manually set the file.



Any suggestions?

Thank you.


Code:
Sub FontReplaceTimesNewRoman()
'
' FontReplaceTimesNewRoman Macro
'
'
Dim objWordApp As Word.Application
Dim objWordDoc As Word.Document
Dim objSelection As Selection
Dim FileString As String
Const wdReplaceAll = 2

    Set objWordApp = CreateObject("Word.Application")
    FileString = "C:\Downloads\VerificationProcedures.docx"

    objWordApp.Documents.Open (FileString)
    objWordApp.Visible = True

    Set objWordDoc = objWordApp.ActiveDocument
    Set objSelection = objWordApp.Selection

    objSelection.Find.Font.Name = "Times New Roman"
    objSelection.Find.Forward = True
    objSelection.Find.Replacement.Font.Name = "Calibri"
    objSelection.Find.Execute "", , False, , , , , , , , wdReplaceAll

    objWordApp.Documents.Close (Word.WdSaveOptions.wdSaveChanges)
    objWordApp.Quit
    Set objWordApp = Nothing
End Sub
Reply With Quote
  #2  
Old 11-23-2015, 10:49 PM
macropod's Avatar
macropod macropod is offline How do I set selection to the active document? Windows 7 64bit How do I set selection to the active document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

As coded, your macro assumes word is running and opens a document. What if Word isn't running or no document is open?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-23-2015, 11:07 PM
gmayor's Avatar
gmayor gmayor is offline How do I set selection to the active document? Windows 7 64bit How do I set selection to the active document? Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Quote:
Originally Posted by gedinfo View Post
Hi,
I want to be able to run a macro on a Word 2010 document that i currently have open.
From which application are you intending to run this macro? If you are running it from Word as posting to this forum suggests, why are you creating a new Word application in which to run it?
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
acvitedocument, selection

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to save active document to SharePoint document library Rose roon Word VBA 9 09-22-2015 10:53 PM
How do I set selection to the active document? copy active document to shared network drive Rose roon Word VBA 9 07-04-2015 09:55 PM
How do I set selection to the active document? Copying text from another to Active document AlexTeslin Word VBA 1 03-08-2013 09:06 PM
Drag Button which refers to the active Word document to Explorer DRob Word 0 10-03-2010 06:40 AM
Document selection procedure kennethc Word 0 09-15-2010 02:56 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:46 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft