【问题标题】:Why can't I access the contents of this pop up box with VBA?为什么我无法使用 VBA 访问此弹出框的内容?
【发布时间】:2017-04-19 04:08:31
【问题描述】:

这是弹出框的html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- BEGIN /layouts/new_empty_layout.jsp -->











<html>
	<head>
		<title>SERFF - System for Electronic Rate and Form Filing</title>
		<!--    Copyright 2016 National Association of Insurance Commissioners -->


<link rel="stylesheet" type="text/css" href="/styles/serff-core.css"/>
<link rel="stylesheet" type="text/css" href="/styles/core-header.css"/>
<link rel="stylesheet" type="text/css" href="/styles/core-content.css"/>
<link rel="stylesheet" type="text/css" href="/scripts/jquery-ui-css/custom-theme/jquery-ui-1.10.4.custom.css"/>
		




<script type="text/javascript">
	var staticUrlPath = '/';
	var appUrlPath = '/serff/';
</script>
<script type="text/javascript" src="/scripts/jquery-1.12.1.min.js"></script>
<script type="text/javascript" src="/scripts/jquery.textarea.pack.js"></script>
<script type="text/javascript" src="/scripts/jquery-ui-1.10.0.min.js"></script>
<script type="text/javascript" src="/scripts/datepicker.js"></script>
<script type="text/javascript" src="/scripts/default_layout.js"></script>





<script>
	// custom SERFF dimensions:
	// dimension1 = SERFF User Type custom GA dimension

	var analyticsEnabled = true;
	
	// insert the analytics script and initialize the Google Analytics "ga" object
	if(analyticsEnabled){
		var currentScript = document.currentScript || (function() {
			var scripts = document.getElementsByTagName('script');
				return scripts[scripts.length - 1];
		    })();
		
		var gaScript = document.createElement('script');
		gaScript.async = true;
		gaScript.src = '/public/lib/ga/analytics.js';
		document.getElementsByTagName("head")[0].insertBefore(gaScript, currentScript);
		
		var ga = window.ga = window.ga || function(){(window.ga.q = window.ga.q || []).push(arguments)};
		window.ga.l = +new Date();
		window.GoogleAnalyticsObject = 'ga';
		ga('create', 'UA-5133885-7', 'auto');
	}
	
	function analyticsPageView()
	{		
		doSendAnalytics(function() {
			ga('send', 'pageview');
		});
	}

	function analyticsPageViewWithUserType()
	{	doSendAnalytics(function() {	
			ga('send', 'pageview', {
				'dimension1' : getUserType()
			});
		});
	}

	function analyticsEvent(eventCategory, eventAction, callback){
		if(callback){
			doSendAnalytics(function() {
				ga('send', 'event', eventCategory, eventAction, {'hitCallback': callback});
			});
		} else {
			doSendAnalytics(function() {
				ga('send', 'event', eventCategory, eventAction);
			});
		}		
	}

	function analyticsEventWithUserType(eventCategory, eventAction, callback){
		if(callback){
			doSendAnalytics(function() {
				ga('send', 'event', eventCategory, eventAction, {'hitCallback': callback, 'dimension1' : getUserType()});
			});
		} else {
			doSendAnalytics(function() {
				ga('send', 'event', eventCategory, eventAction, {'dimension1' : getUserType()});
			});
		}
	}

	function getUserType(){
		var userType = 'I';
	    if(userType == 'S'){
	        userType = 'State User';
	    } else if(userType == 'I') {
			userType = 'Industry User';
	    } else if (userType == 'N') {
			userType = 'NAIC Admin User';
		}
	    return userType;
	}

	// only send a message to GA if enabled in the INI file
	function doSendAnalytics(method){		
		if(analyticsEnabled){
			method();
		}
	}
	

</script>
		
		<link rel="stylesheet" type="text/css" href="/styles/forms2.css"/>
		<!-- BEGIN /layouts/iefixes.jsp -->












	

	

	

	


<!-- END /layouts/iefixes.jsp -->

		

	</head>
	
	<body>
		<div id="content">
			<div id="main" style="background: none;">
				<!--    Copyright 2014 National Association of Insurance Commissioners -->
<!-- BEGIN /filing/ -->








<html>
<head>
<title>SERFF - System for Electronic Rate and Form Filing</title>
<link rel="stylesheet" type="text/css"
	href="/styles/serff-core.css" />
<link rel="stylesheet" type="text/css"
	href="/styles/core-header.css" />
<link rel="stylesheet" type="text/css"
	href="/styles/core-content.css" />
<link rel="stylesheet" type="text/css"
	href="/styles/forms2.css" />
<!-- BEGIN /layouts/iefixes.jsp -->












	

	

	

	


<!-- END /layouts/iefixes.jsp -->


	<script language="javascript">
		var uploadSubmitted = false;
		$( document ).ready(function() {
			$( 'form' ).submit(function( event ) {
				if (uploadSubmitted) {
					alert( "You have already performed this action.  Please wait for the page to refresh." );
					event.preventDefault();
				} else {
					uploadSubmitted = true;
				}
			});
		});
	
		var skipcycle = false;
		function updateAttachments() {
			
			var groupId = window.opener.selectedAttachmentGroup;
			if (uploadForm.groupId.value == null || uploadForm.groupId.value ==''){
				uploadForm.groupId.value = groupId;
			}
			var index = window.opener.selectedIndex;
			if (uploadForm.index.value == null || uploadForm.index.value ==''){
				uploadForm.index.value = index;
			}
			
		}
		
		function focusOnMe(){
		if (!skipcycle){
		window.focus(); 
		}
		mytimer = setTimeout('focusOnMe()', 500);
		}						
		
	</script>
</head>
<body onload = "mytimer = setTimeout('focusOnMe()', 500);updateAttachments();">
<h2>SERFF File Attachment Upload</h2>
<div class="documentArea">
	<form name="uploadForm" method="POST" action="/serff/uploadFileAttachment.do" class="styled" enctype="multipart/form-data" id="uploadForm">
		<input type="hidden" name="groupId" />
		<input type="hidden" name="index" />
		
		
			
				
			
			
		
		<input type="hidden" name="attachmentType" value="0">
	
		<div class="uploadWarning" style="width:325px">
			Attachments larger than 3MB or Microsoft Word(tm) documents cannot be uploaded to SERFF.
		</div>
		
			
		<div class="fieldgroup">File 1:&nbsp;&nbsp; <input type="file" name="contentIndexed[0]" value="" onfocus="skipcycle=true" class="inputFile"></div>
	
		<div class="fieldgroup">File 2:&nbsp;&nbsp; <input type="file" name="contentIndexed[1]" value="" onfocus="skipcycle=true" class="inputFile"><br />
		</div>
	
		<div class="fieldgroup">File 3:&nbsp;&nbsp; <input type="file" name="contentIndexed[2]" value="" onfocus="skipcycle=true" class="inputFile"><br />
		</div>
	
		<div class="fieldgroup">File 4:&nbsp;&nbsp; <input type="file" name="contentIndexed[3]" value="" onfocus="skipcycle=true" class="inputFile"><br />
		</div>
		<div class="fieldgroup">File 5:&nbsp;&nbsp; <input type="file" name="contentIndexed[4]" value="" onfocus="skipcycle=true" class="inputFile"><br />
		</div>
		<br />
		<div class="actionButtons">
			<input type="submit" value="Upload" class="inputSubmit">
			<input type="button" class="inputButton" onclick="window.close();" value="Cancel" />
		</div>
	</form>
</div>
</body>
</html>
<!-- END /filing/fileUpload.jsp -->

			</div> <!-- end main div --> 
		</div> <!-- end content div -->
	</body>
</html>
<!-- END /layouts/new_empty_layout.jsp -->

我正在尝试通过使用 vba “单击”其中一个浏览按钮来显示上传文件对话框。问题是vba 不会让我在uploadForm 中找到输入标签。

我自己的代码:

Set uploadForm = ieUpload.document.getElementById("uploadForm")
For Each inputTag In uploadForm.getElementsByTagName("input")
If inputTag.Name = "contentIndexed[0]" Then inputTag.Click
Next inputTag

Vba 在 for 循环中引发“对象变量或未设置块变量”错误。

【问题讨论】:

  • 我已经更新了这个问题。我的目标是打开文件对话框并使用sendkeys(颤抖)输入文件名。我尝试使用发送键通过窗口选项卡并选择浏览按钮,但也无法正常工作。

标签: javascript html excel vba


【解决方案1】:

在聊天讨论后编辑

原来文档还没有完成加载。检查ieUpload.Busy 就可以了。例如,参见this question,它在DoEvents 循环中检查Busy

原答案

首先:

  • 确保您的源文件顶部有Option Explicit
  • 确保您已将对象引用添加到您正在使用的 HTML 库中。除非您别无选择,否则不要使用CreateObject
  • 确保您已使用 Dim 为所有变量指定特定类型 - 而不是 VariantObject

现在,假设您使用的是MSHTML,则没有函数document.getElementsById。你想要的是getElementById,“元素”上没有“s”。单步执行代码——当你点击 For 循环时,我怀疑 uploadFormNothing,所以 uploadForm.getElementsByTagName 会引发错误。

编辑 那么我为什么要在上面提到添加参考呢?因为如果您添加了引用,并且您说过Dim uploadForm as MSHTML.IHTMLElement2,VBA 编辑器会自动将大小写更改为uploadForm.getElementsByTagName。既然没有,我认为您没有 Dim,或者它不是特定的 Dim

(当然,除非您创建了一个名为 getelementsbytagname 的变量,否则您不应该这样做:)

Here's 一个 MSHTML 示例,链接自 this answer

【讨论】:

  • 不错的收获,谢谢,我需要复习要点。我仍然得到同样的错误。就好像uploadForm 中根本没有输入标签一样。我不明白为什么它不合作:(
  • @Seraphim 所以当你在调试器中单步运行时,uploadForm 的值是多少?此外,您可以添加 Debug.Print 语句或使用 Watch 窗口查看变量的值。尝试thisthis 获取一些调试教程。
  • 出于某种原因,我得到了 uploadForm = 什么都没有。为什么在html中有id“uploadForm”时会这样说?
猜你喜欢
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 2018-09-17
  • 2012-05-20
  • 1970-01-01
  • 2013-10-14
  • 2016-03-29
  • 2020-11-03
相关资源
最近更新 更多