在VBA中转换文件关联

在VBA中转换文件关联,vba,excel,ms-word,Vba,Excel,Ms Word,好的,这是我第二次尝试编写代码,也是我被指派进行的第二个VBA宏项目。在过去的一周半里,我一直在努力学习VBA作为我的第一种编码语言,所以我为愚蠢的错误道歉。也就是说,直接谈正事。下面是我为word文档宏准备的内容: Sub MacroToUpdateWordDocs() 'the following code gets and sets a open file command bar for word documents Dim Filter, Caption, Selecte

好的,这是我第二次尝试编写代码,也是我被指派进行的第二个VBA宏项目。在过去的一周半里,我一直在努力学习VBA作为我的第一种编码语言,所以我为愚蠢的错误道歉。也就是说,直接谈正事。下面是我为word文档宏准备的内容:

Sub MacroToUpdateWordDocs()
    'the following code gets and sets a open file command bar for word documents
    Dim Filter, Caption, SelectedFile As String
    Dim Finalrow As String
    Dim FinalrowName As String
    Filter = "xlsx Files (*.xlsx),*.xlsx"
    Caption = "Please Select A .xlsx File, " & TheUser
    SelectedFile = Application.GetOpenFilename(Filter, , Caption)
    'check if value is blank if it is exit
    Finalrow = Cells(Rows.Count, 1).End(xlUp).Row
    FinalrowName = Finalrow + 1
    If (Trim(SelectedFile) = "") Then
        Exit Sub
    Else
        'setting up the inital word application object
        Set auditmaster = CreateObject("excel.sheet")
        'opening the document that is defined in the open file dialog
        auditmaster.Application.Workbooks.Open (SelectedFile)
        'ability to change wether it needs to burn cycles updating the UI
        auditmaster.Visible = False
        'declare excel sheet
        Dim wdoc As Document
        'set active sheet
        Set wdoc = Application.ActiveDocument
        Dim i As Integer
        Dim u As Integer
        Dim ColumnAOldAddy As String
        Dim ColumnCNewAddy As String
        u = 1
        i = 1
        'MsgBox (wordapp.ActiveDocument.Hyperlinks.Count)
        'Sets up a loop to go through the Excel Audit file rows.
        For i = 1 To auditmaster.ActiveSheet.Rows.Count
            'Identifies ColumnAOldAddy and ColumnCNewAddy as columns A and C for each row i.  Column A is the current hyperlink.address, C is the updated one.
            ColumnAOldAddy = auditmaster.Cells(i, 1)
            ColumnCNewAddy = auditmaster.Cells(i, 3)
            'If C has a new hyperlink in it, then scan the hyperlinks in wdoc for a match to A, and replace it with C
            If ColumnCNewAddy = Not Nothing Then
                For u = 1 To doc.Hyperlinks.Count
                    'If the hyperlink matches.
                    If doc.Hyperlinks(u).Address = ColumnAOldAddy Then
                        'Change the links address.
                        doc.Hyperlinks(u).Address = ColumnCNewAddy
                    End If
                'check the next hyperlink in wdoc
                Next
            End If
            'makes sure the macro doesn't run on into infinity.
            If i = Finalrow + 1 Then GoTo Donenow
        'Cycles to the next row in the auditmaster workbook.
        Next
Donenow:
        'Now that we've gone through the auditmaster file, we close it.
        auditmaster.ActiveSheet.Close SaveChanges:=wdDoNotSaveChanges
        auditmaster.Quit SaveChanges:=wdDoNotSaveChanges
        Set auditmaster = Nothing
    End If
End Sub
因此,假设这段代码采用由我的第一个宏创建的超链接审计文件(由于堆栈溢出社区,最后一个bug得到了修复并运行良好!)。审核文件在目标中找到的每个超链接都有3列和一行。docx:a=超链接地址,B=超链接显示文本,C=新的超链接地址

当代码从要更新的.docx文件运行时,它允许用户选择审核文件。从那里,它逐行检查更新的超链接地址是否已由较旧的已审核地址/显示名称写入C列,然后在.docx文件中搜索旧的超链接地址,并将其替换为新的超链接地址。此时,它将完成对文档的搜索,然后转到审核excel文件中的下一行


我的问题是,这些代码的大部分是从excel宏复制/粘贴出来的。我花了很长时间才弄明白如何将代码翻译成适当标识/引用word/excel文档的内容。我希望有更多经验的人可以看看这个宏,让我知道我在哪里完全出了问题。它不断给我“方法或数据成员未找到”的错误,目前到处都是,主要是关于我试图引用审计excel文件的地方。我很确定这是一个相对容易的解决方案,但我没有足够的词汇来找出如何用谷歌搜索答案

编译正常,但未测试:

Sub MacroToUpdateWordDocs()

    Dim Filter, Caption, SelectedFile As String
    Dim Finalrow As String
    Dim appXL As Object
    Dim oWB As Object
    Dim oSht As Object
    Dim wdoc As Document
    Dim ColumnAOldAddy As String
    Dim ColumnCNewAddy As String
    Dim i As Long
    Dim h As Word.Hyperlink
    Dim TheUser As String

    Filter = "xlsx Files (*.xlsx),*.xlsx"
    Caption = "Please Select A .xlsx File, " & TheUser

    Set appXL = CreateObject("excel.application")
    appXL.Visible = True
    SelectedFile = appXL.GetOpenFilename(Filter, , Caption)
    appXL.Visible = False

    If Trim(SelectedFile) = "" Then
        appXL.Quit
        Exit Sub
    Else
        Set oWB = appXL.Workbooks.Open(SelectedFile)
        Set oSht = oWB.worksheets(1)
        Finalrow = oSht.Cells(oSht.Rows.Count, 1).End(-4162).Row '-4162=xlUp
    End If

    Set wdoc = Application.ActiveDocument

    For i = 1 To Finalrow

        ColumnAOldAddy = oSht.Cells(i, 1).Value
        ColumnCNewAddy = oSht.Cells(i, 3).Value

        If ColumnCNewAddy <> ColumnAOldAddy Then
            For Each h In wdoc.Hyperlinks
                If h.Address = ColumnAOldAddy Then
                    h.Address = ColumnCNewAddy
                End If
            Next h
        End If

    Next i

    oWB.Close False
    appXL.Quit

End Sub
Sub-MacroToUpdateWordDocs()
Dim筛选器、标题、SelectedFile作为字符串
模糊最终流为字符串
作为对象的Dim appXL
作为对象的Dim oWB
将oSht作为对象
将wdoc设置为文档
Dim ColumnAlDaddy作为字符串
暗列作为字符串显示
我想我会坚持多久
模糊h为Word.Hyperlink
将用户设置为字符串
Filter=“xlsx文件(*.xlsx),*.xlsx”
Caption=“请选择一个.xlsx文件,”&用户
设置appXL=CreateObject(“excel.application”)
appXL.Visible=True
SelectedFile=appXL.GetOpenFilename(过滤器,标题)
appXL.Visible=False
如果Trim(SelectedFile)=“”,则
appXL.退出
出口接头
其他的
设置oWB=appXL.Workbooks.Open(SelectedFile)
设置oSht=oWB。工作表(1)
Finalrow=oSht.Cells(oSht.Rows.Count,1)。End(-4162)。Row'-4162=xlUp
如果结束
设置wdoc=Application.ActiveDocument
对于i=1到最后一行
ColumnAOldAddy=oSht.Cells(i,1).Value
ColumnCNewAddy=oSht.单元格(i,3).值
如果ColumnCNewAddy ColumnAldaddy那么
对于wdoc.超链接中的每个h
如果h.Address=ColumnAOldAddy,则
h、 地址=列CNEWADDY
如果结束
下一个h
如果结束
接下来我
关闭错误
appXL.退出
端接头

模糊过滤器、标题、SelectedFile As String
的作用与您认为的不同。见我的解释。我不认为这个问题会在这个具体案例中造成问题,但它肯定会,所以这是一个需要改掉的坏习惯。啊,谢谢让·弗朗索瓦。这肯定是我犯下的一个错误,不止这一次。这起到了巨大的作用。现在,我已经学会了如何使用文件选择应用程序正确地引用excel(更不用说清理自己代码的几种方法了)。谢谢你三次了!不过,只是一个简单的问题。这条线是干什么的?如果ColumnCNewAddy ColumnAOldAddy,则仅当“新”地址与“旧”地址不同时,才需要更新超链接<代码>表示“不等于”