vba 从excel打开word
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16418292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open word from excel
提问by koubin
I can't open Word from Excel macro (Office XP). If I use this code, it will stop on line Set wdDoc = wordapp.Documents.Open(polozka.ShortPath)
and program freezes. If I use Set wdDoc = GetObject(polozka.ShortPath)
instead of this line, program stops here With wdDoc.Selection
with error:
我无法从 Excel 宏 (Office XP) 打开 Word。如果我使用此代码,它将在线停止Set wdDoc = wordapp.Documents.Open(polozka.ShortPath)
并且程序冻结。如果我使用Set wdDoc = GetObject(polozka.ShortPath)
而不是这一行,程序会在此处停止With wdDoc.Selection
并出现错误:
"Object doesn't support this property"
“对象不支持此属性”
Dim wordapp As Word.Application
Dim wdDoc As Word.Document
Set fso = CreateObject("Scripting.FileSystemObject")
Set files = fso.GetFolder("C:\path").Files
Set wordapp = CreateObject("Word.Application")
For Each polozka In files
Set wdDoc = wordapp.Documents.Open(polozka.ShortPath)
wordapp.Visible = True
With wdDoc.Selection
.HomeKey Unit:=6
.Find.Text = "Název (typ):"
.Find.Wrap = wdFindContinue
...
End With
...
wordapp.Quit
Set wordapp = Nothing
Next
回答by user2358068
You have to declare your variable as Object
like below:
你必须as Object
像下面这样声明你的变量:
Dim Paragraphe As Object, WordApp As Object, WordDoc As Object
And to use the doc:
并使用文档:
File= "D:\path"
'Word session creation
Set WordApp = CreateObject("Word.Application")
'word will be closed while running
WordApp.Visible = False
'open the .doc file
Set WordDoc = WordApp.Documents.Open(File)
And to close the application:
并关闭应用程序:
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
I hope it can help you.
我希望它能帮助你。
回答by Jacob Huttel
I had a similar problem with excel not recognizing the word.application command and other word Objects. If you want those objects to be recognized by excel you will need to select Tools>References... in the Visual Basic editor. When you select References a window will populate, go down through the list until you find Microsoft Word x.0 Object Library. Select the check box, this will allow excel to now recognize word commands. You can also change the priority level of that Object Library to make it easier to find next time.
我有一个类似的问题,excel 无法识别 word.application 命令和其他单词 Objects。如果您希望 Excel 能够识别这些对象,您需要在 Visual Basic 编辑器中选择 Tools>References...。当您选择“引用”时,将出现一个窗口,向下浏览列表,直到找到 Microsoft Word x.0 对象库。选中复选框,这将允许 excel 现在识别单词命令。您还可以更改该对象库的优先级,以便下次更容易找到。
回答by Cpt Dangerous
Sub substitute()
'
' substitute Macro
'
' Note: In Excel VBA, in tools -> references: Enable Microsoft Word 12.0 0bject
'
Dim FindStr As String
Dim ReplaceStr As String
Dim path_src As String
Dim path_dest As String
' Define word object
Dim WA As Object
Dim cs As Worksheet
Dim Idx As Integer
' Data worksheet "Data" col A find text, Col B replace text
Set cs = ActiveWorkbook.Worksheets("Data")
Set WA = CreateObject("Word.Application")
WA.Visible = True
path_src = "C:\Temp\data.docx"
path_dest = "C:\Temp\data_out.docx"
WA.documents.Open (path_src)
' Set word object active
WA.Activate
' Optional, use Idx to limit loop iterations
Idx = 1
Do While ((Len(cs.Cells(Idx, 1).Value) > 1) And (Idx < 100))
'
FindStr = cs.Cells(Idx, 1).Value
ReplaceStr = cs.Cells(Idx, 2).Value
With WA
.Selection.HomeKey Unit:=wdStory
.Selection.Find.ClearFormatting
.Selection.Find.Replacement.ClearFormatting
With .Selection.Find
.Text = FindStr
.Replacement.Text = ReplaceStr
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
.Selection.Find.Execute Replace:=wdReplaceAll
End With
Idx = Idx + 1
Loop
WA.Application.ActiveDocument.SaveAs path_dest
WA.documents.Close
Set WA = Nothing
End Sub