我无法从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

最佳答案

您必须像下面这样声明变量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


希望对您有所帮助。

10-05 21:19