4

我无法从 Excel 宏 (Office 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
4

3 回答 3

14

你必须as Object像下面这样声明你的变量:

Dim Paragraphe As Object, WordApp As Object, WordDoc As Object

并使用文档:

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)

并关闭应用程序:

WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing

我希望它可以帮助你。

于 2013-05-07T12:40:39.127 回答
3

我遇到了类似的问题,excel 无法识别 word.application 命令和其他单词对象。如果您希望这些对象被 excel 识别,您需要在 Visual Basic 编辑器中选择 Tools>References...。当您选择引用时,将弹出一个窗口,向下浏览列表,直到找到 Microsoft Word x.0 对象库。选中复选框,这将允许 excel 现在识别单词命令。您还可以更改该对象库的优先级,以便下次更容易找到。

于 2013-12-13T21:10:29.047 回答
2
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  
于 2013-09-21T10:31:26.687 回答