- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Outlook 宏,可以将用户任务列表
导出到存储在网络驱动器上的 Excel 电子表格。
我正在尝试检查目录 ( If statement taken form here ) 中是否已存在工作簿。
如果没有,则创建一个新工作簿,其中包含一个名为“工作表 1”的工作表,如果已有一个具有正确用户名的工作簿,则将其打开 ( add statement taken from here ):
感谢SO ,我已经正确修复了我遇到的命名错误,但现在新创建的工作簿没有保存在目录文件夹中。没有抛出任何错误,并且宏末尾的消息框正确显示,因此我不知道为什么该文件没有显示在文件资源管理器中。
这是我的整个程序:
Sub Task_Grab_V2()
Dim sKillExcel As String
Dim strReport As String
Dim olnameSpace As Outlook.NameSpace
Dim taskFolder As Outlook.MAPIFolder
Dim tasks As Outlook.Items
Dim tsk As Outlook.TaskItem
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Dim sht As Excel.Worksheet
Dim NAME_s As String
Dim Range As Excel.Range
Dim str As String, strClean As String
Dim z As Integer
Dim strMyName As String
Dim x As Integer
Dim y As Integer
Dim stat_string As String
Dim r As Range, s As String, iloc As Long
Dim s1 As String, cell As Range, col As Long
Dim sChar As String
Dim strUserName As String
objExcel.DisplayAlerts = False
'Use the Application Object to get the Username
NAME_s = Environ("USERNAME")
Dim FilePath As String
Dim TestStr As String
FilePath = "some\directory" & NAME_s & ".xlsx"
TestStr = ""
On Error Resume Next
TestStr = Dir(FilePath)
On Error GoTo 0
If TestStr = "" Then
Set exWb = objExcel.Workbooks.Add(1)
exWb.Sheets("Sheet1").Name = "Sheet1Old"
exWb.Sheets.Add().Name = "Sheet1"
exWb.Sheets("Sheet1Old").Delete
Else
Set exWb = objExcel.Workbooks.Open("some\directory" & NAME_s & ".xlsx")
exWb.Sheets.Add().Name = "Sheet1"
exWb.Sheets("Sheet1_old").Delete
End If
Set olnameSpace = Application.GetNamespace("MAPI")
Set taskFolder = olnameSpace.GetDefaultFolder(olFolderTasks)
Set tasks = taskFolder.Items
strReport = ""
'Create Header
exWb.Sheets("Sheet1").Cells(1, 1) = "Subject"
exWb.Sheets("Sheet1").Cells(1, 2) = "Category"
exWb.Sheets("Sheet1").Cells(1, 3) = "Due Date"
exWb.Sheets("Sheet1").Cells(1, 4) = "Percent Complete"
exWb.Sheets("Sheet1").Cells(1, 5) = "Status"
exWb.Sheets("Sheet1").Cells(1, 6) = "Notes"
y = 2
For x = 1 To tasks.Count
Set tsk = tasks.Item(x)
'strReport = strReport + tsk.Subject + "; "
'Fill in Data
If Not tsk.Complete Then
If tsk.Status = olTaskDeferred Then
stat_string = "Deferred"
End If
If tsk.Status = olTaskInProgress Then
stat_string = "In Progress"
End If
If tsk.Status = olTaskNotStarted Then
stat_string = "Not Started"
End If
If tsk.Status = olTaskWaiting Then
stat_string = "Waiting on Someone Else"
End If
exWb.Sheets("Sheet1").Cells(y, 1) = tsk.Subject
exWb.Sheets("Sheet1").Cells(y, 2) = tsk.Categories
exWb.Sheets("Sheet1").Cells(y, 3) = tsk.DueDate
exWb.Sheets("Sheet1").Cells(y, 4) = tsk.PercentComplete
exWb.Sheets("Sheet1").Cells(y, 5) = stat_string
exWb.Sheets("Sheet1").Cells(y, 6) = tsk.Body
'the following section searches the body of the task for a specified character and deletes everything after it
col = 6 ' assumes column 6, change to your column
sChar = "#" ' assume character to look for is hash, change to yours
With objExcel.ActiveSheet
Set r = .Range(.Cells(2, col), .Cells(.Rows.Count, col).End(xlUp))
End With
For Each cell In r
s = cell.Text
If Len(Trim(s)) > 0 Then
iloc = InStr(1, s, sChar, vbTextCompare)
If iloc > 1 Then
s1 = Left(s, iloc - 1)
cell.Value = s1
Else
If iloc <> 0 Then
cell.ClearContents
End If
End If
End If
Next cell
y = y + 1
stat_string = ""
End If
Next x
'Autofit all column widths
On Error Resume Next
For Each sht In objExcel.ActiveWorkbook.Worksheets
sht.Columns("A").EntireColumn.AutoFit
sht.Columns("B").EntireColumn.AutoFit
sht.Columns("C").EntireColumn.AutoFit
sht.Columns("D").EntireColumn.AutoFit
sht.Columns("E").EntireColumn.AutoFit
sht.Columns("F").EntireColumn.AutoFit
Next sht
exWb.Save
exWb.Close
Set exWb = Nothing
'this kills the excel program from the task manager so the code will not double up on opening the application
sKillExcel = "TASKKILL /F /IM Excel.exe"
Shell sKillExcel, vbHide
MsgBox ("Tasks have been sucessfully exported.")
End Sub
有人能明白为什么上面的代码不会保存创建的文件吗?
最佳答案
您正在此处保存工作簿:
exWb.Save
如果工作簿是在此处创建的:
If TestStr = "" Then
Set exWb = objExcel.Workbooks.Add(1)
那么您没有指定工作簿的文件名,因此如果它是 Book1
,那么您的我的文档<中很可能有一个新的 Book1.xlsx
文件/em> 文件夹。
如果已经存在 Book1.xlsx
文件,objExcel
实例会弹出警报:
我需要在这里做出假设,但我的理论是1 objExcel
是一个 Excel 应用程序实例,创建它是为了“运行在后台”,它是不可见的。但即使应用程序不可见,通常您也会看到该警告框。除非您明确禁用它:
objExcel.DisplayAlerts = False
禁用警报后,保存
将仅覆盖现有文件。
因此,您不会遇到任何错误,但该文件不在您期望的文件夹中,也不具有您保存它的文件名,但它已创建。
如果您想将文件保存在指定路径/文件名下,请使用SaveAs
方法而不是Save
- but that's no news .
1 its just declared as Dim objExcel As New Excel.Application. – scb998 2 mins ago
关于excel - 由 Outlook 宏创建的新 Excel 工作簿未保存在目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44013989/
我正在为我的程序编写安装脚本,它应该在 Linux/Unix 操作系统上运行。以下文件的默认目录是什么: 可执行文件(程序)。程序应通过从命令行键入其名称来执行。 共享库。 第三方共享库(程序未开源,
我有一堆用户、组和应用程序注册,我的 MVC 应用程序使用 AAD 数据进行身份验证和授权。是否可以将 Azure Active Directory 从一个租户(目录)迁移到另一个租户(目录)?如果可
查看 cljsbuild 文档 https://github.com/emezeske/lein-cljsbuild :cljsbuild { :builds [{ ; The
忽略已经版本控制的文件 如果你不小心添加了一些应该被忽略的文件,你如何将它们从版本控制中去除而不会丢失它们?或许你有自己的IDE配置文件,不是项目的一部分,但将会花费很多时间使之按照自己的方式工作。
我想使用\tableofcontents 命令,但没有目录从新页面开始或在末尾创建新页面,并且所有内容都是单倍行距。我怎样才能做到这一点?我假设使用 tocloft,但有哪些选择? 谢谢 最佳答案 试
我有一些 javascript 菜单代码,可以在单独的目录中正常工作。但是,当我尝试从同一目录中调用相同的 .js 文件时,它不会看到这些文件。 以下内容来自另一个目录: script type="t
我有这样的路径: /my/path/to/important_folder 在同一级别上,我还有其他文件和文件夹想要在达到与 important_folder 相同的级别时列出。 我的文件夹可能更深,
1、获取文件路径实现 1.1 获取当前文件路径 ? 1
我正在使用最新版本的 NTEmacs。 我写了一个名为“.dir-locals.el”的文件,如下所示。 ((nil . ((tab-width . 8) (fill-column .
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
在我的 .vimrc 中有这些行 :set foldmethod=marker :set foldmarker=SECTION:,ENDSECTION: 用于自定义代码折叠。在我的文件中,相关语言的注
在 fish 中: for x in * echo $x end *这里包括所有目录和文件,如何只列出文件(或目录)? 最佳答案 fish 没有很多花哨的通配语法。但是,目录可以像这样迭代: f
这是我的目录结构: ├── src │ ├── helpers │ │ ├── __init__.py │ │ ├── foo.py │ │ └── bar.py │
我想递归重命名文件夹/目录名称并找到 this solution所以。但是这个命令没有效果 find . -type f -exec rename 's/old/new/' '{}' \; 这是一个正
我想在相册中创建一个文件夹,并希望将图像保存在创建的相册中。 这可能吗?有什么办法可以做到这一点吗? 我已经搜索过,大多数人都说这是不可能的。 感谢您的帮助。 最佳答案 您也许可以使用AssetsLi
如何在python中使用用户定义的名称创建临时文件/目录。我知道 tempfile .但是我看不到任何以文件名作为参数的函数。 注意:我需要这个来对包含临时文件的临时目录上的 glob(文件名模式匹配
我在项目中使用JaCoCo Gradle插件。 作为问题的一个示例,我的大部分代码都在com.me.mysoftware包下。 我正在使用代码生成器来生成build/generated/java/..
我正在尝试使用 Gradle 开始运行 jar 文件 我的任务如下所示: task startServer(type: Exec) { workingDir file("${buildDir}/a
如何在 Ant 中定义一个目录集,其中包括两个目录:项目的基目录和子目录“test”? 看起来您无法使用“/”、“.”或“”专门包含目录集的根目录。例如,这包括“./test”,但不包括“.”:
我正在使用 CTAGs 包,它使用 Sublime Text 2 生成两个文件 .tags 和 .tags_sorted_by_file。 那么当我进行项目搜索(CMD + SHIFT + F)时,如
我是一名优秀的程序员,十分优秀!