[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-09-19
8
Medium Priority
?
784 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
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.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

649 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