Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

vba - Open word from excel

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:

"Object doesn't support this property"

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to declare your variable as Object like below:

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

And to use the doc:

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)

And to close the application:

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

I hope it can help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

2.1m questions

2.1m answers

60 comments

57.0k users

...