简体   繁体   English

EXCEL VBA-循环错误

[英]EXCEL VBA - LOOP error

I am trying to run a particular function, I tested this yesterday and it works. 我正在尝试运行一个特定的功能,我昨天对此进行了测试,并且可以正常工作。 I wanted this looped but when excel tries the same function for the next row, I get a file path error. 我希望这是循环的,但是当excel在下一行尝试相同的功能时,出现文件路径错误。

Any ideas? 有任何想法吗? Any help would be much appreciated. 任何帮助将非常感激。

Option Explicit

Sub odoc()

Dim fpath As String
Dim objWord As Object
Dim cel As Range
Dim selectedRange As Range

Set objWord = CreateObject("Word.Application")
fpath = Application.ActiveCell.Value
Set selectedRange = Application.Selection

For Each cel In selectedRange.Cells
objWord.Documents.Open (fpath)
objWord.Visible = True
objWord.Application.Run MacroName:="CopySAM"
ActiveCell.Offset(0, 14).Select
ActiveSheet.Paste
objWord.Application.Quit
ActiveCell.Offset(1, -14).Select
Next cel

End Sub

Thanks. 谢谢。

This tidies up your loop - it should now work. 这可以整理您的循环-现在应该可以了。 Though I have to say there are better ways to paste the data than the way you're using.. 尽管不得不说,粘贴数据要比您使用的方法更好。

Sub odoc()

    Dim objWord As Object
    Dim cel As Range
    Dim selectedRange As Range

    Set selectedRange = Application.Selection

    For Each cel In selectedRange.Cells
        Set objWord = CreateObject("Word.Application")
        objWord.Documents.Open (cel)
        objWord.Visible = True
        objWord.Application.Run MacroName:="CopySAM"
        cel.Offset(0, 14).Select
        ActiveSheet.Paste
        objWord.Application.Quit
    Next cel

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM