Solved

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

Posted on 2011-09-19
8
714 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
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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now