我无法从Excel宏(办公室xp)打开Word。如果我使用此代码,它将在Set wdDoc = wordapp.Documents.Open(polozka.ShortPath)
行停止并冻结程序。如果我使用Set wdDoc = GetObject(polozka.ShortPath)
代替此行,则程序会在With wdDoc.Selection
停止,并且“对象不支持此属性...”错误
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
答案 0 :(得分:12)
你必须将你的变量声明为对象,如下所示
Dim Paragraphe As Object, WordApp As Object, WordDoc As Object
并使用doc:
File= "D:\path"
'creationsession Word
Set WordApp = CreateObject("Word.Application")
'word ll be close to run
WordApp.Visible = False
'open the file .doc
Set WordDoc = WordApp.Documents.Open(File)
并关闭applcation
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
我希望他们可以帮助你
答案 1 :(得分:2)
我有一个类似的问题,excel没有识别word.application命令和其他单词对象。如果您希望excel识别这些对象,则需要在Visual Basic编辑器中选择工具>引用....当您选择引用时,将填充一个窗口,向下浏览列表,直到找到Microsoft Word x.0对象库。选中该复选框,这将允许excel现在识别单词命令。您还可以更改该对象库的优先级,以便下次更容易找到。
答案 2 :(得分:1)
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