Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Declare Function DeleteUrlCacheEntry Lib "wininet" Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long
'下载网络图片
Sub DownloadImageName(ByVal ImageURL As String, ByVal ImagePath As String)
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, ImageURL, ImagePath, 0, 0)
If lngRetVal = 0 Then
DeleteUrlCacheEntry ImageURL '清除缓存
'MsgBox "成功"
Else
'MsgBox "失败"
End If
End Sub Sub OneKeyCreateExam()
Dim ImgNames As Variant
Dim strText As String
Dim i As Long, n As Long, m As Long
Dim OneTagP As Object
Dim OneTagA As Object
Dim TagP As Object
Dim PosText As String
Dim Arr() As String
ReDim Arr(1 To 1) As String
Dim Brr() As String
ReDim Brr(1 To 1)
Dim ImageURL As String
Dim FilePath As String
Dim FileName As String Dim dContent As Object
Set dContent = CreateObject("Scripting.Dictionary")
Dim dImageName As Object
Set dImageName = CreateObject("Scripting.Dictionary") Dim StartTime As Variant '开始时间
Dim UsedTime As Variant '使用时间
StartTime = VBA.Timer '记录开始时间 AppSettings
On Error GoTo ErrHandler '设置URL,访问网页获取网页源码
URL = ActiveSheet.Range("A2").Text
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.Send
strText = .responsetext
End With '创建网页文件
With CreateObject("htmlfile")
.write strText
'获取标题
FileName = .getElementsByTagName("h2")(0).innerhtml
Debug.Print FileName Application.StatusBar = ">>>>>>正在下载图片>>>>>>" i = 0 '初始化序号 For Each OneTagA In .getElementsByTagName("a") '循环所有A标签
If OneTagA.HasChildNodes Then
If OneTagA.href Like "http://photo.blog.sina.com.cn/showpic.html*" Then '获取之前的一个段落
Set TagP = OneTagA.PreviousSibling
Do While TagP.tagName <> "P"
Set TagP = TagP.PreviousSibling
Loop i = i + 1 '文字内容提取
PosText = TagP.innerhtml
PosText = RegReplace(PosText, "<.*?>")
PosText = Replace(PosText, " ", "") '获取图片URL
ImageURL = OneTagA.FirstChild.getAttribute("real_src")
ImageName = "Image" & i & ".jpg"
ImagePath = ThisWorkbook.Path & Application.PathSeparator & ImageName
DownloadImageName ImageURL, ImagePath '下载图片 '获取图片
If dImageName.Exists(PosText) = False Then
dImageName(PosText) = ImageName
Else
dImageName(PosText) = dImageName(PosText) & "|" & ImageName
End If End If
End If
Next Application.StatusBar = ">>>>>>正在获取文本>>>>>>" i = 0 '初始化序号
n = 0 '初始化序号
For Each OneTagP In .getElementsByTagName("p")
'文字内容提取
PosText = OneTagP.innerhtml
PosText = RegReplace(PosText, "<.*?>")
PosText = Replace(PosText, " ", "") i = i + 1 If PosText = "喜欢" Then Exit For '提前结束循环
If i > 20 Then '开始记录试卷内容
If Len(PosText) > 0 Then '保留非空数组
n = n + 1
ReDim Preserve Arr(1 To n)
Arr(n) = PosText '存入数组
'Debug.Print n; " "; PosText
'dContent(PosText) = n
End If
End If
Next
End With Application.StatusBar = ">>>>>>正在创建Word文档>>>>>>" FilePath = ThisWorkbook.Path & "\" & FileName & ".doc"
On Error Resume Next
Kill FilePath
On Error GoTo 0 Dim wdApp As Object
Dim Doc As Object
Set wdApp = CreateObject("Word.Application")
Set Doc = wdApp.documents.Add() Doc.Activate For i = 1 To UBound(Arr) PosText = Arr(i) wdApp.Selection.TypeText Text:=PosText
wdApp.Selection.TypeParagraph If dImageName.Exists(PosText) Then '如果含有图片
If InStr(dImageName(PosText), "|") = 0 Then '如果只含有一张图片
ImageName = dImageName(PosText)
ImagePath = ThisWorkbook.Path & Application.PathSeparator & ImageName
wdApp.Selection.InlineShapes.AddPicture FileName:=ImagePath, LinkToFile:=False, SaveWithDocument:=True
wdApp.Selection.TypeParagraph
Else
ImgNames = Split(dImageName(PosText), "|")
For n = LBound(ImgNames) To UBound(ImgNames) Step 1
ImageName = ImgNames(n)
ImagePath = ThisWorkbook.Path & Application.PathSeparator & ImageName
wdApp.Selection.InlineShapes.AddPicture FileName:=ImagePath, LinkToFile:=False, SaveWithDocument:=True
wdApp.Selection.TypeParagraph
Next n
End If
End If Next i Doc.SaveAs FilePath
Doc.Close
wdApp.Quit Application.StatusBar = ">>>>>>正在删除Image图片>>>>>>" For Each Key In dImageName.keys
If InStr(dImageName(Key), "|") = 0 Then
ImageName = dImageName(Key)
ImagePath = ThisWorkbook.Path & Application.PathSeparator & ImageName
Kill ImagePath
Else
ImgNames = Split(dImageName(Key), "|")
For n = LBound(ImgNames) To UBound(ImgNames) Step 1
ImageName = ImgNames(n)
ImagePath = ThisWorkbook.Path & Application.PathSeparator & ImageName
Kill ImagePath
Next n
End If
Next Key UsedTime = VBA.Timer - StartTime
MsgBox "本次运行耗时:" & Format(UsedTime, "#0.0000秒") ErrorExit:
Set wdApp = Nothing
Set Doc = Nothing AppSettings False
Exit Sub
ErrHandler:
If Err.Number <> 0 Then
MsgBox Err.Description & "!", vbCritical, "QQ 84857038"
Debug.Print Err.Description
Err.Clear
Resume ErrorExit
End If End Sub
Public Function RegReplace(ByVal OrgText As String, ByVal Pattern As String, Optional RepStr As String = "") As String
'传递参数 :原字符串, 匹配模式 ,替换字符
Dim Regex As Object
Dim newText As String
Set Regex = CreateObject("VBScript.RegExp")
With Regex
.Global = True
.Pattern = Pattern
End With
newText = Regex.Replace(OrgText, RepStr)
RegReplace = newText
Set Regex = Nothing
End Function
Public Sub AppSettings(Optional IsStart As Boolean = True)
If IsStart Then
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.StatusBar = ">>>>>>>>Macro Is Running>>>>>>>>"
Else
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.StatusBar = False
End If
End Sub

  

GetTextAndImageCreateExamPaper的更多相关文章

随机推荐

  1. EditPlus 4.3.2555 中文版已经发布

    新的版本修复了之前版本出现的多行文本缩进调整的问题. 下载连接在页面左上角!

  2. ASP.NET控件--DropDownList

      设置默认值:DropDownList1.Items[i].Selected=true;绑定:DropDownList1.DataSource = dataSet.Tables["Tabl ...

  3. Js基础知识4-函数的三种创建、四种调用(及关于new function()的解释)

    在js中,函数本身属于对象的一种,因此可以定义.赋值,作为对象的属性或者成为其他函数的参数.函数名只是函数这个对象类的引用. 函数定义 // 函数的三种创建方法(定义方式) function one( ...

  4. 在 Linux 中使用超级用户权限

    在你想要使用超级权限临时运行一条命令时,sudo 命令非常方便,但是当它不能如你期望的工作时,你也会遇到一些麻烦.比如说你想在某些日志文件结尾添加一些重要的信息,你可能会尝试这样做: $ echo & ...

  5. web前端----JavaScript(JS)函数

    函数 函数定义 JavaScript中的函数和Python中的非常类似,只是定义方式有点区别. // 普通函数定义 function f1() { console.log("Hello wo ...

  6. Sony/索尼 NW-ZX300A ZX300 无损音乐播放器4.4口

    https://item.taobao.com/item.htm?spm=a1z0d.7625083.1998302264.6.5c5f4e69ELHOcm&id=557859816402 ( ...

  7. c++ 11开始语言本身和标准库支持并发编程

    c++ 11开始语言本身和标准库支持并发编程,意味着真正要到编译器从语言和标准库层面开始稳定,估计得到17标准出来.14稳定之后的事情了,根据历史经验,新特性的引入到稳定被广泛采用至少要一个大版本的跨 ...

  8. UESTC 594 我要长高 - 单调性优化

    韩父有N个儿子,分别是韩一,韩二…韩N.由于韩家演技功底深厚,加上他们间的密切配合,演出获得了巨大成功,票房甚至高达2000万.舟子是名很有威望的公知,可是他表面上两袖清风实则内心阴暗,看到韩家红红火 ...

  9. C++ tinyXML的使用和字符编码转换

    转载:http://jetyi.blog.51cto.com/1460128/761708/ 关于tinyxml使用的文档有很多(这篇文章就写的很好),这里仅提一下字符编码的转换问题,如果你不熟悉字符 ...

  10. python中的迭代器和生成器学习笔记总结

    生成器就是一个在行为上和迭代器非常类似的对象.   是个对象! 迭代,顾名思意就是不停的代换的意思,迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果.每一次对过程的重复称为一次“迭代”,而 ...