读取活动文档的完整路径 [英] Read active document's full path

查看:66
本文介绍了读取活动文档的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我是否可以在Access,Word或AutoCAD等任何应用程序中读取活动文档的完整路径和名称吗?

最好不使用COM.
例如:不公开其类的Autocad LT.

如果可以,怎么办?

谢谢.

Can anyone tell me if it is possible to read the active document''s full path and name in any application such as Access, Word or AutoCAD etc?

Preferably without using COM.
eg: Autocad LT which doesn''t expose its classes.

And if so, how?

Thanks.

推荐答案

在我看来,由于文档类型之间的差异,不可能获得任何MDI应用程序的活动文档的名称.但是...
...我编写了简单的类来获取活动的 MS Office文档 的全名.

In my opinion, it''s impossible to get the name of active document of any MDI application, because of the difference between types of documents. BUT...
... i wrote simple class to get the full name of active MS Office document.

Public Class TOfficeDoc
    Implements IOfficeDoc

    Private Const sAcc As String = "Access.Application"
    Private Const sExc As String = "Excel.Application"
    Private Const sWrd As String = "Word.Application"
    Private Const sPpo As String = "PowerPoint.Application"

    Public Sub New()
        '
    End Sub

    Private Function GetOfficeApp(ByVal sAppName As String) As Object
        Dim obj As Object = Nothing
        Try
            obj = GetObject(, sAppName)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")

        End Try

        Return obj
    End Function

    Function GetOfficeDoc(ByVal iApp As Integer) As String Implements IOfficeDoc.GetOfficeDoc
        Dim sFileName As String = String.Empty

        Try

            Select Case iApp
                Case OffDoc.iAcc '0
                    sFileName = GetAccDoc()

                Case OffDoc.iExc '1
                    sFileName = GetExcDoc()

                Case OffDoc.iWrd '2
                    sFileName = GetWrdDoc()

                Case OffDoc.iPpo '3
                    sFileName = GetPpoDoc()

                Case Else
                    MsgBox("Unknow application!", MsgBoxStyle.Exclamation, "Error...")

            End Select

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")

        End Try

        Return sFileName
    End Function


    Private Function GetAccDoc() As String
        Dim sDoc As String = String.Empty, oApp As Object = Nothing

        Try
            oApp = GetOfficeApp(sAcc)
            If oApp Is Nothing Then Exit Try
            sDoc = oApp.CurrentDb.Name

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")

        Finally
            oApp = Nothing
        End Try

        Return sDoc
    End Function


    Private Function GetExcDoc() As String
        Dim sDoc As String = String.Empty, oApp As Object = Nothing

        Try
            oApp = GetOfficeApp(sExc)
            If oApp Is Nothing Then Exit Try
            sDoc = oApp.ActiveWorkbook.FullName


        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")
        Finally
            oApp = Nothing

        End Try

        Return sDoc
    End Function

    Private Function GetWrdDoc() As String
        Dim sDoc As String = String.Empty, oApp As Object = Nothing

        Try
            oApp = GetOfficeApp(sWrd)
            If oApp Is Nothing Then Exit Try
            sDoc = oApp.ActiveDocument.FullName


        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")
        Finally
            oApp = Nothing

        End Try

        Return sDoc
    End Function

    Private Function GetPpoDoc() As String
        Dim sDoc As String = String.Empty, oApp As Object = Nothing

        Try
            oApp = GetOfficeApp(sPpo)
            If oApp Is Nothing Then Exit Try
            sDoc = oApp.ActivePresentation.FullName

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")
        Finally
            oApp = Nothing

        End Try

        Return sDoc
    End Function


    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class



它仅公开1个功能:GetOfficeDoc.



It expose only 1 function: GetOfficeDoc.

Public Interface IOfficeDoc

    Function GetOfficeDoc(ByVal iApp As Integer) As String

End Interface



公共变量声明:



Declaration of public variables:

Module ModOffDoc

    Public oOffApp As IOfficeDoc = Nothing

    Public Enum OffDoc
        iAcc = 0
        iExc = 1
        iWrd = 2
        iPpo = 3
    End Enum

End Module



用法:



Usage:

oOffApp = New TOfficeDoc()
'to get MS Access document
MsgBox(oOffApp.GetOfficeDoc(0), MsgBoxStyle.Information, "Message...")


据我所知,没有100%的方法可以做到这一点.但是,您也可以尝试检查进程具有的打开文件.

看一下文章列出使用过的文件 [处理v3.46 [
As far as I also know, there''s no 100% way to do this. But as an alternative, you could try checking the open files that a process has.

Have a look at the article Listing Used Files[^]. Perhaps that could be modified to suit your needs. Another option could be to utilize Handle v3.46[^]


这篇关于读取活动文档的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆