将路径“Const as String”更改为Variable

时间:2016-01-23 16:20:18

标签: excel vba excel-vba

我想在文件夹中生成文件列表 - 包括属性。带有宏的Excel文件将与其使用的文件位于同一文件夹中。

问题是Excel文件和所有其他文件将在不同的Windows计算机之间同步,因此附加宏的文件夹路径必须是相对的,因为它在每台计算机上都不同。

SortDescription

尝试过的想法(例如“\”或“...... \”等),搜索论坛。

完整的脚本:

Const STRFOLDER As String = "D:\GIS-Projekte_Sync\"

2 个答案:

答案 0 :(得分:0)

// Prepare professor representation Map<String, Object> professor = new HashMap<>(); professor.put("name", "someone"); professor.put("degree", "PhD"); // Prepare request HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_TYPE, "application/json"); HttpEntity<Map<String, Object>> request = new HttpEntity<>(professor, headers); // Send prepared request ResponseEntity<String> response = template.postForEntity(REST_SERVICE_URI + "/saveProf", request, String.class); 为您提供当前文件的路径,以便在相对路径不适合您时使用该路径。但是,您不能在路径中使用CONST,您必须使用标准变量。

您需要记住的另一件事是VBA中的路径现在可能是URI,并且这并不总是与代码的旧部分(例如public class SearchableAdapter extends BaseAdapter implements Filterable { private List<String>originalData = null; private List<String>filteredData = null; private LayoutInflater mInflater; private ItemFilter mFilter = new ItemFilter(); public SearchableAdapter(Context context, List<String> data) { this.filteredData = data ; this.originalData = data ; mInflater = LayoutInflater.from(context); } public int getCount() { return filteredData.size(); } public Object getItem(int position) { return filteredData.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { // A ViewHolder keeps references to children views to avoid unnecessary calls // to findViewById() on each row. ViewHolder holder; // When convertView is not null, we can reuse it directly, there is no need // to reinflate it. We only inflate a new View when the convertView supplied // by ListView is null. if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item, null); // Creates a ViewHolder and store references to the two children views // we want to bind data to. holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.list_view); // Bind the data efficiently with the holder. convertView.setTag(holder); } else { // Get the ViewHolder back to get fast access to the TextView // and the ImageView. holder = (ViewHolder) convertView.getTag(); } // If weren't re-ordering this you could rely on what you set last time holder.text.setText(filteredData.get(position)); return convertView; } static class ViewHolder { TextView text; } public Filter getFilter() { return mFilter; } private class ItemFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { String filterString = constraint.toString().toLowerCase(); FilterResults results = new FilterResults(); final List<String> list = originalData; int count = list.size(); final ArrayList<String> nlist = new ArrayList<String>(count); String filterableString ; for (int i = 0; i < count; i++) { filterableString = list.get(i); if (filterableString.toLowerCase().contains(filterString)) { nlist.add(filterableString); } } results.values = nlist; results.count = nlist.size(); return results; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { filteredData = (ArrayList<String>) results.values; notifyDataSetChanged(); } } } )很好地配合。例如,如果文件来自Office 365(例如OneDrive for Business),则路径将是URL,DIR将失败。

答案 1 :(得分:0)

回答我自己的问题:你必须将STRFOLDER定义为Variant。然后,您可以使用“ThisWorkbook.path”来获取文件夹位置。

这里是完整的宏:

Public Sub Auto_Open()
Dim STRFOLDER As Variant
Dim objShell As Object, objFolder As Object
Dim bytIndex As Byte, intColumn As Integer, lngRow As Long
Dim varName, arrHeaders(37)
STRFOLDER = ThisWorkbook.path & "\"
If Dir(STRFOLDER, 16) = "" Then
    MsgBox "Der Ordner " & STRFOLDER & " wurde nicht gefunden!", 64, "Hinweis"
    Exit Sub
End If
Application.ScreenUpdating = False
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(STRFOLDER)
intColumn = 1
For bytIndex = 0 To 37
    arrHeaders(bytIndex) = objFolder.GetDetailsOf(varName, bytIndex)
    Cells(1, intColumn + bytIndex) = arrHeaders(bytIndex)
Next
Rows(1).Font.Bold = True
lngRow = 2
For Each varName In objFolder.Items
    For bytIndex = 0 To 37
        Cells(lngRow, intColumn + bytIndex) = objFolder.GetDetailsOf(varName, bytIndex)
    Next
    lngRow = lngRow + 1
Next
Columns.AutoFit
Set objShell = Nothing
Set objFolder = Nothing
Application.ScreenUpdating = True

End Sub