• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

VBA in Access 2007 unable to open Word 2007

Dim dirf As String
    Dim rng As Range

    dirf = MFunctions.ReadFile("CC_FileDir.txt")

    mpath = "C:\" & dirf & "\Reports\Patient-Summary-for-Bob-Smith"

    Set wo = CreateObject("Word.Application")
   
   'Add a new document
    wo.Documents.Add
   
    'To speed the document creation
    wo.ScreenUpdating = False

    Set rng = wo.Application.ActiveDocument.Range
   
    wo.WindowState = wdWindowStateMaximize
   
    'Create the Title
    With rng
        .Paragraphs.Last.Range.Style = wdStyleHeading2
        .Text = "Hello World"
    End With
      

   
    wo.WindowState = wdWindowStateMaximize
    wo.ActiveDocument.SaveAs FileName:=mpath
    wo.Visible = True
0
jbotts
Asked:
jbotts
  • 4
  • 2
3 Solutions
 
jbottsAuthor Commented:
Failed to mention that using the submitted code, Word 2007 opens with the ribbon but I am unable to view the file contents. I am able to find the file in the directory where it was saved, and double click of the file will open Word normally.
0
 
GrahamSkanCommented:
It might help if you make the application visible immediately after creating it.
0
 
GrahamSkanCommented:
Also, it is better practice to capture the new document into an object variable instead of relying on the ActiveDocument:
    Dim dirf As String
    Dim rng As Range
    Dim doc As Word.Document
    
    dirf = MFunctions.ReadFile("CC_FileDir.txt")

    mpath = "C:\" & dirf & "\Reports\Patient-Summary-for-Bob-Smith"

    Set wo = CreateObject("Word.Application")
     wo.Visible = True
  
   'Add a new document
    Set doc = wo.Documents.Add
   
    'To speed the document creation
    wo.ScreenUpdating = False

    Set rng = doc.Range
   
    wo.WindowState = wdWindowStateMaximize
   
    'Create the Title
    With rng
        .Paragraphs.Last.Range.Style = wdStyleHeading2
        .Text = "Hello World"
    End With
   
    wo.WindowState = wdWindowStateMaximize
    wo.ActiveDocument.SaveAs FileName:=mpath

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jbottsAuthor Commented:
With: wo.Visible = True placed after Set wo = CreateObject("Windows.Application"), Word opens normally but with a blank screen and with the file name on top. There is no content of the file. I have tried assigning the new document to the variable doc and still have the same problem. I am able to run this code on another computer without problems.
0
 
jbottsAuthor Commented:
The following code now works:
    Dim wo As Word.Application
    Set wo = CreateObject("Word.Application")
    Dim doc As Word.Document
    Dim rng As Range
    Dim mpath As String
    Dim dirf As String
   
    dirf = MFunctions.ReadFile("CC_FileDir.txt")

    mpath = "C:\" & dirf & "Patient-Summary-for-Bob-Smith.docx"

    With wo
        .WindowState = wdWindowStateMaximize
        .Documents.Add
        Set doc = wo.ActiveDocument
        Set rng = doc.Range
        With rng
            .Paragraphs.Last.Range.Style = wdStyleHeading2
            .Text = "Hello World"
        End With
    End With
   
    doc.SaveAs mpath
    wo.Visible = True
0
 
jbottsAuthor Commented:
I did not select my own solution as the best solution.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now