打开一个Word文件 [英] Opening a Word file

查看:64
本文介绍了打开一个Word文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开我的Word文件(进行更改然后将其保存为其他名称).

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

我无法打开文件.

我的第一个代码:

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

来自这里:
EXCEL VBA以打开Word,编辑和将Saveas保存在指定位置.
这大致就是我要处理的Word文件的内容,但是问题就在第一步上.

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.

我在这里读到,这是一个普遍的问题. VBA Excel-无法打开现有的Word文档文件

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

我在这里找到了更接近我的情况的答案: Excel宏-打开特定的Word文件
根据此查询的建议,我安装了以下代码:

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

但问题是相同的.

另一个答案在这里: Excel VBA打开循环中有多个Word文件
但我不想打开该文件夹中的所有Word文档.

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.

这个简单的解决方案:通过VBA打开Excel或Word文件
也带来同样的错误.

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

推荐答案

正如芬克在评论中指出的那样,您的文件图标看起来像您的文件是 docm 文件.由于图标是由文件扩展名选择的,因此只有一种解决方案:检查文件扩展名是否在资源管理器中不可见( https://fileinfo.com/help/windows_10_show_file_extensions ).

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).

您的文件显然称为 RAMS.docx.docm ,但是您看不到文件扩展名 .docm .

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

检查以下项目是否有效

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

或打开文件扩展名视图并重命名文件.

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

这篇关于打开一个Word文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆