【问题标题】:word vba to set sPath as current directoryword vba 将 sPath 设置为当前目录
【发布时间】:2016-06-13 21:02:41
【问题描述】:

循环粘贴 .emf 文件到 word 中的简单代码:

Sub LoopEMF()
Dim sPic As String
Dim sPath As String

sPath = "C:\Users\me\Desktop\Test2\"
sPic = Dir(sPath & "*.emf")

Do While sPic <> ""
    Selection.TypeParagraph
    Selection.InlineShapes.AddPicture _
      FileName:=sPath & sPic, _
      LinkToFile:=False, SaveWithDocument:=True
    sPic = Dir
    Selection.TypeParagraph
Loop
End Sub

我只想查看word文件(即打开的)所在的活动目录,而不是指定目录。很多搜索都没有找到任何线索——这令人惊讶、尴尬,并且可能意味着我没有使用正确的关键词。

帮助?

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    当我打开一个 Excel 文档 D:\db\tmp\test1.xlsm:

    CurDir() returns C:\Users\[username]\Documents
    
    ActiveWorkbook.Path returns D:\db\tmp
    

    所以 CurDir() 有一个系统默认值,可以更改。

    ActiveWorkbook.Path 对于同一个保存的工作簿不会改变。

    例如,当您执行“文件/另存为”命令并在文件/目录选择对话框中选择一个随机目录时,CurDir() 会发生变化。然后单击取消跳过保存。但是 CurDir() 已经变成了上次选择的目录。

    发件人:
    How to get current working directory using vba?

    【讨论】:

      【解决方案2】:

      更尴尬的是——我一直在使用正确的代码:

      Sub NewLoopEMF()
      Dim sPic As String
      Dim sPath As String
      
      sPath = ActiveDocument.path & "\"
      sPic = Dir(sPath & "*.emf")
      
      Do While sPic <> ""
          Selection.TypeParagraph
          Selection.InlineShapes.AddPicture _
            FileName:=sPath & sPic, _
            LinkToFile:=False, SaveWithDocument:=True
          sPic = Dir
          Selection.TypeParagraph
      Loop
      End Sub
      

      不幸的是,活动目录位于同步的 SharePoint 文件夹中,因此返回的名称是超文本格式 (http://all the rest/),这样一来,您就知道什么是松散的了。我通过使用以下代码解决了这个问题:

      Sub GetActiveDocumentPath()
      
      MsgBox ActiveDocument.path
      
      End Sub
      

      所以看起来简单的解决方案是不使用共享点文件夹来存储项目。任何人都为我们这些在 SharePoint 环境中工作的人提供了一个聪明的解决方案?

      【讨论】:

      • Stack Overflow 不是一个讨论“论坛”——它是一个问答网站。所以你不应该在“答案”中发布后续问题(在这种情况下你不太可能得到回应)。一种选择是使用edit 链接来修改原始问题的标题和内容。另一个是开始一个新问题,这个问题可以作为“离题/不可重现”关闭(或者你可以删除它)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 2014-06-15
      • 1970-01-01
      相关资源
      最近更新 更多