如何从带有通配符的目录中插入对象?

时间:2017-11-03 02:33:00

标签: excel excel-vba vba

Worksheets("Sheet3").OLEObjects.Add filename:="C:\Users\myname\Desktop\" & fdcode & "*INVESTMENT INCOME DETAIL.pdf", Link:=False, DisplayAsIcon:=False, Left:=40, Top:=40, Width:=150, Height:=10

fdcode是基金代码的变量(go figure)

不让我因某种原因插入

基本上我有一堆基金代码,报告不同。例如:

FundA Investment Income Detail  
FundA Tax Summary

FundB Investment Income Detail  
FundB Tax Summary

唯一的问题是在FUND和REPORT NAME之间可能会有一些随机字符。

我需要能够放置某种外卡,而忽视它们之间的内容。

1 个答案:

答案 0 :(得分:1)

首先,找到您要使用的文件名:

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;
int main()
{
    while (1)
    {
        string directory = "C:/";
        string promptInput;
        string afterPrint;
        string prompt = "| " + directory + " |> ";
        cout << prompt;
        cin >> promptInput;

        if (promptInput == "") 
        {

        }
        else if (promptInput == "h:/")
        {
            directory = "H:/";
        }
        if (promptInput != "") {
            cout << "    " << afterPrint << "\n";
        }
    }
    return 0;
}

这会将Dim fName As String fName = Dir("C:\Users\myname\Desktop\" & fdcode & "*INVESTMENT INCOME DETAIL.pdf") 设置为它在该目录中找到的第一个文件,该文件与您提供的规范相匹配。

然后在其他代码中使用该文件名(不包括目录名,因此需要继续指定)

fName