Excel VBA - 更改 Javascript 函数的值(无参数)

2023-12-27

由于我是菜鸟,我不知道这是否可以用 VBA Excel 实现。我试图在几个论坛中找到解决方案,但我真的不知道该寻找什么。

我想做的事:我想使用 Excel VBA 在网站上自动上传表单。然而,我很挣扎,因为单击文件上传按钮会打开文件资源管理器(弹出的窗口要求您选择文件)。

我尝试过的:我发现单击“上传”按钮会触发 JavaScript 函数 doSubmit 打开文件资源管理器,然后使用它来上传文件。

是否可以使用 VBA 更改 fileValue 并使用新文件值运行 doSubmit 函数?

function doSubmit() {
var fileValue = jQuery('#file').val();

该按钮的 HTML 如下所示:

<div class="button-wrapper"><input class="design-file-input" type="file"><a class=" button prio1" href="javascript:void(0);">Design hochladen</a></div>

原始的javascript代码如下:

<script type="text/javascript">
var token = "rEjpwK07JxGGqA2jlfG4tzUpqF0fpNKIEf4MZFNhoX8=" || "";
// <![CDATA[
function doSubmit() {
var fileValue = jQuery('#file').val();
if (!fileValue) {
showErrMsg('noFileErrMsg');
return false;
}
if (isVector(fileValue)) {
var form = document.getElementById('upload_design_form');
if(form){
var showError = true;
for (var i = 0; i < form.count_colors.length; i++) {
var obj = form.count_colors[i];
if(obj.checked){
showError = false;
}
}
if(showError){
showErrMsg('colorCountErrMsg');
return false;
}
}
}
if (!document.getElementById('copyright_check').checked) {
showErrMsg('copyrightErrMsg');
return false;
}
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "designUploadToken");
input.setAttribute("value", token);
document.getElementById("upload_design_form").appendChild(input);
return true;
}
function isVector(filename) {
var dotIndex;
if (-1 != (dotIndex = filename.lastIndexOf('.'))) {
if (filename.substr(dotIndex + 1).match(/^(fh\d?\d?|cdr|ai|svg|eps|pdf)$/i))
return true;
}
return false;
}
function showErrMsg(err) {
if (!document.getElementById('errMsg')) {
var errMsg = document.createElement('div');
jQuery(errMsg).addClass('message error').attr('id', 'errMsg');
document.getElementById('errMsgContainer').appendChild(errMsg);
}
jQuery('#errMsg').html(jQuery('#'+err).html());
}
(function($){
if($){
$('#file').on('change', function(e){
var file = e.target.value;
if(isVector(file)){
$('#colorChooser').slideDown();
}else{
$('#colorChooser').slideUp();
}
});
}
})(jQuery);
// ]]>
</script>

如果您有任何建议,我将不胜感激!


我曾经花了几天时间解决类似的问题,我需要将图像上传到网站。最终,我在网上找到了一个脚本,它搜索 OpenFileDialog 并将文件路径粘贴到其中并按下提交。它无法从同一个 Excel 实例触发,因此我需要在不同的 Excel 实例中打开一个包含此代码的不同文件。已经是几年前的事了,所以我不再详细说明。希望它可以帮助您:

Private Declare PtrSafe Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long

 'declere API function to get next window for search

Private Declare PtrSafe Function GetNextWindow Lib "user32" Alias _
"GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long

 'declere API function to get lenth of a windows text

Private Declare PtrSafe Function GetWindowTextLength Lib _
"user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

 'declere API function to get windows text

Private Declare PtrSafe Function GetWindowText Lib "user32" Alias _
"GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

 'declere API function to find in child windows

Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias _
"FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

 'declere API function to find window

Private Declare PtrSafe Function Findwindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare PtrSafe Sub SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal _
hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As _
Long, ByVal cy As Long, ByVal wFlags As Long)

Private Declare PtrSafe Function SetCursorPos Lib "user32" _
(ByVal X As Integer, ByVal Y As Integer) As Long

Private Declare PtrSafe Function GetWindowRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long

Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Declare PtrSafe Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Long, _
ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN As Long = &H2
'~~> Constants for Releasing left button of the mouse
Private Const MOUSEEVENTF_LEFTUP As Long = &H4

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40

Private Const WIN_ClassName_FilePath As String = "COMBOBOXEX32" 'class name of filepathbox
Private Const WIN_ClassName_Button As String = "BUTTON" 'class name of buuton
Private Const WM_SETTEXT = &HC 'send messaget value for set text to file path box
Private Const BM_CLICK = &HF5 'send message value to click button
Private Const WIN_NEXT As Long = 2 'value to search window through next
Private Const WIN_PREVIOUS As Long = 3 'value to search window through previous
Sub MP_FileDialog_automation()
Dim Ret As Long, ChildRet As Long, OpenRet As Long
Dim strBuff As String, ButCap As String, Pos As RECT, X As Long, File_Path As String
File_Path = GetSetting("MPAPP", "FileData", "FilePath")

    '~~> Get the handle of the "File Download" Window
    X = 0
Findwindow:
    X = X + 1
    Ret = Findwindow(vbNullString, "Bestand selecteren voor uploaden")

    If Ret <> 0 Then
        'MsgBox "Main Window Found"
        Dlg_ChildWIN = FindWindowEx(Ret, 0, WIN_ClassName_FilePath, vbNullString)

        If Dlg_ChildWIN <> 0 Then
            Dlg_Retun = SendMessage(Dlg_ChildWIN, WM_SETTEXT, 0, ByVal File_Path) 'set file path

            If Dlg_Retun <> 1 Then 'Ensure that path set successfully if not exit
                MsgBox "Path Not set please try again"
                Exit Sub
            End If

        Else
            MsgBox "File path window not found"
            Exit Sub
        End If
        '~~> Get the handle of the Button's "Window"
        ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString)

        '~~> Check if we found it or not
        If ChildRet <> 0 Then
            'MsgBox "Child Window Found"

            '~~> Get the caption of the child window
            strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
            GetWindowText ChildRet, strBuff, Len(strBuff)
            ButCap = strBuff

            '~~> Loop through all child windows
            Do While ChildRet <> 0
                '~~> Check if the caption has the word "Open"
                '~~> For "Save" or "Cancel", replace "Open" with
                '~~> "Save" or "Cancel"
                If InStr(1, ButCap, "O") Then
                    '~~> If this is the button we are looking for then exit
                    OpenRet = ChildRet
                    'MsgBox OpenRet
                    'Exit Do
                End If

                '~~> Get the handle of the next child window
                ChildRet = FindWindowEx(Ret, ChildRet, "Button", vbNullString)
                '~~> Get the caption of the child window
                strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
                GetWindowText ChildRet, strBuff, Len(strBuff)
                ButCap = strBuff
            Loop
            '~~> Check if we found it or not
            If OpenRet <> 0 Then
                '~~> Retrieve the dimensions of the bounding rectangle of the
                '~~> specified window. The dimensions are given in screen
                '~~> coordinates that are relative to the upper-left corner of the screen.
                GetWindowRect OpenRet, Pos

                '~~> Move the cursor to the specified screen coordinates.
                SetCursorPos (Pos.Left - 10), (Pos.Top - 10)
                '~~> Suspends the execution of the current thread for a specified interval.
                '~~> This give ample amount time for the API to position the cursor
                Sleep 100
                SetCursorPos Pos.Left, Pos.Top
                Sleep 100
                SetCursorPos (Pos.Left + Pos.Right) / 2, (Pos.Top + Pos.Bottom) / 2

                '~~> Set the size, position, and Z order of "File Download" Window
                SetWindowPos Ret, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
                Sleep 100

                '~~> Simulate mouse motion and click the button
                '~~> Simulate LEFT CLICK
                mouse_event MOUSEEVENTF_LEFTDOWN, (Pos.Left + Pos.Right) / 2, (Pos.Top + Pos.Bottom) / 2, 0, 0
                Sleep 700
                '~~> Simulate Release of LEFT CLICK
                mouse_event MOUSEEVENTF_LEFTUP, (Pos.Left + Pos.Right) / 2, (Pos.Top + Pos.Bottom) / 2, 0, 0

            Else
                MsgBox "The Handle of Open Button was not found"
            End If
        Else
             MsgBox "Child Window Not Found"
        End If
    Else
        If X < 4 Then GoTo Findwindow:
    End If
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Excel VBA - 更改 Javascript 函数的值(无参数) 的相关文章

随机推荐

  • Android:如何通过 SQlite 的名称从服务器动态加载图像

    我是 android 新手 在根据 Sqlite 的名称显示来自服务器的图像时遇到问题 IE 我只在 SQLite 数据库中存储图像名称 文本 列名称图像 并且我想根据图像想要在 imageview 中显示的 sqlite 图像名称从服务器
  • Solr 嵌套文档未正确设置

    我正在尝试使用子文档创建 solr 文档 我使用的是solr 8 2 0 为了遵守中的说明https lucene apache org solr guide 8 0 indexing nested documents html index
  • 如何隐藏pygame中的标题栏?

    我想知道有人知道如何隐藏 pygame 任务栏吗 我的 pygame 程序真的需要这个 Thanks 您可以使用pygame NOFRAME screen pygame display set mode 640 480 pygame NOF
  • 具有字符串输入的 Tensorflow 数据集不保留数据类型

    All 可重现的下面的代码在 Google Colab 上使用 TF 2 2 0 rc2 运行 改编简单的例子文档 https www tensorflow org api docs python tf data Dataset用于从简单的
  • AWS Lambda@Edge 调试

    我目前正在研究 lambda edge 函数 我在 CloudWatch 或其他调试选项上找不到任何日志 使用 测试 按钮运行 lambda 时 日志将写入 CloudWatch 当 lambda 函数由 CloudFront 事件触发时
  • 如何解决错误:ORA-01406 获取的列值被截断?

    我正在尝试执行这个查询 select from gi todo isim WHERE d insercao gt 04 JUL 13 AND d insercao lt 25 JUL 13 然而 我得到了这个错误 ORA 01406 fet
  • 如何使用 jQuery Mobile 防止链接添加到历史堆栈

    使用 jQuery mobile 我使用带有上一个和下一个链接的列表视图进行分页 一切正常 但我不希望将上一页和下一页添加到历史堆栈中 这个想法是 回击只会转到实际的上一页 我发现执行此操作的唯一方法是将 data rel dialog 添
  • python 2to3 中的 eval(input())

    来自Python2to3 doc http docs python org library 2to3 input 转换input prompt to eval input prompt 在使用 Python 2 几年后 我目前正在尝试学习
  • Objective C:将字符串转换为枚举

    如果我有一个枚举 typedef enum SOMETHING MyEnum 我有一个 NSString SOMETHING 有没有办法可以直接从字符串转到 ENUM 值 我意识到我可以制作一本字典来做到这一点 但我很好奇 在 Object
  • 直接标签将标签放在错误的位置

    我正在尝试做一个情节ggplot2并尝试将标签放置在具有置信区域的平均轨迹上的正确位置 由于我无法共享我的数据 因此我创建了一个可重现的示例 set seed 456 library ggplot2 library directlabels
  • Leafletjs动态绑定到可见标记/簇

    这与之前的一个问题有关 Leafletjs动态地将地图绑定到可见的叠加层 https stackoverflow com questions 45286918 leafletjs dynamically bound map to visib
  • 在 QML ListView 中按角色对 QAbstractListModel 派生模型进行排序

    我创建了一个基于底层 QHash 的 QAbstractListModel 派生模型 由于我需要在 QML 中使用模型 因此我无法使用 Qt 小部件和视图集成的排序功能 我尝试使用 QSortFilterProxyModel 但它似乎不适用
  • 安装 JAR 文件

    我刚刚下载了 Jsouphttp jsoup org download http jsoup org download jsoup 1 7 2 jar 问题很简单 如何安装 安装最新的 JRE 版本后 我已经尝试过右键单击 gt 打开方式
  • 在 SQL 中使用 UNION 的单个通用 WHERE 条件

    我正在尝试做这样的事情 SELECT a date AS EnrollDate a id a name b address FROM student a JOIN Location b ON a id b id UNION SELECT a
  • 错误:withCallingHandlers 导致 R 崩溃

    我一直在使用基于 plyr 的函数summarySE and ddply几个月没有任何问题 今天 当我在 R 中运行极其基本的例程时 出现了一些错误消息并导致 R 崩溃 这是示例代码以及 R 崩溃之前出现的错误 install packag
  • 为什么我无法在变量中捕获 FakeItEasy 期望?

    我正在使用 FakeItEasy 来伪造一些实体框架调用 以确保一堆奇怪的遗留数据库表得到正确映射 我需要断言发票与特定 DeliveryAddress 匹配的客户已添加到数据库中 如果我这样做 A CallTo gt db Custome
  • UITableView 像 Clear 应用程序一样重新排序

    长按检测后如何开始移动 使用 uilongpressgesturerecognizer 进行长按检测 我的代码 void viewWillAppear BOOL animated super viewWillAppear animated
  • 在 MySQL 中查找包含给定值的列

    我在 MySQL 数据库中有一个表 我得到了一个作为该表中的单元格值出现的值 但我不知道它是哪个单元格 即该单元格的行和列 查找该值所属列的最有效方法是什么 提前致谢 Example Column 1 Column 2 Column 3 1
  • 为什么 T 实现了 A+B 而没有实现特质 C:A+B?

    我对这个例子感到困惑 虽然i32实现了所有的Num One Zero PartialOrd RemAssign Ord 当我将它们放入一个特征中以便我可以给出别名时 它不起作用 use num traits Num identities O
  • Excel VBA - 更改 Javascript 函数的值(无参数)

    由于我是菜鸟 我不知道这是否可以用 VBA Excel 实现 我试图在几个论坛中找到解决方案 但我真的不知道该寻找什么 我想做的事 我想使用 Excel VBA 在网站上自动上传表单 然而 我很挣扎 因为单击文件上传按钮会打开文件资源管理器