Forum Discussion

sim2020's avatar
sim2020
New Contributor
5 years ago
Solved

How to SaveAs a word doc which is already open

My application opens up a word document in preview mode. I'm trying to close the preview mode, then it opens the file in a compatibility mode. I need to save as the file in a folder.

I've tried like this:

 

 Sys.Process("WINWORD", 2).Close      - This closes the preview mode

If (Sys.Process("WINWORD").Exists) Then
Set WordObject =Sys.OleObject("Word.Application")
WordObject.ActiveDocument.SaveAs("C:\work.doc")

End If

 

I get an error in the 'WordObject.ActiveDocument.SaveAs("C:\work.doc")' statement 'This command is not available because no document is open'

 

How can I handle this? Any help would appreciate.

 

 

  • sim2020's avatar
    sim2020
    5 years ago

    Thanks, tristaanogre for the reply. That didn't work, unfortunately. I have managed to find the temp file in a folder and take a copy of that as a solution to my issue.

4 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Sys.OleObject doesn't use an exsisting process of Word but actually creates a new instance.  So, your WordObject is a different instance than the running Word instance hence why it doesn't think there is doc open.

     

    Suggested change... no guarentees, just a suggestion.

     

     Sys.Process("WINWORD", 2).Close      - This closes the preview mode
    Set WordObject = Sys.WaitProcess('WINWORD")
    If (WordObject.Exists) Then
    WordObject.ActiveDocument.SaveAs("C:\work.doc")
    
    End If
    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Thank you for help tristaanogre! 

      Hi sim2020 , did you solve the issue?   

    • sim2020's avatar
      sim2020
      New Contributor

      Thanks, tristaanogre for the reply. That didn't work, unfortunately. I have managed to find the temp file in a folder and take a copy of that as a solution to my issue.

      • sonya_m's avatar
        sonya_m
        SmartBear Alumni (Retired)

        Great to hear you’ve found a workaround!