Show picture/image from userform in print header, VBA MS Project - vba

I am making a template and thought to store our logo.jpg inside a userform. Then I'll call this userform and insert the logo in the print header for each "macro activated" print.
So far I have managed to print the image only as numbers. Bits and bytes probably(?). I might be missing some conversion of the picture before printing:
FilePageSetupHeader Alignment:=pjLeft, Text:="&P" & UserForm1.Image1.Picture & " "
This prints as: 1-670746914 in the top left header.
What am I missing?
The syntax I found on MSDN gives the following parameters for inserting a picture:
&;P""path"" Inserts the specified image. An example would be &;P"" [My Documents] \Image.gif"". The term [My Documents] represents the full path to your My Documents folder.
My code makes a copy of the current view, make changes to the view settings, headers etc, before exporting to PDF and deleting the view again.

UserForm1.Image1.Picture is a picture object, not the path to the original source file. Once a picture is loaded into an image control, it is embedded and its original path is not stored.
Store the path the the picture so that you can reference it later (e.g. using the Tag property of the Image control). See Stack Overflow: VBA UserForm Get Filename for more details. (FYI: Excel and Project use the same UserForm object, so this is applicable.)

I gave up on saving the logo inside the Project file itself.
Instead I check if the logo exists inside "C:\CompanyLogo", if does not, create that direcotry and download it from imgur.

Related

How to save RichTextBox text to a text resource file?

I have created a text file named 'Notes' in the resources of my project and want to load/save the text of a RichTextBox to it. I tried using MyProject.My.Resources.Notes in the path. It doesn't show any error in the code window but when I run it, it shows an error.
Here's what I'm using to load the text:
RichTextBox1.Text = System.IO.File.ReadAllText(MyProject.My.Resources.Notes)
Here's what I'm using for saving it:
System.IO.File.WriteAllText(MyProject.My.Resources.Notes, RichTextBox1.Text)
This is not a duplicate. I did not get an answer from the other question
Why don't you use a regular file which always works fine or a database?
Edit: maybe this helps?
www.dondraper.com/2011/01/easily-save-and-retrieve-application-and-user-settings-in-vb-net-or-c-apps/

VBA Save a String across submodule calls

I am making an "intelligent save" button for word and excel files.
The first time it is run from a file, it will prompt user to navigate to the correct folder. The important part is the selected path will be saved for that file and automatically referenced the next time someone uses the macro. Then the user can specify pdf vs. docx/xlsx file type, then save the file.
Is the bolded part possible, and what kind of technique/functions can I use to do this?
Posted as an answer, with a bit of example code and more detail:
For such a small amount of data, why not use VBA's SaveSetting/GetSetting commands to put the needed info in the registry?
Sub SaveGetSettingExample()
' Saves string values to:
' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\AppName
SaveSetting "AppName", "Section", "Key", "Value"
' Displays the just-stored value: Value
MsgBox GetSetting("AppName", "Section", "Key", "Default Value")
End Sub
I'm assuming you're embedding the code in the workbook (and not in the Personal code folder). If so, I've had success writing the file path to a cell in a hidden column (usually out to far right of view) in Excel. Your code can reference this default location as needed when loading the next time.
I'm not sure about Word, our Word documents stay pretty simple.

I am having trouble inserting an image as stimuli from the builder view.

I am trying to import an image as stimuli from an excell spreadsheet. It is at .tiff image and am receiving this eror "targ.setImage(stimuli) NameError:name 'stimuli' is not defined. I named the column in excell 'stimuli' and used 'PsychoPy\VisualCC\Stimuli\targ.tiff' (without the quotations).
In the image properties I named it targ and set the image to $stimuli. From what I can tell this is accurate. Please advise.
Glitch_311
The usual cause for this error is that you need to set the button next to the image field to "set every repeat" rather than "constant".
The variable name "stimuli" only has meaning within the loop that refers to the conditions file containing it, so if the image is set to be constant, when the image is created at the start of the experiment, it tries to use the variable named "stimuli" but it doesn't exist yet.

Inserting a picture at a bookmark (OR how to tell if any list box items have been selected)

I'm new to VBA and still struggling a lot.
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.
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.
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?
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.
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).
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:
Does the bookmark exist in your document?
3:
Does ThisDocument refer to what you think it does? The simplification below works for me.
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:
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.
Can you please verify that each of these three items are correct and what you expect?
ListLogo
bmLogo
ThisDocument

What kind of variable is "&[Page]" in VB?

Today I discovered the expression "&[Page]" in some old VB code. It seems to hold a pagenumber in case some document would get printed. I (VB novice!) didn't know this existed but it did help me out seriously today! So I wonder whether there are more such "hidden" variables. Where can I find an overview?
I thought this is an "environment variable" or a "system variable" but Googling these seems to suggest that "&[Page]" belongs to neither category.
These are Header and Footer elements.
From the Header and footer tools (image from excel 2010):
Click this button to:
Page Number: Insert the &[Page] code that puts in the current page number.
Number of Pages: Insert the &[Pages] code that puts in the total number of pages.
Current Date: Insert the &[Date] code that puts in the current date.
Current Time: Insert the &[Time] code that puts in the current time.
File Path: Insert the &[Path]&[File] codes that put in the directory path along with the name of the workbook file.
File Name: Insert the &[File] code that puts in the name of the workbook file.
Sheet Name: Insert the &[Tab] code that puts in the name of the worksheet as shown on the sheet tab.
Picture: Insert the &[Picture] code that inserts the image that you select from the Insert Picture dialog box that shows the contents of the my Pictures folder on your computer by default.
Format Picture: Apply the formatting that you choose from the Format Picture dialog box to the &[Picture] code that you enter with the Insert Picture button without adding any code of its own.