0

I would like to open my Word file (to make the changes then save it under different name).

I can't open my file.

enter image description here

My first code:

Sub RamsOpen2()

Dim Doc
Dim DocPath
Dim DocObj
Dim VarResult

DocPath = "C:\Users\mariuszk\Desktop\cf10\RAMS.docx"
Set DocObj = CreateObject("word.Application")
Doc = DocObj.Documents.Open(DocPath)
DocObj.Visible = True

With Doc.ActiveDocument
    Set myRange = .Content
    With myRange.Find
        .Execute FindText:="FindText", ReplaceWith:="ReplaceText", Replace:=2
    End With
End With

VarResult = Doc.GetSaveAsFilename( _
FileFilter:="DP Document (*.doc), *.doc, DP Document (*.docx), *.docx", Title:="Save DP", 
initialvalue:="InitialDocument")

End Sub

which comes from here:
EXCEL VBA to Open Word, Edit and Saveas in the specified location.
This is roughly what I want to do with my Word file, however question is on the first step.

I have read here, that it is a common problem. VBA Excel - Unable to open existing Word Document file

I found a closer answer to my situation here: Excel macro - open specific word file
Following the advice from this query I mounted the following code:

Sub RamsOpen3()
Dim appWD As Word.Application
Set appWD = New Word.Application
Dim docWD As Word.Document
Set docWD = appWD.Documents.Open("C:\Users\mariuszk\Desktop\cf10\RAMS.docx")
appWD.Visible = True
'
' Data is selected and copied into "Design"
'
'Copy all data from Design
 Sheets("Frontsheet").Select
 Range("D18").Copy
' Tell Word to create a new document

appWD.Selection.Paste
' Save the new document with a sequential file name
Sheets("Sheet1").Select
appWD.ActiveDocument.SaveAs filename:=ThisWorkbook.path & "/" & "TEST" & Range("C8").Text
' Close this new word document
appWD.ActiveDocument.Close
' Close the Word application
appWD.Quit
End Sub

but the problem is the same.

Another answer is here: Excel VBA to Open Multiple Word files in a loop
but I don't want to open all Word documents in the folder.

This simple solution: VBA to open Excel or/and Word Files
also brings the same error.

Community
  • 1
  • 1
Geographos
  • 827
  • 2
  • 23
  • 57
  • so which line does your code get stuck ? – HTH Apr 02 '20 at 15:35
  • Well, if VBA says it couldn't find your file then it is either not there or you don't have the permissions to read it. Did you try with another file? Did you try another location/path? Make sure your file is not already opened. Note that this `Set DocObj = CreateObject("word.Application")` opens a new instance of Word but you never quit it. Check your task manager and kick out all Word instances. Make sure you always run `DocObj.Quit` in case of error or in the end. Otherwise a Word might stay open. – Pᴇʜ Apr 02 '20 at 15:36
  • And if all that doesn't help. Just in case: Did you turn it off and on again? Do a reboot of your computer. – Pᴇʜ Apr 02 '20 at 15:38
  • @HTH this is: Set docWD = appWD.Documents.Open("C:\Users\mariuszk\Desktop\cf10\RAMS.docx") or: DocPath = "C:\Users\mariuszk\Desktop\cf10\RAMS.docx" – Geographos Apr 02 '20 at 15:40
  • 1
    getting stuck at such a statement as `DocPath = "C:\Users\mariuszk\Desktop\cf10\RAMS.docx"` being `Dim DocPath` (i.e. a `Variant` type) looks impossible to me – HTH Apr 02 '20 at 15:42
  • `Doc = DocObj.Documents.Open(DocPath)` - this line is missing a `Set`, but I would think you'd get an "Object required" error before getting the error you've shown above. – dwirony Apr 02 '20 at 15:51
  • 1
    @Pᴇʜ no, I didn't. I will give it a try before the turn into the pumpkin. – Geographos Apr 02 '20 at 15:55
  • Are you able to successfully open the file manually? Just looking at the file icon it looks like its a Macro-Enabled file, and should have the `.docm` file extension. – Fink Apr 02 '20 at 16:54
  • @Fink Since when are icons in windows tied to the file content? I'm pretty sure they are tied to the file extension (you can easily test this by renaming a file). But you were on the right track. The solution is, that the OP has file extensions turned invisible in Explorer. Therfore the real file name is `RAMS.docx.docm`, which would explain the icon and due to the non-showed file extension it appears as `RAMS.docx`. So here `.docx` is **not** the file extension but part of the file name. – Pᴇʜ Apr 02 '20 at 17:37

1 Answers1

2

As Fink pointed out in the comments your file icon looks like your file is a docm file. Since icons are chosen by file extensions there is only one solution: Check if you have file extensions turned invisible in Explorer (https://fileinfo.com/help/windows_10_show_file_extensions).

Your file is obviously called RAMS.docx.docm but you cannot see the file extension .docm.

Checkout if the following works

Set docWD = appWD.Documents.Open("C:\Users\mariuszk\Desktop\cf10\RAMS.docx.docm")

or turn on your file extension view and rename the file.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • It works, the file is being opened, however the debugger keeps on saying that subscript is out of range. – Geographos Apr 24 '20 at 11:55
  • @MariuszKrukar This is a new error and therefore a new question. Please open a new question for that post your current code and tell which error message you get in which line. Otherwise it is impossible to give answers. – Pᴇʜ Apr 24 '20 at 11:58