Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 62
Default Word opens Excel workbook - how to close it?

I'm trying to pass values from Word to Excel. The following Word vba
codes do not give an error message, though it doesn't do what I want it
to do (that's another problem).

After the code finished running, I tried to open test.xlsm but got a
message saying the file is open. I was offered the option of opening the
file as read only.

How do I close the file test.xlsm? Thanks.


==============================
Option Explicit

Sub count_words()
Dim oRange As Word.Range
Dim iTotalWords1 As Integer
Dim iTotalWords2 As Integer
Dim iLoop As Integer
Dim n As Integer

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim ExcelWasNotRunning As Boolean
Dim WorkbookToWorkOn As String

'specify the workbook to work on
WorkbookToWorkOn = "C:\Documents and Settings\me\My Documents\test.xlsm"


Windows("test.docx").Activate

'If Excel is running, get a handle on it; otherwise start a new instance
of Excel
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err Then
ExcelWasNotRunning = True
Set oXL = New Excel.Application
End If

On Error GoTo Err_Handler

'If you want Excel to be visible, you could add the line: oXL.Visible =
True here; but your code will run faster if you don't make it visible

'Open the workbook
Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn)


iLoop = ActiveDocument.ComputeStatistics(wdStatisticParagr aphs)

For n = 1 To iLoop

iTotalWords1 = ActiveDocument.ComputeStatistics(wdStatisticWords)

Set oRange = ActiveDocument.Paragraphs(1).Range

oRange.Delete

iTotalWords2 = ActiveDocument.ComputeStatistics(wdStatisticWords)


oXL.ActiveWorkbook.Worksheets("sheet8").Cells(n, 4).Value = iTotalWords1
- iTotalWords2

Next n



If ExcelWasNotRunning Then
oXL.Quit
End If

'Make sure you release object references.

Set oWB = Nothing
Set oXL = Nothing

'quit
Exit Sub

Err_Handler:
MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description,
vbCritical, _
"Error: " & Err.Number
If ExcelWasNotRunning Then
oXL.Quit
End If



End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 1,588
Default Word opens Excel workbook - how to close it?

oWB.Close True

Tim


"John Smith" wrote in message
...
I'm trying to pass values from Word to Excel. The following Word vba
codes do not give an error message, though it doesn't do what I want it
to do (that's another problem).

After the code finished running, I tried to open test.xlsm but got a
message saying the file is open. I was offered the option of opening the
file as read only.

How do I close the file test.xlsm? Thanks.


==============================
Option Explicit

Sub count_words()
Dim oRange As Word.Range
Dim iTotalWords1 As Integer
Dim iTotalWords2 As Integer
Dim iLoop As Integer
Dim n As Integer

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim ExcelWasNotRunning As Boolean
Dim WorkbookToWorkOn As String

'specify the workbook to work on
WorkbookToWorkOn = "C:\Documents and Settings\me\My Documents\test.xlsm"


Windows("test.docx").Activate

'If Excel is running, get a handle on it; otherwise start a new instance
of Excel
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err Then
ExcelWasNotRunning = True
Set oXL = New Excel.Application
End If

On Error GoTo Err_Handler

'If you want Excel to be visible, you could add the line: oXL.Visible =
True here; but your code will run faster if you don't make it visible

'Open the workbook
Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn)


iLoop = ActiveDocument.ComputeStatistics(wdStatisticParagr aphs)

For n = 1 To iLoop

iTotalWords1 = ActiveDocument.ComputeStatistics(wdStatisticWords)

Set oRange = ActiveDocument.Paragraphs(1).Range

oRange.Delete

iTotalWords2 = ActiveDocument.ComputeStatistics(wdStatisticWords)


oXL.ActiveWorkbook.Worksheets("sheet8").Cells(n, 4).Value = iTotalWords1
- iTotalWords2

Next n



If ExcelWasNotRunning Then
oXL.Quit
End If

'Make sure you release object references.

Set oWB = Nothing
Set oXL = Nothing

'quit
Exit Sub

Err_Handler:
MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description,
vbCritical, _
"Error: " & Err.Number
If ExcelWasNotRunning Then
oXL.Quit
End If



End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
when opening an Excel Workbook, another blank workbook also opens Gord Dibben Excel Discussion (Misc queries) 0 October 12th 07 09:49 PM
when opening an Excel Workbook, another blank workbook also opens spmu Excel Discussion (Misc queries) 0 October 12th 07 01:46 PM
help creating a macro in excel that opens a specific word document Prohock Excel Discussion (Misc queries) 3 March 30th 06 04:58 PM
Excel macro that opens new MS Word file and pastes data as a pictu Rob Excel Worksheet Functions 0 July 6th 05 05:12 PM
Personal workbook opens when Excel opens SheriTingle Excel Discussion (Misc queries) 2 March 30th 05 12:22 AM


All times are GMT +1. The time now is 03:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"