Forum Discussion

ChandanD's avatar
ChandanD
Contributor
5 years ago

Error while creating word document using Sys.OleObject

Hello,

 

I am unable to create a word document and open the word document. I am adding error message of 

Sorry, we couldn't find your file. Was it moved, renamed, or deleted?
 (C:\KBData\...\Win10\Word_test.docx)

This is the code which I am using.

 

function worddoc ()
{
  Word = Sys.OleObject("Word.Application")
  Word.Visible = true
  Word.Documents.Open("C:\\KBData\\Training_svn\\trunk\\Win10\\Word_test.docx")
  Word.Documents.Add()
//  oWord.Documents.Close()
//  oWord.Quit()
}​

 

  • def Word_test():
      Word = Sys.OleObject["Word.Application"]
      Log.Message("Opening Word and making it visible")
      Word.Visible = True
      #Opening up a new doc
      Word.Documents.Add()
      #activated doc and adding some text
      Word.ActiveDocument.Content = "Here is some text"
      #saving as a new file
      Word.ActiveDocument.SaveAs("C:\\Users\\justin.kim\\OneDrive - SmartBear Software, Inc\\Word_TC_test.docx")
      Log.Message("Shutting MS Word down")
      Word.Quit()

     

    So the error message you are getting is telling me that you have not yet created a word doc with that name. 

    If you are choosing to create a file, then you must save that file first. Although the (Python) syntax above is different, the order in which I used the methods are the same. 

  • def Word_test():
      Word = Sys.OleObject["Word.Application"]
      Log.Message("Opening Word and making it visible")
      Word.Visible = True
      #Opening up a new doc
      Word.Documents.Add()
      #activated doc and adding some text
      Word.ActiveDocument.Content = "Here is some text"
      #saving as a new file
      Word.ActiveDocument.SaveAs("C:\\Users\\justin.kim\\OneDrive - SmartBear Software, Inc\\Word_TC_test.docx")
      Log.Message("Shutting MS Word down")
      Word.Quit()

     

    So the error message you are getting is telling me that you have not yet created a word doc with that name. 

    If you are choosing to create a file, then you must save that file first. Although the (Python) syntax above is different, the order in which I used the methods are the same. 

    • ChandanD's avatar
      ChandanD
      Contributor

      Will the same code be applicable for Excel?

      Or there is some different method for excel. 

      • hkim5's avatar
        hkim5
        Staff

        probably would be similar? 

        Excel.Application

        Excel.Application.Spreadsheet

        so on and so forth?

        The only difference would be that you might have to use nested for loops to target certain rows and columns when you are trying to populate the cells.