请登录[¤ 阳光论坛 ¤]参与讨论


阳光宝宝
90

 □ 主题: 转载VB控制
 □ 内容: 1楼

      QQ:有没有办法得到一个excle表的行的总数和列的总数?? 
      AA: 
      xlSheet.UsedRange.Rows.Count 
      xlSheet.UsedRange.Cols.Count 
       
      QQ: 
      如何打开一个word的模板! 
      最近做一个word的模板程序,打开word是Set NewDoc = MyWord.Documents.Add 
      这是一个新的doc,名字叫文档1(后面会累加,自动的),但是现在我希望直接新建打开一个我写好的模板程序,名字还是叫文档1。请问应该怎么写! 
      AA: 
      On Error Resume Next ''忽略错误 
      Set Wrd = GetObject(, "Word.Application") ''查找一个正在运行的Word拷贝 
      If Err.Number <> 0 Then ''如果 Word 没有运行则 
      Set Wrd = CreateObject("Word.Application") ''运行它 
      End If 
      Err.Clear ''清除发生错误的 Err 对象 
      On Error GoTo 0 ''保留普通错误进程 
       
      Dim dot As String 
      Dim doc As String 
      Wrd.Visible =true 
      dot = "C:\temp.dot" 
      doc = "c:\temp.doc" 
      Documents.Open FileName:=dot, _ 
      ConfirmConversions:=False, ReadOnly:=True, AddToRecentFiles:=False, _ 
      PasswordDocument:="", PasswordTemplate:="", Revert:=False, _ 
      WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _ 
      0 
      Wrd.ActiveDocument.Close 
      Wrd.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 
       
      WORD中打字 
      Selection.TypeText Text:="您好,先生" 
       
      如何让Word的保存命令调用我自己编写的保存方法? 
      在doc文档被修改以后,在退出的时候当提示用户文档已经修改,问是否保存 
      和直接点击保存按钮的时候,能否让Word去调用我自己编写的SaveDoc方法, 
      如何实现? 
      创建名为“FileSave”的宏,把你的代码写入在这个宏中。当点击“保存”按钮或退出程序要求“保存”时,都会直接执行你的“FileSave”宏,而不再执行Word的内置命令。 
       
       
      怎么样判断Word文档中有没有图片? 
      Word里的图片包括两种(据我所知),楼上的漏掉了InlineShapes 
      If ActiveDocument.Shapes.Count + ActiveDocument.InlineShapes.Count > 0 Then 
      MsgBox "esit" 
      Else 
      MsgBox "Document doesn''t contain a shape" 
      End If 
       
       
      QQ:怎样在程序中把图片写入到word中。 
      AA: 
      功能是:在指定位置插入图片并设置图片的格式(包括设置为衬托于文字下    方) 
      Sub 设置图片格式() 
      Selection.InlineShapes.AddPicture FileName:="D:\Mypicture\800x600.jpg", _ 
      LinkToFile:=False, SaveWithDocument:=True 
      Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend 
      Selection.InlineShapes(1).ConvertToShape.Select 
      Selection.ShapeRange.Fill.Visible = msoFalse 
      Selection.ShapeRange.Fill.Transparency = 0# 
      Selection.ShapeRange.Line.Weight = 0.75 
      Selection.ShapeRange.Line.DashStyle = msoLineSolid 
      Selection.ShapeRange.Line.Style = msoLineSingle 
      Selection.ShapeRange.Line.Transparency = 0# 
      Selection.ShapeRange.Line.Visible = msoFalse 
      Selection.ShapeRange.LockAspectRatio = msoTrue 
      Selection.ShapeRange.Height = 361.4 
      Selection.ShapeRange.Width = 481.6 
      Selection.ShapeRange.PictureFormat.Brightness = 0.5 
      Selection.ShapeRange.PictureFormat.Contrast = 0.5 
      Selection.ShapeRange.PictureFormat.ColorType = msoPictureAutomatic 
      Selection.ShapeRange.PictureFormat.CropLeft = 0# 
      Selection.ShapeRange.PictureFormat.CropRight = 0# 
      Selection.ShapeRange.PictureFormat.CropTop = 0# 
      Selection.ShapeRange.PictureFormat.CropBottom = 0# 
      Selection.ShapeRange.RelativeHorizontalPosition = _ 
      wdRelativeHorizontalPositionColumn 
      Selection.ShapeRange.RelativeVerticalPosition = _ 
      wdRelativeVerticalPositionPage 
      Selection.ShapeRange.Left = wdShapeCenter 
      Selection.ShapeRange.Top = wdShapeCenter 
      Selection.ShapeRange.LockAnchor = False 
      Selection.ShapeRange.WrapFormat.AllowOverlap = True 
      Selection.ShapeRange.WrapFormat.Side = wdWrapBoth 
      Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0) 
      Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0) 
      Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0.32) 
      Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0.32) 
      Selection.ShapeRange.WrapFormat.Type = 3 
      ‘实际上真正有用的是下面的语句设置图片为衬托于文字下方 
      Selection.ShapeRange.ZOrder msoSendBehindText 
      End Sub 
       
      QQ:有没有方法,把一个word文件,按编号返回一段一段的段落文章。 
      比如 
      1 XXXX 
      1.1 YYYY 
      1.1.1 ZZZZ 
      QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ 
      1.1.2 SSSS 
      PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP 
       
      能够返回 一个编号对应的一个标题和一段话。 
      谢谢 
      AA: 
      加载Microsoft Word 9.0 Object Library 
      Dim dcApp As Word.Application 
      Dim dcWd As Word.Document 
       
      Private 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 
      Next 
       
       
      dcWd.Close 
      Set dcWd = Nothing 
      dcApp.Quit 
      Set dcApp = Nothing 
      End Sub 
       
      QQ:如何用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 string 
       
      s="要插入的字符" 
      Set wrdapp = CreateObject("Word.Application") 
      strFileName = App.Path & "\temp.dot" 
      Set wrddoc = wrdapp.Documents.Open(strFileName) 
      i = wrddoc.Range.GoTo(wdGoToBookmark, , , "a") 
      后面该如何写,才能将s插入到bookmark后面?另外我上面的代码正确吗?谢谢 
      ?????????????? 
       
       
      QQ: VBA:在word文档中搜索单词“审稿人”,将光标移到它后面,插入图片?怎么实现!!! 
      我现在可以打开文件和插入图片了,就是没有搜索单词、移动光标、插入指定大小的图片的功能,那位老大出来指点指点呀? 
       
      我的代码如下: 
      Option Explicit 
      Dim Doc As New Document 
      Dim Visi As Boolean 
      Dim wordApp As Word.Application 
      Dim docWord As Word.Document 
      Private Sub Command2_Click() 
      Set wordApp = New Word.Application 
      Set docWord = Word.Documents.Open("D:\test.doc") 
      ActiveDocument.Shapes.AddPicture ("d:\shan.jpg") 
      docWord.Save 
      docWord.Close 
      wordApp.Application.Quit() 
      End Sub 
      AA: 
      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 = False 
       
      wordApp.Find.Execute 
       
       
      QQ:请问如何将加载的菜单不是以悬浮的菜单形式出现? 
      AA:CommandBar的Position属 
       
      QQ: 在vb 中将word文件保存为纯文本 
      AA: 
      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 Sub 
       
       
      QQ:怎么在一个word文档的最后插入东西 
      AA: 
      WORD XP的VB帮助中有示例: 
      本示例在名为“Changes.doc”的文档末尾插入文本。“For Each...Next”语句用来判断此文档是否已打开。 
       
      For Each aDocument In Documents 
      If InStr(LCase$(aDocument.Name), "changes.doc") Then 
      Set myRange = Documents("Changes.doc").Content 
      myRange.InsertAfter "the end." 
      End If 
      Next aDocument 
       
      QQ:在WORD的宏里如何编写查询页数和行数, 
      AA: 
      ActiveDocument.BuiltInDocumentProperties(wdPropertyPages) 
      ActiveDocument.BuiltInDocumentProperties(wdPropertyLines) 
       
       
      QQ:请问网页考到word后,那个向下的箭头是什么标记,如何自动替换掉?手工替换很麻烦阿!但又不知道这是什么标记,无法下手阿。急用!! 
      AA: 
      手工换行符 
       
      可以用查找替换的,或者试试这个 
       
      Sub blankdel() 
      Dim i As Integer 
      For i = ActiveDocument.Paragraphs.Count To 1 Step -1 
      ActiveDocument.Paragraphs(i).Range.Select 
      a = Selection.Words.Count 
      If a = 1 Then 
      Selection.Delete 
      m = m + 1 
      End If 
      Next i 
      MsgBox "共删除空白段落" & m & "个" 
      End Sub 
       
       
      按ALT+F11,把代码COPY进去运行一下就行了 
       
       
      QQ:请问怎么样用word对象中控制word的分页? 
      AA: 
      我刚刚试出来的:此VB6下的代码可以解你问题 
       
      Set pword = CreateObject("Word.Application") 
      pword.Documents.Add dotName, False 
      pword.WindowState = 2 ''wdWindowStateMinimize ''2 
       
      pword.Selection.InsertBreak wdPageBreak 
       
       
      QQ:我想控制用户只能修改Word文件的部分内容,所以需要设置部分区域为只读,不知道该用什么方法,请大家赐教。 
      AA: 
      Selection.InsertBreak Type:=wdSectionBreakContinuous 
      ActiveDocument.Sections(1).ProtectedForForms = False 
      ActiveDocument.Sections(2).ProtectedForForms = True 
      ActiveDocument.Protect Password:="", NoReset:=False, Type:= _ 
      wdAllowOnlyFormFields 
       
       
      QQ:请问怎么插入图片的 
      AA: 
      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 = wdWrapNone 
       
       
      QQ:如何访问表格单元格内容 
      AA: 
      objWordDoc.Tables(3).Cell(1, 1).Range.Text
——
      
争分夺秒背单词 → master  n.主人;能手;硕士
 □ 发帖时间:2012-8-24|14:40:22 |回复|返回|



阳光宝宝
90

 □ 主题: 回应:转载VB控制
 □ 内容: 2楼

      VB程序操作word表格(文字、图片) 2008-01-12 16:20:40| 分类: Visual Basic | 标签: |字号大中小 订阅 . 
       
       很多人都知道,用vb操作excel的表格非常简单,但是偏偏项目中碰到了VB操作word表格的部分,google、baidu搜爆了,都没有找到我需要的东西。到是搜索到了很多问这个问题的记录。没办法,索性只有自己去尝试了。下面把一些代码发上来,给需要的朋友一点提示。 
       打开一个已经存在的wrod文件(这个文件包含了表格) 
       
      Dim WordApp 
      Dim Word 
      Set WordApp = CreateObject("Word.Application") 
      WordApp.Visible = True 
      Set Word = WordApp.Documents.Open("c:\record.dot") 知道了就很简单了,下面是选定某一个表格的一个单元格,并修改其内容 
       
      Word.Tables(1).cell(1, 2)="内容" VBA中的这些数组元素下标都是从1开始的,比如excel的第一行一列也是ExSheet.Cells(1,1),而不是ExSheet.Cells(0,0),WORD的表格也是这样,不信自己试一下就知道了。所以上面那句话的意思就是对整个word文档中的第一个表格的第一行第二列的内容改变为“内容”。很简单吧?网上有些人在问是不是 
      Word.Tables(1).cell(1, 2).range.text或者Word.Tables(1).cell(1, 2).text。试一下就发现这2种都不对。 
       插入图片其实也很简单,代码如下: 
       
      Word.Tables(1).cell(1, 3).Range.InlineShapes.AddPicture ("c:\photo.jpg") 
      说到这,肯定又有人会问怎么在一个word里插入一个表格。其实很简单: 
       
      Call word.Tables.Add(wdBook.application.Selection.Range, 16, 5, 1, 0) 如果你的程序里涉及到合并及拆分单元格,那么你可能试一下这段代码: 
       
      dim Table  
      set Table = wdApp.ActiveDocument.Tables.Add(wdApp.Application.Selection.Range, NumRows:=27, NumColumns _  
      :=7, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _  
      wdAutoFitFixed) 
       
      Set mySelection = wdApp.Documents.Application.Selection  
      mySelection.Cells.Borders(-7).LineStyle = 1  
      ''选中表格的第2行第3列  
      table.Cell(2, 3).Select  
      ''向下移动6格,第1个参数和第3个是常数  
      Call wdBook.Application.Selection.MoveDown(5, 6, 1)  
      ''合并  
      wdBook.Application.Selection.Cells.Merge  
      ''拆分成7行2列  
      Call wdBook.Application.Selection.Cells.Split(7, 2, True)  
       如果大家碰到了更复杂的程序,用程序生成起来比较麻烦,那么你就可以用模板来实现了?你可以先用word做一个模板,把表格什么的全都先写好,然后保存成模板文件。然后你再用程序加载这个模板,然后往模板里填写数据。这样难度要低一些。不过具体情况具体分析。word这些集合的下标都是从1开始,然后只要找到表格那个集合,然后选取第一个表格就是要操作的表格了(假设你想操作的表格是模板中的第一个表格)。 代码如下: 
       
      dim table 
       
      set table=wdApp.ActiveDocument.Tables(1) 
       
       总之微软的那一套东西集成得很不错,其之间任意调用非常方便,大家如果想用VB对WORD做更多的应用,却又不知道怎么实现,我想最好的办法就是录制宏了,你把你想完成的功能操作一遍,然后查看宏,一目了然了吧?呵呵。。。.不过宏代码和真正VB的代码是有点差别的,需要转换一下,多用几次就清楚了。(
——
      
争分夺秒背单词 → engine  n.发动机,引擎;机车
 □ 发帖时间:2012-8-24|14:41:48 |回复|返回|



阳光宝宝
90

 □ 主题: 回应:转载VB控制
 □ 内容: 3楼

      VB 读Word表格单元格数据包含回车和黑点处理办法  
       今天因工作需要做一个小程序,其中一个功能就是将Word里的表格数据导入到Oracle中去.VB操作Word简单明了,很好用,当然用VB了.关键的语句如下: 
       
       Set WordApp = CreateObject("Word.Application") 
       WordApp.Visible = False 
       Set word = WordApp.Documents.Open("c:\test.doc") 
       
       msgbox word.tables(i).cell(3, 7).Range.Text 
       
       结果发现弹出的对话框不但换了行,而且有一个黑点,我想这是因为Word的格子里有特殊字符的原因吧,找了许久办法都没找到这样一个方法.后来索性自己写了一个方法如下: 
       
       Function filterWordTableText(ByVal str) 
       If str = "" Then 
       filterWordTableText = "" 
       End If 
       filterWordTableText = Replace(Replace(str, Chr(13), ""), "", "") 
       End Function 
       
      然后修改一下刚才的程序就可以了: 
       
       msgbox filterWordTableText(word.tables(i).cell(3, 7).Range.Text) 
       
      我到目前为止还是这样弄的,要是哪个知道有更好的办法一定要告诉我啊,谢谢! 
       
      
——
      
争分夺秒背单词 → urge  vt.推进;催促 n.冲动
 □ 发帖时间:2012-8-24|14:42:40 |回复|返回|

 页次:1/1页 每页10  本主题贴数2 分页: 1


你还没有登录论坛,所以不能发表你的意见。你可以选择:

1、我已注册,我要

2、我还没注册,我要

3、太麻烦了,我还是

Go Top

Copyright by(C)2003-2015 http://abc.sy578.cn