人气 3701

word相关的VBA编程技巧 [复制链接]

Iella 2016-11-19 23:32:43
word相关的VBA编程技巧1.如何打开一个word的模板?最近做一个word的模板程序,打开word是Set NewDoc = MyWord.Documents.Add这是一个新的doc,名字叫文档1(后面会累加,自动的),但是现在我希望直接新建打开一个我写好的模板程序,名字还是叫文档1。请问应该怎么写!On Error Resume NextSet Wrd = GetObject(, "Word.Application")  '查找一个正在运行的Word拷贝If Err.Number  0 Then  '如果 Word 没有运行则Set Wrd = CreateObject("Word.Application")  '运行它End IfErr.Clear ‘ '清除发生错误的 Err 对象On Error GoTo 0  '保留普通错误进程Dim dot As StringDim doc As StringWrd.Visible =truedot = "C:\temp.dot"doc = "c:\temp.doc"Documents.Open FileName:=dot, ConfirmConversions:=False, ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", Format:= 0Wrd.ActiveDocument.CloseWrd.Documents.Add Template:=dot, NewTemplate:=False '结果系列操作
ActiveDocument.SaveAs FileName:=doc, FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False
'打印出来
ActiveDocument.PrintOut FileName:=doc, Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
Wrd.ActiveDocument.Close
2.如何让Word的保存命令调用我自己编写的保存方法?在doc文档被修改以后,在退出的时候当提示用户文档已经修改,问是否保存和直接点击保存按钮的时候,能否让Word去调用我自己编写的SaveDoc方法,如何实现?创建名为“FileSave”的宏,把你的代码写入在这个宏中。当点击“保存”按钮或退出程序要求“保存”时,都会直接执行你的“FileSave”宏,而不再执行Word的内置命令。3.怎么样判断Word文档中有没有图片?If ActiveDocument.Shapes.Count + ActiveDocument.InlineShapes.Count > 0 ThenMsgBox "exsit"ElseMsgBox "Document doesn 't contain a shape"End If4.如何能够返回 一个编号对应的一个标题和一段话?Dim dcApp As Word.Application
Dim dcWd As Word.DocumentPrivate Sub Form_Load()
Set dcApp = New Word.Application
Set dcWd = dcApp.Documents.Open("c:\rpt.doc")For i = 1 To dcWd.Paragraphs.Count
Debug.Print dcWd.Paragraphs(i).Range.Text
NextdcWd.Close
Set dcWd = Nothing
dcApp.Quit
Set dcApp = Nothing
End Sub5. 如何用vb实现在word的bookmark后插入字符?在word模版中指定了boolmark:a,
Dim wrdapp As Word.Application
Dim wrddoc As Word.Document
Dim strFileName As String
Dim i As Variant
dim s as strings="要插入的字符"
Set wrdapp = CreateObject("Word.Application")
strFileName = App.Path & "\temp.dot"
Set wrddoc = wrdapp.Documents.Open(strFileName)
i = wrddoc.Range.GoTo(wdGoToBookmark, , , "a")
后面该如何写,才能将s插入到bookmark后面?另外我上面的代码正确吗?6.在word文档中搜索单词“审稿人”,将光标移到它后面,插入图片?wordApp.Find.ClearFormatting
wordApp.Find.Text = "审稿人"
wordApp.Find.Replacement.Text = ""
wordApp.Find.Forward = True
wordApp.Find.Wrap = wdFindContinue
wordApp.Find.Format = False
wordApp.Find.MatchCase = False
wordApp.Find.MatchWholeWord = False
wordApp.Find.MatchByte = True
wordApp.Find.CorrectHangulEndings = False
wordApp.Find.MatchAllWordForms = False
wordApp.Find.MatchSoundsLike = False
wordApp.Find.MatchWildcards = False
wordApp.Find.MatchFuzzy = FalsewordApp.Find.Execute7.如何将加载的菜单不是以悬浮的菜单形式出现?CommandBar的Position属性设置8.将word文件保存为纯文本?Private Sub Command1_Click()
Dim oW As Object, oD As Object, x As Object
Set oW = CreateObject("word.application")
Set oD = oW.documents.open("c:\aaa.doc")
'oW.Visible = True
For Each x In oD.words
Text1 = Text1 & x
Next x
Open "c:\bbb.txt" For Output As #1
Print #1, Text1
Close #1
oW.quit
End Sub9.:怎么在一个word文档的最后插入东西?本示例在名为“Changes.doc”的文档末尾插入文本。“For Each...Next”语句用来判断此文档是否已打开。For Each aDocument In DocumentsIf InStr(LCase$(aDocument.Name), "changes.doc") ThenSet myRange = Documents("Changes.doc").ContentmyRange.InsertAfter "the end."End IfNext aDocument10.在WORD的宏里如何编写查询页数和行数?ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)如何自动替换掉手工换行符?Sub blankdel()Dim i As IntegerFor i = ActiveDocument.Paragraphs.Count To 1 Step -1ActiveDocument.Paragraphs(i).Range.Selecta = Selection.Words.CountIf a = 1 ThenSelection.Deletem = m + 1End IfNext iMsgBox "共删除空白段落" & m & "个"End Sub按ALT+F11,把代码COPY进去运行一下就行了11.如何用word对象中控制word的分页?Set pword = CreateObject("Word.Application")
pword.Documents.Add dotName, False
pword.WindowState = 2  'wdWindowStateMinimize  '2pword.Selection.InsertBreak wdPageBreak12.如何控制用户只能修改Word文件的部分内容,所以需要设置部分区域为只读?Selection.InsertBreak Type:=wdSectionBreakContinuous
ActiveDocument.Sections(1).ProtectedForForms = False
ActiveDocument.Sections(2).ProtectedForForms = True
ActiveDocument.Protect Password:="", NoReset:=False, Type:= _
wdAllowOnlyFormFields13.Word中怎么插入图片的?Dim myShape As Shape
Dim MyInshape As InlineShape
'插入一个嵌入式图片对象
Set MyInshape = Selection.InlineShapes.AddPicture(FileName:="D:\pic001.jpg", _
LinkToFile:=False, SaveWithDocument:=True)
'将InlineShape对象转变为Shape对象
Set myShape = MyInshape.ConvertToShape
'使图片浮于文字上方
myShape.WrapFormat.Type = wdWrapNone14.如何访问Word表格单元格内容?objWordDoc.Tables(3).Cell(1, 1).Range.Text15.得到一个excle表的行的总数和列的总数?xlSheet.UsedRange.Rows.Count
xlSheet.UsedRange.Cols.Count
您需要登录后才可以回帖 登录 | 立即注册

QQ|手机版|精益人 ( 沪ICP备19004111号-1 )

GMT+8, 2024-6-5 01:16 , Processed in 0.200328 second(s), 19 queries .

Powered by Lean.ren X3.5 Licensed  © 2001-2030 LEAN.REN