VBA在文件夹名称中使用通配符

时间:2016-06-23 10:53:56

标签: excel vba

到目前为止,我发现了许多指南和工作辅助如何在文件名中使用通配符(我知道如何使用)但是当它在文件夹路径中使用它时,我没有遇到什么麻烦。这是我的代码:

public static void ExtractAttachment(string targetDir)
{

    SearchFilter.IsEqualTo ffrom = new SearchFilter.IsEqualTo(EmailMessageSchema.From, "xxx@yyyy.com.tr");
    SearchFilter.ContainsSubstring fsubject = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "yyyyyyyy", ContainmentMode.Substring, ComparisonMode.IgnoreCaseAndNonSpacingCharacters);
    SearchFilter.IsEqualTo fattach = new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true);
    SearchFilter.IsGreaterThanOrEqualTo fdate = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, DateTime.Now.Date);
    SearchFilter.SearchFilterCollection mfilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, fdate, ffrom, fattach, fsubject);

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
    service.Credentials = new WebCredentials(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);
    service.Url = new Uri(ConfigurationManager.AppSettings["ExchangeService"]);
    ItemView view = new ItemView(24);
    view.PropertySet = new PropertySet(ItemSchema.DateTimeReceived, ItemSchema.Subject, EmailMessageSchema.HasAttachments, EmailMessageSchema.From);
    view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
    view.Traversal = ItemTraversal.Shallow;
    FindItemsResults<Item> searchitem = service.FindItems(WellKnownFolderName.Inbox, mfilter, view);
    if (searchitem.TotalCount > 0)
    {
        for (int i = 0; i < searchitem.Items.Count; i++)
        {
            Console.WriteLine("loop is working");

            searchitem.Items[i].Load(PropertySet.FirstClassProperties);

            foreach (Attachment part in searchitem.Items[i].Attachments)
            {
                if (part is FileAttachment && string.IsNullOrEmpty(part.Name) == false && Path.GetExtension(part.Name).Equals(".txt", StringComparison.InvariantCultureIgnoreCase))
                {

                    using (MemoryStream ms = new MemoryStream())
                    {
                        FileAttachment fileAttachment = part as FileAttachment;
                        fileAttachment.Load(ms);
                        ms.Position = 0;

                        string fname = Path.Combine(targetDir, fileAttachment.Name);
                        string fdir = Path.GetDirectoryName(fname);
                        if (!Directory.Exists(fdir)) Directory.CreateDirectory(fdir);

                        using (FileStream fstream = new FileStream(fname, FileMode.Create))
                        {
                            fstream.Write(fileAttachment.Content, 0, fileAttachment.Content.Length);
                        }

                    }
                }
                break;
            }
        }
    }

}

myFolder = Cells(k, 7) Archive_Path_0 = "C:\New Folder\" & myFolder & "* \" 'wildcard in folder name myFileName = Cells(r, 8) CountName = Len(myFileName) Windows(myFileName).Activate ActiveWorkbook.SaveAs Archive_Path_0 & Left(myFileName, CountName - 4) & " AFTER.xls" 是变量,可以从excel电子表格及其常量中获取。我想将myFolder保存在myFileName位置,但只是部分名称的全名,可由其他人更改或修改。想解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这是有助于我和它的作品的代码!

Sub ShowFolderList()
Dim fs, f, f1, s, sf As Object
folderspec = "C:\windows\"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set sf = f.SubFolders
NextRow = 2
i = 2
Do While Cells(i, 2) <> ""
    Fund = Cells(i, 2)
    For Each f1 In sf
        s = s & f1.Name
        If Left(s, 4) = Fund Then
            Cells(i, 3).Value = s
        End If
        s = ""
        NextRow = NextRow + 1
    Next f1
    i = i + 1
    NextRow = 2
Loop
MsgBox ("Done!")

End Sub