vba - vbs宏方法对象 'IWshShell3'的运行仅在较大的Word文件上失败

标签 vba powershell ms-word

我真的是VBA新手。我仅粘贴了该子句的最后一部分,但可以在需要时将其粘贴。不胜感激。我已经搜索了几天,但大多数与此错误有关的问题是由路径名中的空格引起的。额外谢谢你!

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

在较小的Word文档上效果很好,但是在较大的Word文档上运行时,出现此错误:

Run-time error '-2147024690 (800700ce)': Method 'Run' of object 'IwshShell3' failed



代码(此位之前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

最佳答案

我喜欢保持简单。而且,大多数(如果不是全部)问题似乎都来自于保持简单性。您有太多的 Activity 部件,太多的间接层。您可以尝试通过添加更多内容来解决该问题,我们可以尝试简化您的方法。

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

对于文件系统操作,Scripting.FileSystemObject非常方便,下面的代码使用它。通过VBA IDE中的“工具”→“引用”菜单,通过引用“Microsoft脚本运行时”库中的,使它可用。

现在,我们有两个基本操作:从源文档创建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的行继续符。使用它可以使较长的代码行更易于阅读。
  • 将内容分解为单独的功能可以更轻松地进行调试和测试(现在,无论是否处于 Activity 状态,您都可以针对任何打开的文档调用CreateHtml())并且易于重用(WriteTextToFile不在乎哪个文件或内容是什么)。
  • 此处是FileSystemObject的文档:https://docs.microsoft.com/en-us/office/vba/language/reference/objects-visual-basic-for-applications
  • 关于vba - vbs宏方法对象 'IWshShell3'的运行仅在较大的Word文件上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59720422/

    相关文章:

    sql-server - 从 Powerpoint 连接到 SQL Server

    excel - 如何在Excel中选择最常见的数字中最高的?

    powershell - 检查文件的前 n 个字节是否在 Powershell 中是特定的

    vba - 仅查找样式为 "Heading 1"的文本(Range.Find 以匹配样式)

    c# - 如何在 C#.NET 中从 MS Word 访问图像名称?

    vba - 如何删除单元格中的非数字字符并将结果连接到 URL?

    vba - Excel 日期格式

    powershell - Powershell数组数组循环过程

    windows - 为什么 power shell clear(或 cls)命令接受数字作为参数?

    python - 可以用Python编辑doc文件吗?