Solved

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

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

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

920 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

20 Experts available now in Live!

Get 1:1 Help Now