?
Solved

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

Posted on 2011-09-19
8
Medium Priority
?
767 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 668 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 664 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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 668 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

764 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