Using VBA to open Word from Excel

J

johnftough

Hi,

I am new to VBA.

I want to open a Word doc from within an Excel macro. This is no
problem. However, I want to open the Word doc using a template file. I
have tried various ways but without success. This is a bit of code I
used:

Dim AppWord As Object
Dim strTemplateName As String 'Path to Word Template file to use

strTemplateFile = "\\beastie\public\Word Templates\template.dot"
'
Set AppWord = CreateObject("Word.Application")
AppWord.Visible = True

'
' the following line gives a compile error
'
AppWord.filenew Template:=strTemplateName

' hardcode a row for testing
Rows("4:4").Select
Selection.Copy

AppWord.Documents.Add
AppWord.Selection.Paste

Application.CutCopyMode = False
Set AppWord = Nothing

Does anyone have a solution?

Thanks.
 
D

Dave Peterson

Here's one that I've saved:

Option Explicit
Sub testme01()

Dim oWord As Object
Dim myWordTemplate As String
Dim testStr As String

myWordTemplate = "c:\msoffice\templates\1033\Elegant Fax.dot"

testStr = ""
On Error Resume Next
testStr = Dir(myWordTemplate)
On Error GoTo 0
If testStr = "" Then
MsgBox myWordTemplate & " doesn't exist"
Exit Sub
End If

On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err Then
Set oWord = CreateObject("Word.Application")
End If

oWord.Visible = True
oWord.Documents.Add Template:=myWordTemplate

End Sub
 
J

johnftough

Hi,

I am new to VBA.

I want to open a Word doc from within an Excel macro. This is no
problem. However, I want to open the Word doc using a template file. I
have tried various ways but without success. This is a bit of code I
used:

    Dim AppWord As Object
    Dim strTemplateName As String 'Path to Word Template file to use

    strTemplateFile = "\\beastie\public\Word Templates\template.dot"
'
    Set AppWord = CreateObject("Word.Application")
    AppWord.Visible = True

'
' the following line gives a compile error
'
    AppWord.filenew Template:=strTemplateName

    ' hardcode a row for testing
    Rows("4:4").Select
    Selection.Copy

    AppWord.Documents.Add
    AppWord.Selection.Paste

    Application.CutCopyMode = False
    Set AppWord = Nothing

Does anyone have a solution?

Thanks.

Solved it:

AppWord.Documents.Add Template:="C:\public\Word Templates
\template.dot"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top