By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,314 Members | 1,539 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,314 IT Pros & Developers. It's quick & easy.

Open word files using combo

P: n/a
jpr
Hello,
I have a form with a cbo which get's its data from a table. This combo
returns names of MS Word files in the following path:
C:\shares\files\*.dot

I would like to open these files (actually it should open a copy of the
template and not the dot file itself) using the OnChange event of my
cbo.

Is there a way or some code to help me? Thanks

Feb 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"jpr" <jp***@tin.it> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Hello,
I have a form with a cbo which get's its data from a table. This combo
returns names of MS Word files in the following path:
C:\shares\files\*.dot

I would like to open these files (actually it should open a copy of the
template and not the dot file itself) using the OnChange event of my
cbo.

Is there a way or some code to help me? Thanks

Although there are several methods you could use, I would use Word
automation as it will allow you to do lots of extra things with the document
when you create it - such as filling in values or saving it ina specified
place.
You can look up the pros and cons of early/late binding if you are
interested, but you could simply take my advice and use late binding for
Word code, such as:

Private Sub CreateWordDoc()

On Error GoTo Err_Handler

Dim wdApp As Object 'Word.Application
Dim wdDoc As Object 'Word.Document
Dim blnError As Boolean
Dim blnNewApp As Boolean
Dim strTemplate As String

strTemplate = Nz(Me.cboTemplate, "")

If Len(strTemplate) > 0 Then
If Len(Dir(strTemplate)) > 0 Then
' The template exists
Else
MsgBox "The template does not exist", vbExclamation
Exit Sub
End If
Else
MsgBox "First select a template", vbInformation
Me.cboTemplate.SetFocus
Exit Sub
End If

' Switch off normal error handling as we attempt to use ruuning instance
' of Word. If Word is not running, we try to start an instance
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set wdApp = CreateObject("Word.Application")
If Err.Number <> 0 Then
blnError = True
MsgBox "Error starting Word", vbCritical, "Error"
GoTo Exit_Handler
Exit Sub
Else
blnNewApp = True
End If
End If

' Switch normal error handling back on
On Error GoTo Err_Handler

Set wdDoc = wdApp.Documents.Add(strTemplate)

wdApp.Visible = True

wdApp.WindowState = 1

wdApp.Browser.Target = 1

Application.Echo True, ""

wdApp.Activate

wdDoc.Activate

Exit_Handler:

If Not wdDoc Is Nothing Then
Set wdDoc = Nothing
End If

If Not wdApp Is Nothing Then
If blnError And blnNewApp Then
wdApp.Quit
Set wdApp = Nothing
End If
End If

Exit Sub

Err_Handler:
DoCmd.Hourglass False
blnError = True
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub
Feb 14 '06 #2

P: n/a

"Anthony England" <ae******@oops.co.uk> wrote in message
news:ds**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
"jpr" <jp***@tin.it> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Hello,
I have a form with a cbo which get's its data from a table. This combo
returns names of MS Word files in the following path:
C:\shares\files\*.dot

I would like to open these files (actually it should open a copy of the
template and not the dot file itself) using the OnChange event of my
cbo.

Is there a way or some code to help me? Thanks

Although there are several methods you could use, I would use Word
automation as it will allow you to do lots of extra things with the
document when you create it - such as filling in values or saving it ina
specified place.
You can look up the pros and cons of early/late binding if you are
interested, but you could simply take my advice and use late binding for
Word code, such as:

Private Sub CreateWordDoc()

<snip old version> End Sub


A minor adjustment on the 'clear up and exit' bit was needed so now the
complete sub is:
Private Sub cmdWord_Click()

On Error GoTo Err_Handler

Dim wdApp As Object 'Word.Application
Dim wdDoc As Object 'Word.Document
Dim blnError As Boolean
Dim blnNewApp As Boolean
Dim strTemplate As String

strTemplate = Nz(Me.cboTemplate, "")

If Len(strTemplate) > 0 Then
If Len(Dir(strTemplate)) > 0 Then
' The template exists
Else
MsgBox "The template does not exist", vbExclamation
Exit Sub
End If
Else
MsgBox "First select a template", vbInformation
Me.cboTemplate.SetFocus
Exit Sub
End If

' Switch off normal error handling as we attempt to use ruuning instance
' of Word. If Word is not running, we try to start an instance
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set wdApp = CreateObject("Word.Application")
If Err.Number <> 0 Then
blnError = True
MsgBox "Error starting Word", vbCritical, "Error"
GoTo Exit_Handler
Exit Sub
Else
blnNewApp = True
End If
End If

' Switch normal error handling back on
On Error GoTo Err_Handler

Set wdDoc = wdApp.Documents.Add(strTemplate)

wdApp.Visible = True

wdApp.WindowState = 1

wdApp.Browser.Target = 1

Application.Echo True, ""

wdApp.Activate

wdDoc.Activate

Exit_Handler:

If Not wdDoc Is Nothing Then
Set wdDoc = Nothing
End If

If Not wdApp Is Nothing Then
If blnError And blnNewApp Then
wdApp.Quit
End If
Set wdApp = Nothing
End If

Exit Sub

Err_Handler:
DoCmd.Hourglass False
blnError = True
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub
Feb 14 '06 #3

P: n/a
jpr
Thanks for your help. Just some additional help since I am not able to
work you code.

What I am trying to do is open work templates stored in a shared folder
of my lan.

The path will be C:\files\templates\*.dot.

I have copied you code in the OnChange event of my cbo which is named
lsteforms and simply changes the cbotemplate to lsteforms.

My cbo gets its data from a table named cboforms which lists the names
of the word templates (ex.21, 22, 23 - these are named because refer to
forms).

The cbo displays the word files but it does not open them.

Perhaps should I indicate the path or is there something else I am
doing wrong?

Thanks.

Feb 15 '06 #4

P: n/a

"jpr" <jp***@tin.it> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Thanks for your help. Just some additional help since I am not able to
work you code.

What I am trying to do is open work templates stored in a shared folder
of my lan.

The path will be C:\files\templates\*.dot.

I have copied you code in the OnChange event of my cbo which is named
lsteforms and simply changes the cbotemplate to lsteforms.

My cbo gets its data from a table named cboforms which lists the names
of the word templates (ex.21, 22, 23 - these are named because refer to
forms).

The cbo displays the word files but it does not open them.

Perhaps should I indicate the path or is there something else I am
doing wrong?

Thanks.

The variable strTemplate should hold the complete path to the template.
Examples:

strTemplate="C:\files\templates\example1.dot"

strTemplate="\\MyServer\SharedFolder\templates\exa mple2.dot"

So if your combo just showed 21, 22, 23, etc but this meant
C:\files\templates\21.dot
C:\files\templates\22.dot
etc

Then the code would be:

Private Sub cmdWord_Click()

On Error GoTo Err_Handler

Dim wdApp As Object 'Word.Application
Dim wdDoc As Object 'Word.Document
Dim blnError As Boolean
Dim blnNewApp As Boolean
Dim strTemplate As String

strTemplate = Nz(Me.cboTemplate, "")

If Len(strTemplate) > 0 Then
strTemplate="C:\files\templates\" & strTemplate & ".dot"
If Len(Dir(strTemplate)) > 0 Then
' The template exists
Else
MsgBox "The template does not exist", vbExclamation
Exit Sub
End If
Else
MsgBox "First select a template", vbInformation
Me.cboTemplate.SetFocus
Exit Sub
End If

' Switch off normal error handling as we attempt to use ruuning instance
' of Word. If Word is not running, we try to start an instance
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set wdApp = CreateObject("Word.Application")
If Err.Number <> 0 Then
blnError = True
MsgBox "Error starting Word", vbCritical, "Error"
GoTo Exit_Handler
Exit Sub
Else
blnNewApp = True
End If
End If

' Switch normal error handling back on
On Error GoTo Err_Handler

Set wdDoc = wdApp.Documents.Add(strTemplate)

wdApp.Visible = True

wdApp.WindowState = 1

wdApp.Browser.Target = 1

Application.Echo True, ""

wdApp.Activate

wdDoc.Activate

Exit_Handler:

If Not wdDoc Is Nothing Then
Set wdDoc = Nothing
End If

If Not wdApp Is Nothing Then
If blnError And blnNewApp Then
wdApp.Quit
End If
Set wdApp = Nothing
End If

Exit Sub

Err_Handler:
DoCmd.Hourglass False
blnError = True
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Feb 15 '06 #5

P: n/a
jpr
Thaks. working fine. Great job.
Anthony England wrote:
"jpr" <jp***@tin.it> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Thanks for your help. Just some additional help since I am not able to
work you code.

What I am trying to do is open work templates stored in a shared folder
of my lan.

The path will be C:\files\templates\*.dot.

I have copied you code in the OnChange event of my cbo which is named
lsteforms and simply changes the cbotemplate to lsteforms.

My cbo gets its data from a table named cboforms which lists the names
of the word templates (ex.21, 22, 23 - these are named because refer to
forms).

The cbo displays the word files but it does not open them.

Perhaps should I indicate the path or is there something else I am
doing wrong?

Thanks.

The variable strTemplate should hold the complete path to the template.
Examples:

strTemplate="C:\files\templates\example1.dot"

strTemplate="\\MyServer\SharedFolder\templates\exa mple2.dot"

So if your combo just showed 21, 22, 23, etc but this meant
C:\files\templates\21.dot
C:\files\templates\22.dot
etc

Then the code would be:

Private Sub cmdWord_Click()

On Error GoTo Err_Handler

Dim wdApp As Object 'Word.Application
Dim wdDoc As Object 'Word.Document
Dim blnError As Boolean
Dim blnNewApp As Boolean
Dim strTemplate As String

strTemplate = Nz(Me.cboTemplate, "")

If Len(strTemplate) > 0 Then
strTemplate="C:\files\templates\" & strTemplate & ".dot"
If Len(Dir(strTemplate)) > 0 Then
' The template exists
Else
MsgBox "The template does not exist", vbExclamation
Exit Sub
End If
Else
MsgBox "First select a template", vbInformation
Me.cboTemplate.SetFocus
Exit Sub
End If

' Switch off normal error handling as we attempt to use ruuning instance
' of Word. If Word is not running, we try to start an instance
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set wdApp = CreateObject("Word.Application")
If Err.Number <> 0 Then
blnError = True
MsgBox "Error starting Word", vbCritical, "Error"
GoTo Exit_Handler
Exit Sub
Else
blnNewApp = True
End If
End If

' Switch normal error handling back on
On Error GoTo Err_Handler

Set wdDoc = wdApp.Documents.Add(strTemplate)

wdApp.Visible = True

wdApp.WindowState = 1

wdApp.Browser.Target = 1

Application.Echo True, ""

wdApp.Activate

wdDoc.Activate

Exit_Handler:

If Not wdDoc Is Nothing Then
Set wdDoc = Nothing
End If

If Not wdApp Is Nothing Then
If blnError And blnNewApp Then
wdApp.Quit
End If
Set wdApp = Nothing
End If

Exit Sub

Err_Handler:
DoCmd.Hourglass False
blnError = True
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Feb 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.