EXCEL VBA在指定位置打开Word,Edit和Saveas. [英] EXCEL VBA to Open Word, Edit and Saveas in the specified location.

查看:90
本文介绍了EXCEL VBA在指定位置打开Word,Edit和Saveas.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试打开W​​ord应用程序,在指定位置编辑,另存为,并需要检查用户是否输入了正确的文件名.这是我的代码

Am trying to Open the Word application, Edit, Saveas in the specified location and Need to check whether user has entered the correct Filename. Here's my code

Dim Doc
Dim DocPath
Dim DocObj
Dim VarResult

DocPath = "C:\MyFolder\MyDocument.doc"    
Set DocObj = CreateObject("word.Application")
Doc = DocObj.Documents.Open(DocPath)
DocObj.Visible = True

打开文档后,我要进行一些更改

After opening the document I am doing some changes

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

现在,我在另存为文件时遇到问题.我使用了两种替代方法,1:GetSaveAsFilename,2:SaveAs.我需要显示saveas对话框(具有所有DefaultLocation,InitialFilename,DocumentType,Title属性).无论用户是否未指定取消"按钮,用户都需要进行选择并需要进行验证.

Now, I have an issue in saveas the file. I used both the alternative methods, 1: GetSaveAsFilename, 2: SaveAs. I need the saveas dialog box to appear(with all DefaultLocation, InitialFilename, DocumentType, Title properties). User needs to select and the same needed to be validated, whether user has not given Cancel button.

varResult = Doc.GetSaveAsFilename( _
FileFilter:="DP Document (*.doc), *.doc, DP Document (*.docx), *.docx", Title:="Save DP", initialvalue:="InitialDocument")
If varResult <> False Then
MsgBox "File choosen = " & varResult
Else
MsgBox "Please select the file"
End If

正在出现运行时错误.预先感谢.

Am getting Run-time error. Thanks in advance.

推荐答案

根据此 Microsoft文章,如果将CreateObject函数与Word.Application或Word.Basic类型的对象一起使用,则如果Word已经在运行,则该函数将失败."通过运行时错误指示失败.Microsoft建议您检查Word是否已在运行.如果尚未运行,请启动Word的新实例."例如,您可以使用"GetObject函数创建Word.Application对象.如果GetObject函数失败,则Word没有运行,因此CreateObject函数用于设置Word.Application对象."链接文章中提供的代码如下:

According to this Microsoft Article, "If you use the CreateObject function with an object of type Word.Application or Word.Basic, the function fails if Word is already running." The failure is indicated by a Run-Time error. Microsoft suggests that you "check to see whether Word is already running. If it is not, start a new instance of Word." For example, you could use "the GetObject function to create a Word.Application object. If the GetObject function fails, Word is not running, so the CreateObject function is then used to set the Word.Application object." The code provided in the linked article is as follows:

Sub RunWord()

   Dim wObj As Word.Application
   On Error Resume Next

   ' Get existing instance of Word if it exists.
   Set wObj = GetObject(, "Word.Application")

   If Err <> 0 Then
      ' If GetObject fails, then use CreateObject instead.
      Set wObj = CreateObject("Word.Application")
   End If

   ' Add a new document.
   wObj.Documents.Add

   ' Exit Word.
   wObj.Quit

   ' Clear object memory.
   Set wObj = Nothing

End Sub

这篇关于EXCEL VBA在指定位置打开Word,Edit和Saveas.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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