c# - Excel文件的VBA代码中的文本替换

标签 c# .net excel vba office-interop

我们有几十个启用了宏的 excel 文件,每个文件都包含几个 VBA 模块,并且在每个模块中都有 SQL 服务器名称和 sql 登录的用户 ID/密码。

我想知道我是否可以编写某种 C# 实用程序来一个一个地加载这些文件,或者使用 .NET-Office Interop。或任何其他方法用其他东西替换这些字符串...只是因为我必须将所有这些 VBA 宏重新指向另一个服务器名称并使用另一个 sql 登录名和密码...我真的不想通过以下方式进行替换手 :( :( :(

谢谢!

最佳答案

开头

很抱歉花了一些时间发帖,但我正在为它创建一个 UI,这样它不仅可以帮助您,也可以帮助任何其他寻求相同功能的人。

您需要先启用Trust Access to the VBA project Object model

打开 Excel 并单击"file"选项卡 |选项 |信任中心 |信任中心设置 |宏设置

启用宏并点击Trust access to Visual Basic projects

enter image description here

在 VBA 编辑器中下一步

点击工具 |选项并在“编辑器”选项卡下选中复选框 Require Variable Declaration

enter image description here

下一步 从 here 下载示例文件然后只需按下 Sheet1 中的 Run 按钮即可启动用户表单,如下所示。

简单地选择包含ONL Excel 文件的文件夹。输入相关信息,然后单击 Start Replace 就完成了:)

enter image description here

使用的代码

Sheet1 代码区

Option Explicit

Private Sub CommandButton1_Click()
    UserForm1.Show
End Sub

用户表单代码区

Option Explicit

Private Sub CommandButton1_Click()
    Dim Ret
    Ret = BrowseForFolder
    If Ret = False Then Exit Sub
    TextBox1.Text = Ret
End Sub

Private Sub CommandButton3_Click()
    On Error GoTo Whoa

    Dim wb As Workbook
    Dim strPath As String, strfile As String
    Dim strToReplaceWith As String, strToReplace As String
    Dim i As Long, j As Long

    Dim VBE As Object

    strPath = TextBox1.Text & "\"

    strfile = Dir(strPath)

    While strfile <> ""
        Set wb = Workbooks.Open(strPath & strfile)

        Set VBE = ActiveWorkbook.VBProject

        If VBE.VBComponents.Item(1).Properties("HasPassword").Value = False Then
            If VBE.VBComponents.Count > 0 Then
                For i = 1 To VBE.VBComponents.Count
                    VBE.VBComponents.Item(i).Activate

                    If VBE.VBE.CodePanes.Item(i).CodeModule.CountOfLines > 0 Then
                        For j = 1 To VBE.VBE.CodePanes.Item(i).CodeModule.CountOfLines
                            If InStr(1, VBE.VBE.CodePanes.Item(i).CodeModule.Lines(j, 1), TextBox2.Text, vbTextCompare) Then
                                strToReplace = VBE.VBE.CodePanes.Item(i).CodeModule.Lines(j, 1)
                                strToReplaceWith = Replace(strToReplace, TextBox2.Text, TextBox3.Text, 1, 1, vbTextCompare)
                                VBE.VBE.CodePanes.Item(i).CodeModule.ReplaceLine j, strToReplaceWith
                            End If
                        Next
                    End If
                Next i
            End If
        End If

        wb.Close True

        strfile = Dir
    Wend

LetsContinue:
    Application.ScreenUpdating = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

'~~> Function to pop the browse folder dialog
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
    Dim ShellApp As Object

    '~~> Create a file browser window at the default folder
    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

    '~~> Set the folder to that selected.  (On error in case cancelled)
    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error GoTo 0

    '~~> Destroy the Shell Application
    Set ShellApp = Nothing

    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
        If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
        If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
        GoTo Invalid
    End Select

    Exit Function

Invalid:
    BrowseForFolder = False
End Function

Private Sub CommandButton4_Click()
    Unload Me
End Sub

更多快照

enter image description here

运行宏前需要替换其代码的文件

enter image description here

宏运行后

enter image description here

编辑

替代文件下载位置

如果上面的 wikisend 链接失效,可以从 here 下载文件。

关于c# - Excel文件的VBA代码中的文本替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11033870/

相关文章:

c# - 使用smtp端口发送邮件时的异常处理

c# - JObject 正在丢失引用

c# - 如何使用 WPF C# 检测光标何时离开书面文本框

c# - "interface based programming"到底是什么?

c# - SqlDataReader.GetString 和 sqlnullvalueexception

excel - Excel 中的二次和三次回归

c# - 线程和 IEnumerable; "Collection Was Modified"- 异常

c# - 如何使用 .NET 从 SSRS 报告中获取数据源信息

excel - 如何在Excel中创建动态按钮

excel - 有什么方法可以在excel中为相同的相邻单元格着色?