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

问题描述

我无法从Excel宏(office xp)中打开Word。如果我使用这个代码,它将在上停止设置wdDoc = wordapp.Documents.Open(polozka.ShortPath)并且程序冻结。如果我使用设置wdDoc = GetObject(polozka.ShortPath)而不是这一行,程序在这里停止使用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 "Object doesn't support this property..." 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


推荐答案

你必须将你的变量声明为下面的对象

you have to declare your variable as object like below

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)

并关闭应用程序

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

我希望他们能帮助你

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

09-18 12:07