Skip to content

Commit 825baa3

Browse files
Sébastien GeiserSébastien Geiser
Sébastien Geiser
authored and
Sébastien Geiser
committed
Enables launching Excel from search results
Adds functionality to open and highlight cells in Excel directly from search results. It achieves this by adding a VBScript to launch Excel and navigate to the correct sheet and cell, and integrating this script into the application's search result navigation.
1 parent 77c2d93 commit 825baa3

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed

RegexDialog/LaunchExcelVbsScript.vbs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
' Check arguments
2+
If WScript.Arguments.Count < 3 Then
3+
WScript.Echo "Usage: cscript open_excel.vbs ""file_path"" ""sheet_name"" ""cell"""
4+
WScript.Echo "Example: cscript open_excel.vbs ""C:\MyFile.xlsx"" ""My Sheet"" ""B5"""
5+
WScript.Quit
6+
End If
7+
8+
' Get parameters
9+
filePath = WScript.Arguments(0)
10+
sheetName = WScript.Arguments(1)
11+
cell = WScript.Arguments(2)
12+
13+
' Check if file exists
14+
Set fso = CreateObject("Scripting.FileSystemObject")
15+
If Not fso.FileExists(filePath) Then
16+
WScript.Echo "Error: File '" & filePath & "' does not exist"
17+
WScript.Quit
18+
End If
19+
20+
On Error Resume Next
21+
22+
' Try to connect to already opened file
23+
Set xl = GetObject(,"Excel.Application")
24+
25+
If Err.Number <> 0 Then
26+
Set xl = CreateObject("Excel.Application")
27+
xl.Visible = True
28+
Set wb = xl.Workbooks.Open(filePath)
29+
Else
30+
fileFound = False
31+
For Each wb In xl.Workbooks
32+
If UCase(wb.FullName) = UCase(filePath) Then
33+
fileFound = True
34+
Set wb = wb
35+
Exit For
36+
End If
37+
Next
38+
39+
' Si le fichier n'est pas ouvert, l'ouvrir
40+
If Not fileFound Then
41+
Set wb = xl.Workbooks.Open(filePath)
42+
End If
43+
End If
44+
45+
' Activate sheet and cell
46+
xl.Visible = True
47+
48+
' Force Excel to the foreground
49+
xl.WindowState = -4143 ' xlMaximized
50+
Set sh = CreateObject("WScript.Shell")
51+
sh.AppActivate xl.Caption
52+
53+
On Error Resume Next
54+
Set ws = xl.Sheets(sheetName)
55+
ws.Activate
56+
Set rng = ws.Range(cell)
57+
rng.Activate
58+
rng.Select

RegexDialog/RegExToolDialog.xaml.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ List<RegexResult> GetMatchesFor(string text, string fileName = "", int selection
724724
GetMatchesFor(GetCellText(cell))
725725
.Select(match =>
726726
{
727+
match.Parent = excelSheetResult;
727728
match.InfoSup = cell.Address.ToString();
728729
return match;
729730
}));
@@ -1377,7 +1378,7 @@ private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e
13771378
{
13781379
try
13791380
{
1380-
if (sender is TreeViewItem treeViewItem && treeViewItem.DataContext is RegexResult regexResult)
1381+
if (sender is TreeViewItem treeViewItem && treeViewItem.DataContext is RegexResult regexResult && regexResult is not RegexExcelSheetResult)
13811382
{
13821383
if (regexResult.FileName.Length > 0
13831384
&& !GetCurrentFileName().Equals(regexResult.FileName, StringComparison.OrdinalIgnoreCase)
@@ -1386,6 +1387,24 @@ private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e
13861387
{
13871388
SetPosition?.Invoke(regexResult.Index, regexResult.Length);
13881389
}
1390+
else if(Config.Instance.TextSourceOn == RegexTextSource.Excel)
1391+
{
1392+
var searchResult = regexResult;
1393+
RegexExcelSheetResult excelSheetResult = null;
1394+
1395+
while (searchResult != null)
1396+
{
1397+
if(searchResult is RegexExcelSheetResult)
1398+
excelSheetResult = (RegexExcelSheetResult)searchResult;
1399+
searchResult = searchResult.Parent;
1400+
}
1401+
1402+
if (excelSheetResult != null)
1403+
{
1404+
string launchExcelVbsScriptPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "LaunchExcelVbsScript.vbs");
1405+
Process.Start(launchExcelVbsScriptPath, $"\"{Config.Instance.TextSourceExcelPath}\" \"{excelSheetResult.SheetName}\" \"{regexResult.InfoSup}\"");
1406+
}
1407+
}
13891408

13901409
e.Handled = true;
13911410
}
@@ -1400,14 +1419,33 @@ private void TreeViewItem_KeyDown(object sender, KeyEventArgs e)
14001419
if (e.Key == Key.Enter
14011420
&& sender is TreeViewItem treeViewItem
14021421
&& treeViewItem.DataContext is RegexResult regexResult
1403-
&& regexResult.FileName.Length > 0
1404-
&& !GetCurrentFileName().Equals(regexResult.FileName, StringComparison.OrdinalIgnoreCase))
1422+
&& regexResult is not RegexExcelSheetResult)
14051423
{
1406-
if ((TryOpen?.Invoke(regexResult.FileName, false) ?? false)
1424+
if (regexResult.FileName.Length > 0
1425+
&& !GetCurrentFileName().Equals(regexResult.FileName, StringComparison.OrdinalIgnoreCase)
1426+
&& (TryOpen?.Invoke(regexResult.FileName, false) ?? false)
14071427
&& regexResult is not RegexFileResult)
14081428
{
14091429
SetPosition?.Invoke(regexResult.Index, regexResult.Length);
14101430
}
1431+
else if (Config.Instance.TextSourceOn == RegexTextSource.Excel)
1432+
{
1433+
var searchResult = regexResult;
1434+
RegexExcelSheetResult excelSheetResult = null;
1435+
1436+
while (searchResult != null)
1437+
{
1438+
if (searchResult is RegexExcelSheetResult)
1439+
excelSheetResult = (RegexExcelSheetResult)searchResult;
1440+
searchResult = searchResult.Parent;
1441+
}
1442+
1443+
if (excelSheetResult != null)
1444+
{
1445+
string launchExcelVbsScriptPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "LaunchExcelVbsScript.vbs");
1446+
Process.Start(launchExcelVbsScriptPath, $"\"{Config.Instance.TextSourceExcelPath}\" \"{excelSheetResult.SheetName}\" \"{regexResult.InfoSup}\"");
1447+
}
1448+
}
14111449

14121450
e.Handled = true;
14131451
}

RegexDialog/RegexDialog.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
</ItemGroup>
5252
<ItemGroup>
5353
<None Include="FodyWeavers.xsd" />
54+
<None Update="LaunchExcelVbsScript.vbs">
55+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
56+
</None>
5457
<None Update="Resources\ModeV2.xsd" />
5558
<None Update="Resources\Regex_syntax_color.xshd" />
5659
</ItemGroup>

RegexDialog/Res.Designer.cs

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)