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
jbottsAsked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
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
 
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
 
GrahamSkanRetiredCommented:
It might help if you make the application visible immediately after creating it.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
jbottsConnect With a Mentor Author 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
 
jbottsConnect With a Mentor Author 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.