2013-05-07 265 views
2

我無法從Excel宏(office xp)打開Word。如果我使用此代碼,它將停止在行Set wdDoc = wordapp.Documents.Open(polozka.ShortPath)並且程序凍結。如果我使用Set wdDoc = GetObject(polozka.ShortPath)而不是這條線,計劃在這裏With wdDoc.Selection停止與「對象不支持此屬性...」錯誤VBA:從excel打開word

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 

回答

10

你必須聲明你的對象變量像下面

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) 

和關閉的applcation

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

我希望他們能幫助你

+0

謝謝,現在我可以用第一種方式打開文檔,但仍然程序在這裏停止'With wdDoc.Selection' with「Object does not support this property or method」error – koubin 2013-05-09 07:38:37

+0

我找到了解決辦法。將此行改爲'用wdDoc.Selection'改爲'用wordapp.Selection'解決它。但是我仍然不知道,爲什麼我不能使用'wdDoc'對象 – koubin 2013-05-09 15:27:13

0
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 
+0

請正確格式化您的代碼。 [閱讀SO標記在線幫助](http://stackoverflow.com/help/formatting)。 – 2013-09-21 10:53:11

2

我用Excel不承認一個Word.Application指揮等字對象類似的問題。如果你想讓這些對象被excel識別,你將需要在Visual Basic編輯器中選擇Tools> References ...。當您選擇引用時,將填充一個窗口,向下瀏覽列表,直至找到Microsoft Word x.0 Object Library。選中複選框,這將允許excel現在識別單詞命令。您還可以更改該對象庫的優先級以便下次更容易找到。