0
votes

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.

1
so which line does your code get stuck ?HTH
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ᴇʜ
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ᴇʜ
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 meHTH
@Pᴇʜ no, I didn't. I will give it a try before the turn into the pumpkin.MKR

1 Answers

2
votes

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.