vba 在书签处插入图片(或如何判断是否选择了任何列表框项目)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16152041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting a picture at a bookmark (OR how to tell if any list box items have been selected)
提问by MrPopinjay
I'm new to VBA and still struggling a lot.
我是 VBA 的新手,仍然很挣扎。
I have a list object on a useform that is populated with the file-names of the contents of the relative ".\logos\" directory. I want to insert the picture at a bookmark named bmLogo
, but the code I've written (see below) doesn't do the trick.
我在一个 useform 上有一个列表对象,该对象填充了相对“.\logos\”目录内容的文件名。我想在名为 的书签中插入图片bmLogo
,但我编写的代码(见下文)并不能解决问题。
If ListLogo <> Null Then
ActiveDocument.Bookmarks("bmLogo").Range _
.InlineShapes.AddPicture FileName:=ThisDocument.Path & "\logos\" & ListLogo
End If
Any tips? Also, if I could set a height and have the image scale to it without changing the aspect ratio that would be very useful!
有小费吗?另外,如果我可以设置一个高度并将图像缩放到它而不改变纵横比,那将非常有用!
Thanks,
Louis
谢谢,
路易斯
EDIT 1: Right, so, bmLogo is the correct name of the bookmark, so that's not the problem. I just used a msgbox to display '"path: " & ThisDocument.Path & "\logos\" & ListLogo' and it looks like the correct path. I'm using ThisDocument.Path as I want it to be relative so the document is more portable. I think I'll try with an absolute path for the time being and seeing if that work, if nothing else it should hint at where the bug is not.
编辑 1:是的,所以 bmLogo 是书签的正确名称,所以这不是问题。我只是用一个 msgbox 来显示 '"path:" & ThisDocument.Path & "\logos\" & ListLogo',它看起来像正确的路径。我正在使用 ThisDocument.Path 因为我希望它是相对的,所以文档更便携。我想我会暂时尝试使用绝对路径,看看它是否有效,如果没有其他方法,它应该暗示错误不在何处。
EDIT 2: It works with an absolute path outside the IF statement and when I get a msgbox to print both the absolute and relative path they are identical. After commenting out the IF statement the relative method works find. I'm glad that it's working now but can anyone tell what the issue is with the IF?
编辑 2:它适用于 IF 语句之外的绝对路径,当我得到一个 msgbox 来打印绝对路径和相对路径时,它们是相同的。注释掉 IF 语句后,相对方法可以找到。我很高兴它现在可以工作了,但谁能告诉我 IF 的问题是什么?
EDIT 3: It turns out that my method for checking if something has been selected in the list box doesn't work at all. Instead I shall be iterating through each item in the list and checking if that one is selected. It's a pretty crude method but it'll do until I can find a better one.
编辑 3:事实证明,我检查列表框中是否选择了某些内容的方法根本不起作用。相反,我将遍历列表中的每个项目并检查是否选择了该项目。这是一种非常粗略的方法,但在我找到更好的方法之前它会这样做。
回答by Gaffi
For the record, this works for me in 2010, so I'm guessing there's either a problem with your filename (ListLogo
), your bookmark (bmLogo
), or else the filepath (ThisDocument.Path
).
作为记录,这在 2010 年对我有用,所以我猜你的文件名 ( ListLogo
)、书签 ( bmLogo
) 或文件路径 ( ThisDocument.Path
) 有问题。
1:
1:
Does the filename match the format you expect? Is it just the filename, or a full path? Does it include the proper extension?
文件名是否符合您期望的格式?它只是文件名还是完整路径?它包括正确的扩展名吗?
2:
2:
Does the bookmark exist in your document?
您的文档中是否存在书签?
3:
3:
Does ThisDocument
refer to what you think it does? The simplification below works for me.
难道ThisDocument
是指你所认为的呢?下面的简化对我有用。
ActiveDocument.Bookmarks("TEST").Range.InlineShapes.AddPicture FileName:="P:\test.png"
I saved the .docx
file in my P:\
path, so the following should have worked:
我将.docx
文件保存在我的P:\
路径中,因此以下内容应该有效:
ActiveDocument.Bookmarks("TEST").Range.InlineShapes.AddPicture FileName:=ThisDocument.Path & "\test.png"
However, the path returned was my AppData directory. When I watched the ThisDocument
object, I saw that it actually pointed to the Normal.dotm
template, where the code was created when I recorded a macro to test this out.
但是,返回的路径是我的 AppData 目录。当我观察这个ThisDocument
对象时,我看到它实际上指向了Normal.dotm
模板,当我录制一个宏来测试它时,代码就是在模板中创建的。
Can you please verify that each of these three items are correct and what you expect?
您能否验证这三项中的每一项是否正确以及您所期望的?
- ListLogo
- bmLogo
- ThisDocument
- 列表标志
- 标志
- 这个文件