VBA Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Good afternoon,

I am using an Access module to contol Excel and Word and I need to know if
there's a way to check to see first, if Excel and Word are running. If they
are not, then I need to run them, If they are already open, I need to see if
doc is already open and if not, to open a blank doc? Any suggestions? Thanks.

Cordially,
 
Hi,

For Word do this --

Dim objWord as Word.Application 'First Make a reference to "Microsoft Word
Object Library"
Dim doc as Word.Document
Dim blnFound as Boolean

On error resume next
Set objWord = GetObject(,"Word.Application") 'checks for running instance of
Word
if objWord is Nothing then
Set objWord = CreateObject("Word.Application") 'if Word not running
instantiate new copy
objWord.Visible = True 'New instnace of Word must be made visible
end if
On error goto 0

for each doc in objWord.Documents
if doc.Name = "YourDocName" then
doc.Activate
blnFound = True
exit for
end if
next

If not blnFound Then
objWord.Documents.Add 'Add a NEw Document to Word
end if


For Excel do this --

Dim objExcel as Excel.Application 'Frist Make a reference to "Microsoft
Excel Object Library"
Dim wb as Excel.Workbook
Dim blnFound as Boolean

On error resume next
Set objExcel = GetObject(,"Excel.Application") 'checks for running instance
of Word
if objExcel is Nothing then
Set objExcel = CreateObject("Excel.Application") 'if Excel not running
instantiate new copy
objExcel.Visible = True 'New instnace of Excel must be made visible
end if
On error goto 0

for each wb in objExcel.Workbooks
if wb.Name = "YourWorkbookName" then
wb.Activate
blnFound = True
exit for
end if
next

If not blnFound Then
objExcel.Workbooks.Add 'Add a New Workbook to Excel
end if


Regards,

Naresh Nichani
Microsoft Access MVP
 
Back
Top