Solved

Is it possible to open a Word document (to be displayed) from VBA?

Posted on 2011-09-19
8
751 Views
Last Modified: 2012-05-12
I have created a Word document with form fields which are populated by VBA. I use the following command to save the document:
With doc
doc.SaveAs "C:\Users\xxxx\Documents\FinalLetter.doc"

I would like the Word document to open after it is saved. Is this possible?
0
Comment
Question by:newbie46
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Author Comment

by:newbie46
ID: 36563360
In case the above is not clear, I want the newly created Word document to display automatically so that the user does not have to go to My Documents to find it and open it.
0
 
LVL 11

Accepted Solution

by:
X_layer earned 167 total points
ID: 36563372
Try this:
Private Sub Command1_Click()

    Dim LWordDoc As String
    Dim oApp As Object

    'Path to the word document
    LWordDoc = "c:\Doc1.doc"

    If Dir(LWordDoc) = "" Then
        MsgBox "Document not found."

    Else
        'Create an instance of MS Word
        Set oApp = CreateObject(Class:="Word.Application")
        oApp.Visible = True

        'Open the Document
        oApp.Documents.Open filename:=LWordDoc
    End If

End Sub

Open in new window

0
 
LVL 12

Assisted Solution

by:pdebaets
pdebaets earned 166 total points
ID: 36563383
Try this from a command button:


Dim objApp As Object
Set objApp = CreateObject(Class:="Word.Application")
objApp.Visible = True
objApp.Documents.Open filename:="C:\Users\xxxx\Documents\FinalLetter.doc"

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:newbie46
ID: 36563578
X_layer and pdebaets,
objApp.visible = true
objApp.Documents.Open .... does the trick
but since I have the following in my code already, I'm not sure if I have to close appWord and then reopen it. Please see the code below:
Set appWord = GetObject (, "Word.Application")

set doc = appWord.Documents.Add("C:\Users\LetterTemplate.docx", , True)

With doc

  .FormFields ("Field1") = Field1Value
  .Activate
  .Fields.Unlink
End With

doc.ActiveWindow.visible = true
doc.ActiveWindow.View.Type = wdPrintView
doc.saveAs "C:\ ......"
doc.close wdDoNotSaveChanges

to open the document, do I need to add the following  code or is there another way to incorporate the app.word.documents.open statement?

appWord.Quit
set appWord = CreateObject(Class:="Word.application")
appword.visible = true
appword.documents.open filename:="C:\............"
0
 

Author Comment

by:newbie46
ID: 36563599
initially, I have defined
Dim appWord as Word.Application
Dim doc as Word.document
0
 
LVL 12

Expert Comment

by:danishani
ID: 36563819
Check this article, on how to get already open Instance of Word, or if not exisiting Instance, create a New Instance of Word Object:

http://www.excelguru.ca/node/10#Word

Hope this helps,
Daniel
0
 

Author Comment

by:newbie46
ID: 36567032
The following code will open a Word document:

Set appWord = GetObject (, "Word.Application")
set doc = appWord.Documents.Add("C:\Users\LetterTemplate.docx", , True)
With doc
  .FormFields ("Field1") = Field1Value
  .Activate
  .Fields.Unlink
End With

doc.ActiveWindow.visible = true
doc.ActiveWindow.View.Type = wdPrintView
doc.saveAs "C:\ ......"
doc.close wdDoNotSaveChanges

appWord.visible = true
appword.documents.open FileName:="C:\...........

Exit_PrivateSubName:
set db = nothing
set rs = nothing
' appWord.quit
'Set appWord = Nothing
Exit Sub

I have 2 questions:
1) If I don't comment out appWord.quit and Set appWord = nothing, the Word doc opens and then closes. Will this cause a problem if appWord is not set to Nothing?
2) The Word document is opened, but does not pop up (on top of the Access database). Is there a way to have Word overlay Access, instead of having to click on the Word icon?
0
 
LVL 12

Assisted Solution

by:danishani
danishani earned 167 total points
ID: 36567695
1) If I don't comment out appWord.quit and Set appWord = nothing, the Word doc opens and then closes. Will this cause a problem if appWord is not set to Nothing?
> You need to release the Object when closing, so it will be released from internal memory. That is done by: Set appWord = Nothing. But the Word Instance is still open in the Task Manager, by using appWord.Quit this Instance will be released from the Task Manager.

2) The Word document is opened, but does not pop up (on top of the Access database). Is there a way to have Word overlay Access, instead of having to click on the Word icon?
> Try to use:
appWord.Activate

This makes the Word Instance the activated one, therefore being the active window.

Hope this helps,
Daniel
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question