Opening a Word file

情到浓时终转凉″ 提交于 2020-07-03 17:33:05

问题


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

I can't open my file.

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:


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.



来源:https://stackoverflow.com/questions/60995114/opening-a-word-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!