读取活动文档的完整路径

编程入门 行业动态 更新时间:2024-10-24 20:18:48
本文介绍了读取活动文档的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以告诉我是否可以在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[^]

更多推荐

读取活动文档的完整路径

本文发布于:2023-11-30 05:36:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   完整   文档

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!