本文介绍了从Excel打开单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从Excel宏(Office XP)中打开Word.如果使用此代码,它将在Set wdDoc = wordapp.Documents.Open(polozka.ShortPath)行上停止并冻结程序.如果我使用Set wdDoc = GetObject(polozka.ShortPath)而不是此行,程序将在With wdDoc.Selection处停止并出现错误:

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:

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:

You have to declare your variable as Object like below:

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

希望它能为您提供帮助.

I hope it can help you.

这篇关于从Excel打开单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 05:58