以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- [求助]怎么能不通过报表的方式向word插入图片,怎么定义位置? (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=126389) |
|||
-- 作者:Engineer1 -- 发布时间:2018/10/20 9:09:00 -- [求助]怎么能不通过报表的方式向word插入图片,怎么定义位置? 如题,需要用代码定义图片的位置而不是“[图片]" [此贴子已经被作者于2018/10/20 10:04:12编辑过]
|
|||
-- 作者:有点蓝 -- 发布时间:2018/10/20 9:51:00 -- 在需要放图片的地方插入一个书签,书签名称为“图片” 代码 Dim app As New MSWord.Application try Dim fileName = "D:\\问题\\123.docx" Dim doc As object = app.Documents.Open(fileName) Dim item = "图片" \'书签名称 Dim bmk = doc.Bookmarks.Item(item ) Dim start = bmk.Start Dim End1 = bmk.End Dim reftrue = True Dim reffalse = False Dim missing = System.Reflection.Missing.Value Dim img = "D:\\问题\\1.png" \'图片路径 Dim rg = doc.Range(start, End1).InlineShapes.AddPicture(img, False, True) rg.Height=100 \'图片高 rg.Width=100 \'图片宽 Doc.save catch ex As exception msgbox(ex.message) finally app.Quit End try |
|||
-- 作者:Engineer1 -- 发布时间:2018/10/20 11:05:00 -- 老师按您的方法倒是可以,但我的文档是随机生成的,员工也不可能对每个文档添加书签来定义,我试了一下在模板里引用书签,但生成报表后书签就没了,您看还有什么可行的方法吗?我这个实例就是报表生成后审核和批准人看到数据再通过确认按钮将签名插到文档里。 [此贴子已经被作者于2018/10/20 11:09:24编辑过]
|
|||
-- 作者:有点蓝 -- 发布时间:2018/10/20 11:43:00 -- 查找特定字符并替换为图片 Dim app As New MSWord.Application try Dim fileName = "D:\\问题\\123.docx" Dim doc As object = app.Documents.Open(fileName) Dim item = "【图片】" \'被替换的字符 Dim sel = app.Selection sel .Find.ClearFormatting With sel .Find .Text = item .Replacement.Text = "" .Forward = True .Wrap = MSWord.WdFindWrap.wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With sel.Find.Execute Dim img = "D:\\问题\\1.png" \'图片路径 Dim pic = sel.InlineShapes.AddPicture(img, False, True) pic.Height=100 \'图片高 pic.Width=100 \'图片宽 Doc.save catch ex As exception msgbox(ex.message) finally app.Quit End try |
|||
-- 作者:Engineer1 -- 发布时间:2018/10/20 13:47:00 -- 老师我想把替换的字符改为当前用户的名字应该怎么写呢? |
|||
-- 作者:有点蓝 -- 发布时间:2018/10/20 13:53:00 -- .Replacement.Text = user.name |
|||
-- 作者:Engineer1 -- 发布时间:2018/10/20 14:12:00 -- Dim app As New MSWord.Application try Dim fileName = ProjectPath & "attachments\\Reports\\" & e.form.controls("TextBox16").Text & ".doc" Dim doc As Object = app.Documents.Open(fileName) app.ActiveWindow.Selection.WholeStory If app.ActiveWindow.Selection.Find.Execute("图片") Then app.ActiveWindow.Selection.InlineShapes.AddPicture ( projectpath & "images\\张杰.png" ) End If app.Visible = True catch ex As exception msgbox(ex.message) app.Quit finally app.quit End try 老师就是把图片换成当前用户的名字,我还是不知道应该怎么写进去。
|
|||
-- 作者:有点蓝 -- 发布时间:2018/10/20 14:48:00 -- .InlineShapes.AddPicture ( projectpath & "images\\" & user.name &".png" ) |
|||
-- 作者:Engineer1 -- 发布时间:2018/10/20 14:52:00 -- 老师,我不是这个意思,我是想把”图片“这2 个字符替换成用户名字。 |
|||
-- 作者:有点蓝 -- 发布时间:2018/10/20 14:55:00 -- Dim app As New MSWord.Application try Dim fileName = "D:\\问题\\123.docx" Dim doc As object = app.Documents.Open(fileName) Dim item = "【图片】" \'被替换的字符 Dim sel = app.Selection sel .Find.ClearFormatting With sel .Find .Text = item .Replacement.Text = user.name .Forward = True .Wrap = MSWord.WdFindWrap.wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchByte = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With sel.Find.Execute Doc.save catch ex As exception msgbox(ex.message) finally app.Quit End try
|