我真的是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的行继续符。使用它可以使较长的代码行更易于阅读。 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/