【问题标题】:vbs macro Method Run of object 'IWshShell3' failed only chokes on larger Word files对象'IWshShell3'的vbs宏方法运行失败仅在较大的Word文件上阻塞
【发布时间】:2020-01-13 16:25:27
【问题描述】:

我对 VBA 真的很陌生。我只粘贴了这个子的最后一部分,但如果有帮助,可以把其余的部分贴上。将不胜感激任何帮助。我已经搜索了几天,但是这个错误的大多数问题都是由路径名中的空格引起的。多谢!

此宏读取标题,然后读取打开的 Word 文档中的每个表格,然后将每一行写入htmlFile 变量。然后它运行将 HTML 文件写入保存路径的 PowerShell ps1 文件。

在较小的 Word 文档上效果很好,但是当我在较大的 Word 文档上运行它时,我得到了这个错误:

运行时错误“-2147024690 (800700ce)”:对象的方法“运行” 'IwshShell3' 失败

代码(在此位之前,Sub 中的所有内容都在读取打开的 Word 文档中的每个表并作为 HTML 代码写入htmlFile):

Sub WriteHtml()
    Dim htmlFile AS String, strText As String

    '
    ' fill htmlFile with HTML code ...

    htmlFile = htmlFile & strText

    Dim pwFileLocation, htmlFileLocation As String
    pwFileLocation = "'O:\Docketbk\DocketToWeb\processFile.ps1'"

    'htmlFileLocation = "'W:" & stringSplits(1) & "'"
    htmlFileLocation = "'Q:\OIT\Web Sites\This Site\Regulatory\Docketbk\" & stringSplits(1) & "'"
    fileNameFinal = Left(ActiveDocument.Name, InStrRev(ActiveDocument.Name, ".") - 1) & ".html"

    Dim shell, command1
    command1 = "powershell -noexit -command powershell.exe -Executionpolicy Bypass -File " & pwFileLocation & "-htmlContent ""'" & htmlFile & """' -savePath " & htmlFileLocation & " -fileName """ & fileNameFinal & """"
    Set shell = CreateObject("WScript.Shell")
    shell.Run command1, 1

End Sub

POWERSHELL 代码:

param(
   [string] $htmlContent,
   [string] $savePath,
   [string] $fileName
)
$fullFilePath = $savePath + "\" + $fileName 

Function CreateHtmlFile()
{
        if (!(Test-Path -Path $fullFilePath)){
            New-Item  $fullFilePath -ItemType File
            WriteHtmlFile       
        } else {
             WriteHtmlFile
        }
}
Function WriteHtmlFile()
{
        Set-Content $fullFilePath $htmlContent.Replace('$DubQ','"')     
}
createHtmlFile

POWERSHELL 移动代码:

param(
   [string] $oldFileLocation,
   [string] $newFileLocation
)

net use W: \\MYSERVERNAME\Websites\PUC\Regulatory\Docketbk

Function MoveFile
{
    $moveDirectory = $oldFileLocation + "/*" 
    Copy-Item -Path $MoveDirectory  -Destination $newFileLocation -Container -Recurse -force
}

MoveFile

【问题讨论】:

标签: vba powershell ms-word


【解决方案1】:

我喜欢保持简单。而且您的大多数(如果不是全部)问题似乎都来自而不是保持简单。你有太多的移动部件,太多的间接层。您可以尝试通过更多尝试来解决这个问题,我们可以尝试简化您的方法。

您所做的一切——创建 HTML 字符串、将其写入文件、复制文件夹——都可以在 VBA 代码中实现。我看不到任何需要 PowerShell 的东西。

对于文件系统操作,Scripting.FileSystemObject 非常方便,下面的代码使用它。让我们通过 VBA IDE 中的工具 → 引用菜单引用“Microsoft Scripting Runtime”库来实现它。

现在我们有两个基本操作,从源文档创建 HTML,以及将给定文本写入文件。

Dim FSO As New Scripting.FileSystemObject

Function CreateHtml(sourceDoc As Document) As String
    ' Creates HTML from the tables of the given document.
    Dim html As String, text As String

    ' ... build html from sourceDoc
    html = html & text

    CreateHtml = html
End Function

Sub WriteTextToFile(content As String, path As String, Optional fileName As String)
    ' Writes a string to a Unicode file. Target will be overwritten if it exists.
    Dim targetPath As String

    If fileName > "" Then
        targetPath = FSO.BuildPath(path, fileName)
    Else
        targetPath = path
    End If

    With FSO.CreateTextFile(fileName:=targetPath, overwrite:=True, unicode:=True)
        .Write content
    End With
End Sub

现在我们可以将它们组合在一个我称为 HtmlWorkflow 的 Sub 中,因为没有更好的名称:

Sub HtmlWorkflow()
    Dim stringSplits As Variant, html as String

    stringSplits = Split("something", "delimiter")
    html = CreateHtml(sourceDoc:=ActiveDocument)

    WriteTextToFile _
        content:=html, _
        path:="Q:\OIT\Web Sites\This Site\Regulatory\Docketbk\" & stringSplits(1), _
        fileName:=FSO.GetBaseName(ActiveDocument.Name) & ".html" _

    FSO.CopyFolder _
        Source:="Q:\whatever\your\source\folder\path\is", _
        Destination:="\\whatever\your\destination\folder\path\is"
End Sub

注意事项:

  • 我在函数调用中使用了命名参数 - 这可以保存一些临时变量并使事情基本上是自记录的。
  • 行尾的_ 是VB 的续行符。使用它可以使长行代码更易于阅读。
  • 将事物分解为单独的函数使调试和测试变得更容易(您现在可以针对任何打开的文档调用 CreateHtml(),无论它是否处于活动状态)并且易于重用(WriteTextToFile 不关心什么文件或什么内容)。
  • FileSystemObject 的文档在这里:https://docs.microsoft.com/en-us/office/vba/language/reference/objects-visual-basic-for-applications

【讨论】:

  • 哇,这看起来干净多了。我只需要一点时间来了解它,并正确设置文件夹源/目标。也不清楚 stringSplits 线。太感谢了。我会努力在早上完成这一切。
  • 好吧,我编造了那一行,因为你的代码使用了stringSplits(1),而你从来没有显示它是什么。我认为它必须是某种字符串拆分操作,所以我放了一个假人。
  • 对函数 CreateHtml 的去向感到困惑。那不是必须在Sub中吗?就像我说的,这几乎是一门外语,我的版本是费了很大力气拼凑而成的。我觉得我很亲近。我什至逐行这样做的原因是清理所有垃圾 Word HTML 标签。你的方式能产生干净的 HTML 吗?再次感谢您的帮助。
  • 在 VBA IDE 中创建一个新模块,并将第一个代码 sn-p 中的两个函数放在那里。或者将它们放入当前代码所在的同一模块中。
  • ...我的第二个代码 sn-p 显示了应该如何使用 CreateHtml() 函数。它必须是Function,因为它返回一个值——一个 HTML 字符串——并且只有函数才能在 VBA 中具有返回值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
相关资源
最近更新 更多