【问题标题】:run powershell in VBA access在 VBA 访问中运行 powershell
【发布时间】:2017-12-04 11:17:28
【问题描述】:

我需要更改多个文本文件中的字符串

我在 ACCESS VBA 中编写了下面的脚本,但错误是 TYPE MISMATCH

Dim str As String
str = "N=maher"
Call Shell("c:\windows\system32\powershell.exe" - Command("get-content -Path e:\temptest.txt") - Replace(str, "maher", "ali"))

【问题讨论】:

  • 这应该可以工作:-Command ("get-content -Path e:\temptest.txt").Replace("maher", "ali")
  • 我不这么认为。如果我不得不猜测,-replace 旨在在 PowerShell 环境中验证,而不是在 VBA 环境中验证。但是OP没有告诉我们,所以没人知道。
  • 显然是 x-y 问题。如果要使用 VBA 替换文件中的字符串,则无需涉及 PowerShell。

标签: vba powershell ms-access


【解决方案1】:

调用 PowerShell 的语法已经过时了。建议:先自己从命令行运行,然后从 Access 运行(奇怪的选择:它只会让事情变得更复杂)。

执行此操作的 PowerShell 脚本(.ps1 文件)需要包含以下内容:

Get-Content -Path "E:\temptest.txt" | ForEach-Object { $_ -Replace 'maher', 'ali' } | do-something-with-the-updated-content

你需要定义:

  • 您要替换的内容(您将 N=maher 传入,但随后将两个字符串硬编码为 Replace
  • 替换后的字符串怎么办(Get-Content 只读取文件)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2023-03-21
    • 2018-01-27
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多