L
LA Lawyer
I am using the following code (from Access 2007) to open Word. This is
supposed to start Word if it is not already running, but fails unless Word
is running. What did I do wrong? Here is the full code (including some
stuff about saving the new document which has, I presume, nothing to do with
this issue):
Public Function StartWord(Optional OpenWith As String, Optional SaveItAs As
String)
Dim WordApp As Word.Application
Dim WordWasNotRunning As Boolean
Dim WordDoc As Word.Document
'Get existing instance of Word if it's open; otherwise create a new one
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err Then
Set WordApp = New Word.Application
WordWasNotRunning = True
End If
Set WordDoc = WordApp.Documents.Add(OpenWith)
With WordApp
.Visible = True
.Activate
.WindowState = wdWindowStateMaximize
If Not IsNull(SaveItAs) Then
Dim OriginalSaveItAs As String
OriginalSaveItAs = SaveItAs
Dim theTempNum As Integer
theTempNum = 1
Do Until Len(SaveItAs) = 0 ', i.e., the file doesn't already exist
SaveItAs = InStr(1, OriginalSaveItAs, "." - 1) & "(" &
CStr(theTempNum) & ")" & Right(OriginalSaveItAs, 4)
theTempNum = theTempNum + 1
Loop
.ActiveDocument.SaveAs FileName:=SaveItAs
End If
End With
End Function
supposed to start Word if it is not already running, but fails unless Word
is running. What did I do wrong? Here is the full code (including some
stuff about saving the new document which has, I presume, nothing to do with
this issue):
Public Function StartWord(Optional OpenWith As String, Optional SaveItAs As
String)
Dim WordApp As Word.Application
Dim WordWasNotRunning As Boolean
Dim WordDoc As Word.Document
'Get existing instance of Word if it's open; otherwise create a new one
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err Then
Set WordApp = New Word.Application
WordWasNotRunning = True
End If
Set WordDoc = WordApp.Documents.Add(OpenWith)
With WordApp
.Visible = True
.Activate
.WindowState = wdWindowStateMaximize
If Not IsNull(SaveItAs) Then
Dim OriginalSaveItAs As String
OriginalSaveItAs = SaveItAs
Dim theTempNum As Integer
theTempNum = 1
Do Until Len(SaveItAs) = 0 ', i.e., the file doesn't already exist
SaveItAs = InStr(1, OriginalSaveItAs, "." - 1) & "(" &
CStr(theTempNum) & ")" & Right(OriginalSaveItAs, 4)
theTempNum = theTempNum + 1
Loop
.ActiveDocument.SaveAs FileName:=SaveItAs
End If
End With
End Function